Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Similar web api quey returning different results

Posted on by Microsoft Employee

Hi,

I am new to web api in CRM using D365 8.2 version

I have 2 webapi requests which are similar in terms of entity relationships, query header but one returns parent lookup text and other just the ID of the lookup.

Query1: new_fundraiser is the parent with _divisionid_value the look up that's returning both lookup ID and text value

------------------------------------------------------------------------------------------------------------------------------------

var serverURL=Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_fundraisings("+thisGUID+")?$select=new_cardnameholder1,_new_fundraisingevent_value&$expand=new_FundRaiser($select=contactid,fullname,_divisionid_value)";
req.open("GET", serverURL,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=\"*\"");

Jason result :

new_FundRaiser: {@odata.etag: "W/"3827216"", contactid: "11cc2f0a-9267-e711-80db-005056919b9d",
fullname: "Abbey Hakala FMPA", _upbeat_divisionid_value@OData.Community.Display.V1.FormattedValue: "NSW", _upbeat_divisionid_value@Microsoft.Dynamics.CRM.associatednavigationproperty: "upbeat_DivisionId", …}

Query2: new_FundName is the parent with _new_fundnamedivision_value the look up returning only the ID :(

-------------------------------------------------------------------------------------------------------------------------------

var serverURL= Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_fundraisingcontactses("+thisGUID
+")?$select=_new_contact_value,new_fundraisingcontactsid&$expand=new_FundName($select=new_cardnameholder1,_new_fundnamedivision_value)";
debugger;
req.open("GET",serverURL, 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=\"*\"");

Jason result:

new_FundName:
@odata.etag: "W/"3973444""
new_cardnameholder1: null
new_fundraisingid: "da3c0ae0-8b57-e911-811a-005056919b9d"
_new_fundnamedivision_value: "86d7df92-8767-e711-80db-005056919b9d"

 

Any help will be much appreciated.

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Similar web api quey returning different results

    When I compare the 2 query result json, I see there is line 'proto object' in the issue query, does that means something?

    Working Query JSON result:

    @odata.context: "$metadata#new_fundraisings(new_cardnameholder1,_new_fundraisingevent_value,new_FundRaiser,

    new_FundRaiser(contactid,fullname,_upbeat_divisionid_value))/$entity"

    @odata.etag: "W/"3973582""

    new_FundRaiser: {@odata.etag: "W/"3827216"", contactid: "11cc2f0a-9267-e711-80db-005056919b9d",

    fullname: "Abbey Hakala FMPA", _divisionid_value@OData.Community.Display.V1.FormattedValue: "NSW", _divisionid_value@Microsoft.Dynamics.CRM.associatednavigationproperty: "_DivisionId", …}

    new_cardnameholder1: "Sarah1"

    new_fundraisingid: "a42d6838-3152-e911-811a-005056919b9d"

    _new_fundraisingevent_value: "75a6835e-3052-e911-811a-005056919b9d"

    _new_fundraisingevent_value@Microsoft.Dynamics.CRM.associatednavigationproperty: "new_FundRaisingEvent"

    _new_fundraisingevent_value@Microsoft.Dynamics.CRM.lookuplogicalname: "x_event"

    _new_fundraisingevent_value@OData.Community.Display.V1.FormattedValue: "EV9000168 - Celebrating Birthday CB1"

    __proto__: Object

    Issue Query JSON result:

    @odata.context: "$metadata#new_fundraisingcontactses(_new_contact_value,new_fundraisingcontactsid,new_FundName,new_FundName(new_cardnameholder1,_new_fundnamedivision_value))/$entity"

    @odata.etag: "W/"3970248""

    new_FundName:

    @odata.etag: "W/"3973444""

    new_cardnameholder1: null

    new_fundraisingid: "da3c0ae0-8b57-e911-811a-005056919b9d"

    _new_fundnamedivision_value: "86d7df92-8767-e711-80db-005056919b9d"

    __proto__: Object

    new_fundraisingcontactsid: "4c649f4b-aa57-e911-811a-005056919b9d"

    _new_contact_value: "21cc2f0a-9267-e711-80db-005056919b9d"

    __proto__: Object

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Similar web api quey returning different results

    Hi,

    As expected and as in above JSON result, new_contact_division returns 86d7df92-8767-e711-80db-005056919b9d

    Putting the URL , it return the below:

    {

     "@odata.context":"https://myURL/api/data/v8.2/$metadata#new_fundraisingcontactses(new_fundraisingcontactsid,new_FundName,new_FundName(_new_fundnamedivision_value))/$entity","@odata.etag":"W/\"3970141\"","new_fundraisingcontactsid":"943bdc4f-9557-e911-811a-005056919b9d","new_FundName":{

       "@odata.etag":"W/\"3976047\"","_new_fundnamedivision_value":"93efc247-8967-e711-80db-005056919b9d","new_fundraisingid":"c69c3e95-8957-e911-811a-005056919b9d"

     }

    }

  • crm development Profile Picture
    crm development 870 on at
    RE: Similar web api quey returning different results

    Hi, try only below code. Also put debugger and check what value you are getting for same. In CRM Rest Builder, copy code generated Url (last tab after Code (Editor)) and check if you are getting any error in browser.

    var new_contact_division = result["new_FundName"]["_new_fundnamedivision_value"];

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Similar web api quey returning different results

    Hi Anand,

    var req = new XMLHttpRequest();

    var serverURL= Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_fundraisingcontactses("+thisGUID

    +")?$select=_new_contact_value,new_fundraisingcontactsid&$expand=new_FundName($select=new_cardnameholder1,_new_fundnamedivision_value)";

    alert(serverURL);

    debugger;

    req.open("GET",serverURL, 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);            

    debugger;

               var new_fundraisingid = result["new_fundraisingcontactsid"];          

    if (result.hasOwnProperty("new_FundName")) {      

     // var new_contact_division = result["new_FundName"]["_new_fundnamedivision_value@OData.Community.Display.V1.FormattedValue"];              

       var new_contact_division = result.value[0]["new_FundName"]["_new_fundnamedivision_value@OData.Community.Display.V1.FormattedValue"];  alert("Contact division ID "+new_contact_division);

    }

    The JSON result has this info:

    new_FundName:

    @odata.etag: "W/"3973444""

    new_cardnameholder1: null

    new_fundraisingid: "da3c0ae0-8b57-e911-811a-005056919b9d"

    _new_fundnamedivision_value: "86d7df92-8767-e711-80db-005056919b9d"

    Thanks for your time again

  • crm development Profile Picture
    crm development 870 on at
    RE: Similar web api quey returning different results

    Hi, Is lookup record name present? Can you copy your code here.

    Thanks,

    Anand

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Similar web api quey returning different results

    Hi Anand,

    Thanks for your response.

    I have 'Formatted values' to Yes in CRM REST Builder.

    The statement var _new_fundnamedivision_value = results.value[0]["_new_fundnamedivision_value@OData.Community.Display.V1.FormattedValue"]; is not working :(

    And I do use formatted values in assignment:

    var new_contact_division = result["new_FundName"]["_new_fundnamedivision_value@OData.Community.Display.V1.FormattedValue"];

    As shown in my question, for some reason the Json result itself didn't has the look up value info in it for query2 but Query1.

  • Suggested answer
    crm development Profile Picture
    crm development 870 on at
    RE: Similar web api quey returning different results

    Hi, have you tried below line of code in your second code? You need to retrieve Formatted Values from the result.

    Also if you have generated web api using "CRM REST Builder", hope you have selected 'Formatted Values' as Yes.

    var _new_fundnamedivision_value = results.value[i]["_new_fundnamedivision_value@OData.Community.Display.V1.FormattedValue"];

    Thanks,

    Anand

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,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans