Skip to main content

Notifications

Microsoft Dynamics AX (Archived)

Updating in Dynamics AX 7 using OData services

Posted on by 185

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Updating in Dynamics AX 7 using OData services

    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'

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Updating in Dynamics AX 7 using OData services

    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

  • Martin Dráb Profile Picture
    Martin Dráb 229,928 Most Valuable Professional on at
    RE: Updating in Dynamics AX 7 using OData services

    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.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,134 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,928 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans