AX 2012, Calling Webservice, Development, Rest APIs, Uncategorized

Calling Rest API from AX 2012

AX 2012 supports system.Net namespace interface. This interface provides simple programming interface for many of the protocols used on the networks today. Herewith i am sharing a simple example to call a rest API asynchronously.  it is very lightweight and has hardly an impact if you are calling asynchronously.

 

static void CallRestAPISample(Args _args)
{
 System.Net.WebHeaderCollection headers ;
 System.Net.WebClient webclient;
 System.Uri uri;
 
 // Create XML msg or string message as parameter to call the Rest API. 
 str msgstring = '<?xml version="1.0"?> ';
 
 msgstring += '<LogRequest>' ;
 
 msgstring += '<ApplicationName>AX Error Tracking</ApplicationName>';
 msgstring += ' <Date>06-22-2017 12:58:22 PM</Date>';
 msgstring += '<ThreadID>0</ThreadID>';
 msgstring += '<LogLevel> <INFO>false</INFO> <DEBUG>false</DEBUG> <ERROR>true</ERROR> <FATAL>false</FATAL> <WARN>false</WARN> </LogLevel>';
 msgstring += '<LogDetails> <ExceptionMessage>Test Exception</ExceptionMessage> <Message>This is test message from AX Logging Call.</Message> </LogDetails> ';
 msgstring += '<Metadata> <Data> <Name>ENV</Name> <Value>DEV</Value> </Data> <Data> <Name>Server</Name> <Value>AX AOS 1</Value> </Data> <Data> <Name>Source</Name> <Value>DAX</Value> </Data> </Metadata>';
 msgstring += '<Email> <From>FromAddr@Fromserver.com</From> <To>toaddrr@toserver.com</To> <Subject>AX Logging Test</Subject> <Message>This is test message</Message> <HasAttachments>false</HasAttachments> <IsBodyHtml>true</IsBodyHtml> <IncludeLogDetails>true</IncludeLogDetails> </Email>';
 msgstring += '</LogRequest>';
 
 
 info(strFmt("%1",time2str(timeNow(),1,1)));
 // initilizing host URI
 uri = new System.Uri("http://LogginServiceHost/api/Log/LogRequest");// FAKE SERVICE HOST ADDRESS
 // initializing webClient
 webclient = new System.Net.WebClient();
 headers = webclient.get_Headers();
 // Set header content type
 Headers.Add(System.Net.HttpRequestHeader::ContentType, "text/xml;charset=utf-8");
 // call API asynchronously
 webclient.UploadStringAsync(uri,msgstring);
 
 info(strFmt("%1",time2str(timeNow(),1,1)));

}

Standard
AX 2009, AX 2012, Development, dialog, Dynamics AX, User parameters, without dialogable class, without runbasebatch

Quick Dialog to take user entered parameters without dilaogable class creation

Following job uses dialog class to take parameters from user and run the job

static void UserParamSample(Args _args)
{

wmslocationID     wmslocationID;

str                           palletstoRAF;

dialog          dlg = new dialog();

 dialogField     dlgPalletIDs,dlgwmslocation;

 dlgPalletIDs = dlg.addField(typeid(ItemFreeTxt),”Container number”,”Enter pallet Ids seperated by commas”);
dlgwmslocation = dlg.addField(typeid(wmslocationid),”Location”);

if(!dlg.run())

return;

else

{

palletstoRAF = dlgPalletIDs.value();

wmslocationid = dlgwmslocation.value();

//Run the Job

  }

}

Standard
AX 2012, Development

Dynamics AX 2012 connect to Visual studio online , Team Foundation Server

With Visual Studio Online, Now you can have your project data on cloud and accessible anywhere. if you are a MSDN subcriber then you can start on VS online today by clicking here. Else you start a trial here .

How to connect to visual Studio online :

With a simplistic scenario when no Branching is setup below are the steps to follow.

1. Create a Team project in your VS online

At the home page of your VS online create new team project by choosing a suitable process template.

Create a Team Project

Create a Team Project

2. Add members to the project using manage members

Add members

3.  Go to AX client and configure TFS as version control for AX

Enable Version control in AX.

Enable TFS in AX.

Enable TFS in AX.

Setup VS online site , make sure that you follow the same pattern with port number etc. else it doesn’t get connected.

Setup TFS online

Setup TFS online

Standard
AX 2009, AX 2012, Development, Dynamics AX, Uncategorized

How to send a file to the printer in dynamics AX

How to send a file to the printer in dynamics AX

Following Job demonstrates on how to send a file from the file system to the locally connected printer( printer physically can be network or local but it should be added to the client and its name should be visible in the control panel–> devices and printers)

static void sendToPrinter(Args _args)
{
PrintJobSettings printJobSettings = new PrintJobSettings();
Dialog dialog = new Dialog();
DialogField dialogFileName;
str CoreLabelExe;
str CoreLabelParm;
;
// initiallize printer with the printer name. This initiallization automatically
// gets dll file details and port details.
printJobSettings = new PrintJobSettings();
printJobSettings.SetTarget(PrintMedium::Printer);
printJobSettings.deviceName(‘Microsoft XPS Document Writer’);
dialogFilename = dialog.addField(typeid(FilenameOpen));
if (dialog.run())
{
// finding executable DLL
CoreLabelExe = WinAPI::findExecutable(dialogFileName.value());
// Create Shell command parameter
CoreLabelParm = strFmt(‘ /PT “%1” “%2” “%3” “%4″‘,
dialogFileName.value(),
printJobSettings.printerPrinterName(),
printJobSettings.printerDriverName(),
printJobSettings.printerPortName());
// run dll on the shel with parameter
winAPI::shellExecute(CoreLabelExe, CoreLabelParm);
}
}

Standard
AX 2012, Development, Dynamics AX

Creating File Using Text I/O class

you can use Text I/O class to create a flat file in file system. TextIO supports Unicode characters.

code Class_LXKFormattedTextFile (1).

Download above class xpo and then change the file extension from “.KEY” to “.XPO”

Class Declaration

Class Declaration

Init File

Init File

Run

Run

init Device

init Device

Format Text

Format Text

Create Data

Create Data

Can create file

Can create file

Creating File Using Text I/O class

Aside
AX 2012, Development, Dynamics AX, Uncategorized

Reserve quantity by X++ code

Quick code snippet to reserve the quantity by X++

SalesLine locSalesline;

InventMovement inventmovement;

InventUpd_Reservation reservation;

inventMovement = InventMovement::constructNoThrow(locSalesline,InventMovSubType::None);

reservation = InventUpd_Reservation::newinventdim(inventMovement, InventDim::find(InventDimReserveFrom),-1*inventqty, false);

reservation.updateNow();

Standard
AX 2012, Development, Dynamics AX

How to Save SysLastValues on the form

you may find some instances where you want to save some additional data on the form w.r.to a user. Following example shows how you can implement this coding pattern to save last values on the form.

For any specific Implementation related queries shoot your questions at lokesh_kant@hotmail.com

  1. Identify the list of variables that you want to save with user’s Usage data.
  2. Implement following code
    public class FormRun extends ObjectRun{

    str x;

    #define.CurrentVersion(1)

    #localmacro.CurrentList

    x

    #endmacro

    }

    public void initParmDefault(){

    if(!x)

    x= “DEFAULT”;

    }

    public dataAreaId lastValueDataAreaId(){

    return curext();

    }

    public userId lastValueUserId(){

    return curuserid();

    }

    public UtilElementType lastValueType(){

    return UtilElementType::Form;

    }

    public identifiername lastValueElementName(){return this.name();}
    public identifiername lastValueDesignName(){return element.args().menuItemName();}
    public boolean unpack(container packedClass){int version = RunBase::getVersion(packedClass);;switch (version){case #CurrentVersion:[version,#CurrentList] = packedClass;

    return true;

    default:

    return false;

    }

    return false;

    }

    public void init(){super();// xctrl.text(“DEFAULT”);xSysLastValue::getLast(element);}
    public void run(){super();if(x)xctrl.text(x);// sxctrl is a string control on the form.}
    public container pack(){

    return [#CurrentVersion,#CurrentList];

    }

    public void close(){

    super();

    x= xctrl.text();

    xSysLastValue::saveLast(element);

    }

Standard