Consuming Dynamics AX 365FO Data entities using Web API (Part 3)

Hi Guys,

This is with the 3rd and final part of same demo, In this post we will see what is the code we need to write on wep api project to get the data from Dynamics ax 365. In this post we will be reading the purchase order data from Dynamics ax 365. In case if you have not read the previous two parts then follow these links

https://msdax.wordpress.com/2018/03/20/consuming-dynamics-ax-365fo-data-entities-using-web-api-part-1/

https://msdax.wordpress.com/2018/03/22/consuming-dynamics-ax-365fo-data-entities-using-web-api-part-2/

To start with, we need to add few dll references on Web api project. Please make sure Microsoft.OData.Client dll should be of same version what we have referenced in AuthenticationUtility and ODataUnity project(These projects are previously discussed in last blogs).

using Manage Nuget Package option we can install package for Microsoft.OData.Client

Now with the simple coding part, we need to reference following dll.

I have created this initConfiguration method to authenticate my call using Azure, GetAuthenticationHeader method is given on OAuthHelper class, we will pass true for web api projects.

Then i have created this fillOrder method to fill the list based on the query i have read from Dynamics ax 365 for purchase orders.

This is the main web api method for reading the purchase orders, i am reading top 50 for now.

After all this simple coding and all the configuration effort, when i click run on the project, i got this cool browser with my results of orders.

This is the result after all the effort,

Dynamics AX 365 insert not allowed for field ‘XXXX'”,”type”:”System.InvalidOperationException”

I was working on creating the new Data entity related to Case Details on Dynamics ax 365, The creation went well and simple. when i tried to consume the new Data entity from odata client using c#, i got this exception.

An error occurred while processing this request.{
“error”:{
“code”:””,”message”:”An error has occurred.”,”innererror”:{
“message”:”insert not allowed for field ‘CaseId'”,”type”:”System.InvalidOp
erationException”,”stacktrace”:” at Microsoft.Dynamics.Platform.Integration.Se
rvices.OData.Update.UpdateProcessor.CreateEntity_Save(ChangeOperationContext con
text, ChangeInfo changeInfo)\r\n at Microsoft.Dynamics.Platform.Integration.Se
rvices.OData.Update.ChangeInfo.ExecuteActionsInCompanyContext(IEnumerable`1 acti
onList, ChangeOperationContext operationContext)\r\n at Microsoft.Dynamics.Pla
tform.Integration.Services.OData.Update.ChangeInfo.TrySave(ChangeOperationContex
t operationContext)\r\n at Microsoft.Dynamics.Platform.Integration.Services.OD
ata.Update.UpdateManager.SaveChanges()\r\n at Microsoft.Dynamics.Platform.Inte
gration.Services.OData.AxODataBatchHandler.<ExecuteRequestMessagesAsync>d__10.Mo
veNext()”
}
}
}

This error is related to CaseId field on Case Details, after enough investigation i found that validatefield method from corresponding table is being fired and not allowing manual insertion on CaseId field. As this field is Number sequence bound. But our requirement was to create the case using custom value provided from Odata client using c#.

After enough research i got the clue to skip the table level validation from data entity. So i have decided to modify the data entity and override following methods and skip the table level validation for the CaseId field.

Thanks

Amir- Happy daxing 🙂

Dynamics ax 2012 client in specific language

1. Open command prompt.
2. Type Ax32.exe –language=culture_id

Example : Ax32.exe –language=en-us

Solving ODBC Connection error in AX 2012 Microsoft ODBC SQL Server Driver SQL Server Changed language setting to us_english

I was working with ODBC and i found this error, On searching i have found this article.

Cannot execute the required database operation.
The operation failed.
[Microsoft][SQL Server Native Client 10.0][SQL Server]Changed language setting to us_english.

Searching on the Internet I found the following post in Microsoft Support:

http://support.microsoft.com/kb/197459

However, after read that I thought that my problem should be due to my code, so i have decided to take a fresh and closer look on code and i found

subscriptionStartDate = resultSet.getDate(4);
subscriptionEndDate =resultSet.getDate(4);

So, in conclusion: I made a stupid mistake, This error is shown if you try to get an index that you previously obtained before.