Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Change Booking Status through custom Button crm field service

Posted on by 361

I am trying to create a custom button to change system status in bookable ressource booking entity from work order command bar but it return error

function updateAccount() {
var results =null;
var recordId = Xrm.Page.data.entity.getId();

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/msdyn_workorders("+recordId+")?$expand=msdyn_msdyn_workorder_bookableresourcebooking_WorkOrder($select=bookableresourcebookingid)", false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var result = JSON.parse(this.response);
            var msdyn_workorderid = result["msdyn_workorderid"];
            for (var a = 0; a < result.msdyn_msdyn_workorder_bookableresourcebooking_WorkOrder.length; a++) {
                var msdyn_msdyn_workorder_bookableresourcebooking_WorkOrder_bookableresourcebookingid = result.msdyn_msdyn_workorder_bookableresourcebooking_WorkOrder[a]["bookableresourcebookingid"];
var entity = {};
                                entity["BookingStatus@odata.bind"] = "/bookingstatuses(C33410B9-1ABE-4631-B4E9-6E4A1113AF34)";

                                $.ajax({
                                    type: "PATCH",
                                    contentType: "application/json; charset=utf-8",
                                    datatype: "json",
                                    url: Xrm.Page.context.getClientUrl() + "/api/data/v8.2/bookableresourcebookings("+msdyn_msdyn_workorder_bookableresourcebooking_WorkOrder_bookableresourcebookingid+")",
                                    data: JSON.stringify(entity),
                                    beforeSend: function(XMLHttpRequest) {
                                        XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
                                        XMLHttpRequest.setRequestHeader("OData-Version", "4.0");
                                        XMLHttpRequest.setRequestHeader("Accept", "application/json");
                                    },
                                    async: true,
                                    success: function(data, textStatus, xhr) {
                                        //Success - No Return Data - Do Something
                                    },
                                    error: function(xhr, textStatus, errorThrown) {
                                        Xrm.Utility.alertDialog(textStatus + " " + errorThrown);
                                    }
                                });


            }
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();

Xrm.Page.getAttribute("msdyn_systemstatus").setValue(690970003);
}
{"error":{"code":"","message":"Bad Request - Error in query syntax.","innererror":{"message":"Bad Request - Error in query syntax.","type":"Microsoft.OData.ODataException","stacktrace":"   at Microsoft.OData.UriParser.SegmentKeyHandler.TryCreateKeySegmentFromParentheses(ODataPathSegment previous, KeySegment previousKeySegment, String parenthesisExpression, ODataUriResolver resolver, ODataPathSegment& keySegment, Boolean enableUriTemplateParsing)\r\n   at Microsoft.OData.UriParser.ODataPathParser.TryBindKeyFromParentheses(String parenthesesSection)\r\n   at Microsoft.OData.UriParser.ODataPathParser.TryCreateSegmentForNavigationSource(String identifier, String parenthesisExpression)\r\n   at Microsoft.OData.UriParser.ODataPathParser.CreateFirstSegment(String segmentText)\r\n   at Microsoft.OData.UriParser.ODataPathParser.ParsePath(ICollection`1 segments)\r\n   at Microsoft.OData.UriParser.ODataPathFactory.BindPath(ICollection`1 segments, ODataUriParserConfiguration configuration)\r\n   at Microsoft.OData.UriParser.ODataUriParser.Initialize()\r\n   at Microsoft.OData.UriParser.ODataUriParser.ParsePath()\r\n   at System.Web.OData.Routing.DefaultODataPathHandler.Parse(String serviceRoot, String odataPath, IServiceProvider requestContainer, Boolean template)\r\n   at System.Web.OData.Routing.DefaultODataPathHandler.Parse(String serviceRoot, String odataPath, IServiceProvider requestContainer)\r\n   at Microsoft.Crm.Extensibility.OData.CrmODataPathHandler.Parse(String serviceRoot, String odataPath, IServiceProvider requestContainer)"}}}


