Hi all,
I am working on a requirement in AX 7 where I have to update few tables using OData services. I have created the entity for the particular table and made them public so that we can access the entities from the 3rd party applications. I have written wrapper service classes in C# to access these entities. I have been able to successful make read and insert. Now I have a requirement where I have to update certain records. It is throwing me an exception for update.
Sample code:
public string Save(List<FeedbackQuestion> feedbackList, string comment, string DataAreaId)
{
string flag = string.Empty;
string appointmentNo = feedbackList[0].AppointmentNo;
dataAreaId = string.IsNullOrEmpty(DataAreaId) ? dataAreaId : DataAreaId;
try
{
var query = from entity in _context.abc //"abc" is the entity from AX
where entity.AppointmentNo == appointmentNo && entity.DataAreaId == dataAreaId
select entity;
if (query.Count() > 0)
{
var feedbackQuestions = query.ToList();
foreach (var ap in feedbackQuestions)
{
foreach (var question in feedbackList)
{
if (question.Question.ToString() == ap.Question.ToString())
{
ap.AppointmentNo = appointmentNo;
ap.DataAreaId = dataAreaId;
ap.Answer = question.Answer;
_context.UpdateObject(ap);
_context.SaveChanges();
}
}
}
flag = "success";
}
}
catch (Exception ex)
{
flag = ex.ToString() + " " + ex.InnerException.ToString();
}
return flag;
}
Error message:
flag "Microsoft.OData.Client.DataServiceRequestException: An error occurred while processing this request. ---> Microsoft.OData.Client.DataServiceClientException: <!DOCTYPE html>\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n<head>\r\n <title>Microsoft Dynamics AX</title>\r\n\t<link id=\"Theme_30_0\" href=\"/Resources/themes/30/0/core.min-7.0.4030.16079.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n <script>\r\n function goDashBoard() {\r\n window.location = \"/\"\r\n }\r\n </script> \r\n <style>\r\n \r\n html, body {\r\n min-height: 100%; /*Sets the min height to the height of the viewport.*/\r\n width: auto;\r\n height: 100%; \r\n }\r\n\r\n html>body, html>body .errorPage-wrapper {\r\n height: auto; /*this undoes the IE hack, hiding it\r\n from IE using the child selector*/\r\n }\r\n\r\n body {\r\n margin: 0;\r\n overflow:auto;\r\n }\r\n\r\n </style> \r\n</head>\r\n<body>\r\n\t<div class=\"errorPage-wrapper\">\r\n <div class=\"errorPage\">\r\n <div id=\"topic\" class=\"titleField\">There is a problem with the server</div> \r\n <div id=\"infomation\" class=\"mainInstruction\">Sorry, the server has encountered an error. It is either not available or it can't respond at this time. Please contact your system administrator.</div> \r\n </div>\r\n <div id=\"errorPage-footer\" class=\"errorPage-footer\">500 Error</div>\r\n\t</div>\r\n</body>\r\n\r\n</html>\r\n\r\n\r\n --- End of inner exception stack trace ---\r\n at Microsoft.OData.Client.SaveResult.HandleResponse()\r\n at Microsoft.OData.Client.BaseSaveResult.EndRequest()\r\n at Microsoft.OData.Client.DataServiceContext.SaveChanges(SaveChangesOptions options)\r\n at Microsoft.OData.Client.DataServiceContext.SaveChanges()\r\n at DW.DAL.DynamicAX.AXCustomerRepository.SubmitFeedback(List`1 feedbackList, String comment, String DataAreaId) in C:\\Users\\axlocaladmin\\Documents\\Visual Studio 2015\\Projects\\Mobility\\MyCarCare\\DW.Dynamix.DAL\\AXCustomerRepository.cs:line 306 Microsoft.OData.Client.DataServiceClientException: <!DOCTYPE html>\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n<head>\r\n <title>Microsoft Dynamics AX</title>\r\n\t<link id=\"Theme_30_0\" href=\"/Resources/themes/30/0/core.min-7.0.4030.16079.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n <script>\r\n function goDashBoard() {\r\n window.location = \"/\"\r\n }\r\n </script> \r\n <style>\r\n \r\n html, body {\r\n min-height: 100%; /*Sets the min height to the height of the viewport.*/\r\n width: auto;\r\n height: 100%; \r\n }\r\n\r\n html>body, html>body .errorPage-wrapper {\r\n height: auto; /*this undoes the IE hack, hiding it\r\n from IE using the child selector*/\r\n }\r\n\r\n body {\r\n margin: 0;\r\n overflow:auto;\r\n }\r\n\r\n </style> \r\n</head>\r\n<body>\r\n\t<div class=\"errorPage-wrapper\">\r\n <div class=\"errorPage\">\r\n <div id=\"topic\" class=\"titleField\">There is a problem with the server</div> \r\n <div id=\"infomation\" class=\"mainInstruction\">Sorry, the server has encountered an error. It is either not available or it can't respond at this time. Please contact your system administrator.</div> \r\n </div>\r\n <div id=\"errorPage-footer\" class=\"errorPage-footer\">500 Error</div>\r\n\t</div>\r\n</body>\r\n\r\n</html>\r\n\r\n" string
*This post is locked for comments
UPDATE: OK I found the solution. The problem was that the EntityKey contained Enum type field...Although AX 7's OData handles enums as a string in the content at retrive and create operations, in the header you must give them in the following format: Microsoft.Dynamics.DataEntities.NoYes'Yes'
Hi,
Please catch the request message with Fiddler, and copy here. I guess this exception was in the respond msg, request message will tell us what you were exactly doing.
@Martin I need your help. I have been working with CRM/AX 7 OData for couple of months. First of all I was wondering why there is not a MS documentation for AX 7 OData (like for CRM), but never mind, I almost figured out how it exactly works. I still have some opening questions, one of them is about identifying the record for UPDATE or DELETE. I thought that the field(s) in EntityKey will give a record obviously, it gives, BUT NOT AT THE CUSTOM ENTITES. So my questions is do you know any properties on the Entity which I have to set for using EntiyKey through OData? I mean: How can I set X field on the Entity to identify a record obviously like: data/MyEntity('x_value')? It seems not enough to create EntityKey, drag n drop fields in it, and set the key on the Entity's PrimaryKey property.
Thanks,
Balint
First of all, I'm surprised that you call SaveChanges() for every single updated object; it clearly makes much more requests in than updating all objects first and saving them at once. Try to fix this and try it again.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,134 Super User 2024 Season 2
Martin Dráb 229,928 Most Valuable Professional
nmaenpaa 101,156