All posts by Admin

How to call a web API from Dynamics 365 FO X++

A short example how to autenticate and talk to web based API from x++

The code below is divided into two pieces/methods just to ease how to do things. One that authenticates to an API and the other one takes the authentication token and adds it to the call. The examples below maybe don’t fit your scenario due to all API are different but the procedure is almost the same.

void authentication() 
str                rawResponse;
Map                responseData;
RetailWebResponse  response;
//URL to the API, Data, Header data, ContentType: 'application/x-www-form-urlencoded'
response = webApi.makePostRequest([URL], @'grant_type=client_credentials&client_id=[clientId]', [Header], [ContentType]);
if(response.parmHttpStatus() != 200)
throw error("Couldnt authenticate");

rawResponse = response.parmData();
responseData = RetailCommonWebAPI::getMapFromJsonString(rawResponse);
token = responseData.lookup("access_token");

In this example below, we got our response back as an ARRAY. we remove the ARRAY tags and with that, the call is translated into JSON.

private void call()
RetailWebRequest   webRequest;
RetailWebResponse  response;
str                rawResponse, ref;
Map                responseData;

webApi = RetailCommonWebAPI::construct();

//URL to get data
webRequest = RetailWebRequest::newUrl([URL]);
//Send token through JSON GET call
webRequest.parmHeader(@'Authorization: Bearer '+token);

response = webApi.getResponse(webRequest);

if(response.parmHttpStatus() != 200)
throw error("No data is returned");
rawResponse = substr(response.parmData(),2,(strlen(response.parmData())-2));
responseData = RetailCommonWebAPI::getMapFromJsonString(rawResponse);

ref        = responseData.lookup("ref");

Problem with str2enum

Just found a strange behaviour in using the standard str2enum function. Consider the following code ;

static void TestOfEnumsWithFirstValueBlank(Args _args)
    ProjCategoryType selectedType;
    str              s;
    selectedType = ProjCategoryType::None;
    s = enum2str(selectedType); // get enum string
    print s; // shows nothing, since None has no label.
    selectedType = str2enum(selectedType,s); // convert back
    if(selectedType == ProjCategoryType::None)
        print "'None' selected";
        print "undefined..."; // sadly this is printed..

This example code prints undefined, NOT ‘None’ selected. This because str2enum does not handle an empty string in a correct way, even if one of the values in the choosen enum has the emptry string as it’s label. This means values to str2enum must always be validated first before each call..

AIF – Wrong field ID in the key data container in entity key

@SYS92355 = “Wrong field ID in the key data container in entity key”

This error message from the AIF in AX4sp2 did give me some headache today..

Finally I solved it. I was about to export a SalesOrder with both header and lines, but I did put my sendElectronically method in the SalesLine table, *not* in SalesTable table.. So when executed I never got the correct keys. The query I used for the axd wizard was based on SalesTable first, and THEN SalesLine..

Might help someone else with the same or similar problem!

How to colour code different companies inside axapta

During development and test we often switch between different companies in our installation. To not mistaken us, so we always make changes in the correct one, we made this small change in the code. With different background-colours for each company, you never delete something importent by mistake. This example shows how it could be done.
Continue reading How to colour code different companies inside axapta

How to catch special keystrokes and use them as shortcuts

Rightclick on a form and go down tracking a specific field or other data, is a tedious work if you do it dozens of times per day.

We wanted to catch the key combination ctrl+Z (since it is rarelly used when running axapta), so we could use it to display common data about a form, field or other info. This is a code example we nowdays always install in all new test and development environments (not production, since it would give normal users unwanted access).
Continue reading How to catch special keystrokes and use them as shortcuts

How to clock routines which (should) go faster than 1 sec

From the basic routines in Axapta we only have the TimeNow() function which returns number of seconds since midnight. To measure something more exact than that, we either need to do an API call to getTickCount or to use the setTimeOut() function. This is a small demo on how to implement both such timers.
Continue reading How to clock routines which (should) go faster than 1 sec