Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

CRM Rest Builder, Javascript help

Posted on by Microsoft Employee

Hi, how do i put my CRM Rest query into a JavaScript function to return the field 'default role'?

So far I have the following, and the default role is a lookup to the 'Bookable Resource Category' entity.

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/msdyn_projectteammembersignups(AE55156F-0D32-E811-A972-000D3A1A7A9)?$expand=msdyn_BookableResource($select=rev_defaultrole)", true);
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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var result = JSON.parse(this.response);
            var msdyn_projectteammembersignupid = result["msdyn_projectteammembersignupid"];
            if (result.hasOwnProperty("msdyn_BookableResource")) {
                var msdyn_BookableResource_rev_defaultrole = result["msdyn_BookableResource"]["rev_defaultrole"];
            }
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();


  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CRM Rest Builder, Javascript help

    All sorted now thanks Ravi

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: CRM Rest Builder, Javascript help

    Hi Graeme,

    The error message in the screenshot is for some other web api call and not the one you have mentioned in the code. You can check the url used in the code is for bookable resource entity whereas in the screenshot, it is for projectteamset (which is old odata endpoints). I suspect that yopur code is working fine but some other code is failing. Try running the same code with hardcoded id on some other entity and see if the code works or not. This will help you isolate if there is some other code failing.

    In the code, I do see some extra spaces in the URL, not sure if this is just displaying here in the porat or actualy exists in your code. If it exists in code then it won't work.

    Hope this helps.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CRM Rest Builder, Javascript help

    Hi Ravi,

    So i have it setup as follows but get this error:

    3515.tempsnip.png

    Key:

    "msdyn_bookableresourceid" = the lookup on the team member record
    "Bookable Resources" = plural name of the above

    "rev_defaultrole" = the field I am trying to retrive

    "bookableresourcecategory" the entity of the lookup i am trying to popualte 
    "msdyn_resourcecategory" and the filed i am trying to put the default role field into.


    Code:

    function setCaseCompany() {
    
        var lookup = Xrm.Page.getAttribute("msdyn_bookableresourceid").getValue();  //you will get the id with exxtra double quotes or square brackets by doing get value hence you to make it readable by CRM , you must slice it. i have use the below method:
        var newid = lookup[0].id.slice(1, -1);  // you will get perfect id like “EDCJDKDJDKJDJDKJDJKD” here.
    
        var req = new XMLHttpRequest();
    
        //once you have the id, you have frame to make a webapi GET call by proving the newid we got.
    
        req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data / v8.1 / Bookable Resources(" + newid + ") ? $select = rev_defaultrole", true);
        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.setRequestHeader("Prefer", "odata.include - annotations=\"*\"");
        req.onreadystatechange = function () {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 200) {
                    var result = JSON.parse(this.response); // you will get the retrieved value in object we stored in result var.
    
                    var retrivedvalue = result.rev_defaultrole; //get the id of the field
                    var retrivedformatedvalue = result["rev_defaultrole@OData.Community.Display.V1.FormattedValue"]; //get the formatted name of the field
                    if (retrivedvalue != null) {
                        var value = new Array();
                        value[0] = new Object();
                        value[0].id = retrivedvalue;
                        value[0].name = retrivedformatedvalue;
                        value[0].entityType = "bookableresourcecategory";
    
                        Xrm.Page.getAttribute("msdyn_resourcecategory").setValue(value); //set the lookup value finally
                    }
                    else
                        alert("some textt!!!!!!") // optional
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send();
    }


  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: CRM Rest Builder, Javascript help

    Okay, then why are you using the expand. Why can't you simply select the bookable resource instead on selecting the project team member and then expanding to select the bookable resource and then selecting default role?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CRM Rest Builder, Javascript help

    Hi Ravi,

    So I'm going to Project new Project Team Member and while in the create state i'm selecting a bookable resource.

    It's this lookup bookable resource  i am using to then retrieve the defualt role custom lookup filed (which is a lookup to 'Bookable Resource Category') to then populate the role field on the Project Team Member form.

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: CRM Rest Builder, Javascript help

    Hi Greame,

    Could you please ahre some more info on your query. What exactly you are retrieving? Based on this it dpeneds how you got the lookup value in the response.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

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