*This post is locked for comments

  • Verified answer
    Anas Rafik Profile Picture
    Anas Rafik 361 on at
    RE: Change Booking Status through custom Button crm field service

    Isolved the problem by replacing "this.readyState" "this.status"

    "this.response" with "req.readyState" "req.status"

    "req.response"

    then i declared the variables "result" and "msdyn_workorderid" where i want to put my response outside the request

  • Thomas David Dayman Profile Picture
    Thomas David Dayman 11,323 on at
    RE: Change Booking Status through custom Button crm field service

    I tested it and below is a screenshot:

    6837.Screenshot_5F00_2.jpg

    It looks like that the readyState is not changing to 4 which is needed to go into the if statement. Maybe try and trim the braces off the recordId.

    Check out this post as I think he could have the same issue as you?

    https://community.dynamics.com/crm/f/117/p/264850/747010#747010

  • Anas Rafik Profile Picture
    Anas Rafik 361 on at
    RE: Change Booking Status through custom Button crm field service

    thank  you so much

  • Thomas David Dayman Profile Picture
    Thomas David Dayman 11,323 on at
    RE: Change Booking Status through custom Button crm field service

    My Queries do the same thing and they work.

    I will test out your code soon and see what is wrong.

  • Anas Rafik Profile Picture
    Anas Rafik 361 on at
    RE: Change Booking Status through custom Button crm field service

    the problem is that i retrieve the guid of the record when i try to insert it in my odata query is take it as string not guid always put it between two quotations ""

  • Thomas David Dayman Profile Picture
    Thomas David Dayman 11,323 on at
    RE: Change Booking Status through custom Button crm field service

    Sorry its - debugger;

    not debugger();

  • Anas Rafik Profile Picture
    Anas Rafik 361 on at
    RE: Change Booking Status through custom Button crm field service

    yes i used the CRM REST Builder v2.5.0.0 tool to build my request and tested them but i debugged my code using onther trick because placing debugger(); statement doesn't work

  • Suggested answer
    Thomas David Dayman Profile Picture
    Thomas David Dayman 11,323 on at
    RE: Change Booking Status through custom Button crm field service

    The error message has shown Bad Request - Error in query syntax.

    Have you tested your query to see if it works using the CRM REST BUILDER? github.com/.../releases

    Also have you debugged your code using F12 and placing a debugger(); statement at the top of your function?

  • Anas Rafik Profile Picture
    Anas Rafik 361 on at
    RE: Change Booking Status through custom crm field service

    {"error":{"code":"","message":"Bad Request - Error in query syntax.","innererror":{"message":"Bad Request - Error in query syntax.","type":"Microsoft.OData.ODataException","stacktrace":"   at Microsoft.OData.UriParser.SegmentKeyHandler.TryCreateKeySegmentFromParentheses(ODataPathSegment previous, KeySegment previousKeySegment, String parenthesisExpression, ODataUriResolver resolver, ODataPathSegment& keySegment, Boolean enableUriTemplateParsing)\r\n   at Microsoft.OData.UriParser.ODataPathParser.TryBindKeyFromParentheses(String parenthesesSection)\r\n   at Microsoft.OData.UriParser.ODataPathParser.TryCreateSegmentForNavigationSource(String identifier, String parenthesisExpression)\r\n   at Microsoft.OData.UriParser.ODataPathParser.CreateFirstSegment(String segmentText)\r\n   at Microsoft.OData.UriParser.ODataPathParser.ParsePath(ICollection`1 segments)\r\n   at Microsoft.OData.UriParser.ODataPathFactory.BindPath(ICollection`1 segments, ODataUriParserConfiguration configuration)\r\n   at Microsoft.OData.UriParser.ODataUriParser.Initialize()\r\n   at Microsoft.OData.UriParser.ODataUriParser.ParsePath()\r\n   at System.Web.OData.Routing.DefaultODataPathHandler.Parse(String serviceRoot, String odataPath, IServiceProvider requestContainer, Boolean template)\r\n   at System.Web.OData.Routing.DefaultODataPathHandler.Parse(String serviceRoot, String odataPath, IServiceProvider requestContainer)\r\n   at Microsoft.Crm.Extensibility.OData.CrmODataPathHandler.Parse(String serviceRoot, String odataPath, IServiceProvider requestContainer)"}}}

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!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans