AX 2012 Call Restful API using basic authentication X++ Dynamics AX 2012

Below code helps you to call the Restful API using basic authentication method of adding Authorization header using HTTP Post method.

 static void ConsumingRestService(Args _args)  
 {  
   str destinationUrl = 'Json Url here', requestXml, responseXml;  
   System.Net.HttpWebRequest    request;  
   System.Net.HttpWebResponse   response;  
   CLRObject            clrObj;  
   System.Byte[]          bytes;  
   System.Text.Encoding      utf8;  
   System.IO.Stream        requestStream, responseStream;  
   System.IO.StreamReader     streamReader;  
   System.Exception        ex;  
   System.Net.WebHeaderCollection httpHeader;  
   str               byteStr;  
   System.Byte[]          byteArray;  
   System.IO.Stream        stream;  
   System.IO.Stream        dataStream;  
   byteStr = strfmt('%1:%2', "USERNAME", "PASSWORD");  
   requestXml = " { \"storeId\": 25001, \"terminalId\":\"012\", \"transactionSequenceNumber\":\"031949640279\", \"lookup\" : { \"nameSearch\" : { \"lastName\" : \"Jones\",\"zipCode\" : \"214034702\" } } }";  
   try  
   {  
     new InteropPermission(InteropKind::ClrInterop).assert();  
     httpHeader = new System.Net.WebHeaderCollection();  
     clrObj = System.Net.WebRequest::Create(destinationUrl);  
     request = clrObj;  
     utf8 = System.Text.Encoding::get_UTF8();  
     bytes = utf8.GetBytes(requestXml);  
     request.set_KeepAlive(true);  
     request.set_ContentType("application/xml");  
     utf8    = System.Text.Encoding::get_UTF8();  
     byteArray  = utf8.GetBytes(byteStr);  
     byteStr   = System.Convert::ToBase64String(byteArray);  
     httpHeader.Add("Authorization", 'Basic ' + byteStr);  
     request.set_ContentType("text/xml; encoding='utf-8'");  
     request.set_ContentLength(bytes.get_Length());  
     request.set_Method("POST");  
     request.set_Headers(httpHeader);  
     requestStream = request.GetRequestStream();  
     requestStream.Write(bytes, 0, bytes.get_Length());  
     response = request.GetResponse();  
     responseStream = response.GetResponseStream();  
     streamReader = new System.IO.StreamReader(responseStream);  
     responseXml = streamReader.ReadToEnd();  
     info(responseXml);  
   }  
   catch (Exception::CLRError)  
   {  
     //bp deviation documented  
     ex = CLRInterop::getLastException().GetBaseException();  
     error(ex.get_Message());  
   }  
   requestStream.Close();  
   streamReader.Close();  
   responseStream.Close();  
   response.Close();  
 }  

Thanks for reading the blog
Happy daxing 🙂

Dynamics ax 2012 : AxBuild.exe xppcompileall stuck on 3 asterisks

Recently with one of the customer i faced this issue, Each time the compile starts, it just shows the AxBuild prompt with 3 asterisks and the window closes after about 15-20 minutes (without actually doing anything).

Event i have run the command as Admin but nothing worked and faced same problem

axbuild.exe  xppcompileall  /s=01 /altbin=”C:\Program Files (x86)\Microsoft Dynamics AX\60\Client\Bin”

After enough research i came to know that this is possible in two scenarios. The first one is the permissions, ensure the user running axbuild.exe has the same permissions as the AOS Service account and account should have enough access on AX database also. For me this was the problem the account through which i am login to system does not have enough permission to AX database.

AxBuild.exe must be run by an account that has no less security authority than the account that runs the permanent AOS has.

Source: https://msdn.microsoft.com/en-us/library/dn528954.aspx

The second time, it happened when the SQL Server had no more RAM resources available. Then fix would be to clear memory for SQL service.

Thanks for reading

Happy daxing 🙂

Amir

 

Dynamics ax 2012 Id Conflicts

Hi Guys,

Recently i was experiencing the Upgrade and i have faced Id conflicts issue, This blog is about what i have experience and how i am able to rectify and identify the problem.

This content is re-blog of following two posts, through that i got the required info and fixed my issue.

http://dev.goshoom.net/en/2011/11/id-change/

https://yetanotherdynamicsaxblog.wordpress.com/category/sql/

So what is Conflicts ?

If a table ID or field ID is changed in Dynamics AX, data are lost during synchronization, because the table with the old ID (and containing data) is dropped at the beginning and then a new table is created with the same structure but a different ID. The same is valid analogically for table fields.

The data loss can be prevented in several ways (e.g. by data export and re-import), but one solution is really simple and painless. Realize how the situation looks in AX after the ID change (e.g. after installation of a layer with different IDs) but before the database synchronization:

Where Ids are stored in AX ?

You can identify all changed IDs by comparing values in AOT with values in SqlDictionary. And the update of SqlDictionary to the new ID can prevent the regeneration of database objects during synchronization.

ReleaseUpdateDB::changeTableId method is used to updated the SQLDictionary table it takes new and old table id and table name to update the record.

 //Updates table ID in SqlDictionary  
       if (ReleaseUpdateDB::changeTableId(  
         sqlDictionaryTable.tabId,  
         dictTable.id(),  
         dictTable.name()))  
       {  
         info(strFmt("Table ID changed (%1 -> %2)", sqlDictionaryTable.tabId, dictTable.id()));  
       }  

Sometime there is also the need of updating the table id and reflect in the metadata.

There is a Table with name “ModelElement” in AX model database. In this table we have few important fields as, it is important for us to understand these fields.

1. ElementType : this field contains type  ID of data object like for table fields its value will be 42 and for tables it will be 44.

2. AxId : this field contains actual object ID value which create conflicts among environments. We have to change this field value, e-g for AccountNum field of CustTable it could be as 1.

3. ParentId : this field contains parent object ID, like ID of the table, whose field we are looking at, e-g for AccountNum field of CustTable it could be as 77, which is table ID for CustTable.

I have build this query to check the table ids using sql.

 // this query can be used to read the current table id from model db  
 SELECT ELEMENTHANDLE, ROOTHANDLE, NAME, AXID, ORIGIN FROM MODELELEMENT   
 JOIN ELEMENTTYPES ON MODELELEMENT.ELEMENTTYPE = ELEMENTTYPES.ELEMENTTYPE  
 WHERE NAME IN ('legCustTableLegacy', 'legACCCreditLimitLevelLine', 'legAccountsParameterSetup', 'legAXEnums') AND ELEMENTTYPENAME IN ('TABLE')  
 
//This query can update the table id and reflect in meta data.  
 Update M  
 SET m.AxId = 105871  
 FROM MODELELEMENT as M  
 JOIN ELEMENTTYPES ON M.ELEMENTTYPE = ELEMENTTYPES.ELEMENTTYPE  
 WHERE NAME IN ('legACCCreditLimitLevelLine') AND ELEMENTTYPENAME IN ('TABLE')  

Thanks for reading this

Happy Daxing 🙂

%d bloggers like this: