web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Web api request

(0) ShareShare
ReportReport
Posted on by 222

Hi All,

I am using the following code to fetch the name of account for specific opportunity.

1.  function setAccount()

 2. {
3. debugger;
4. var opportintytId = Xrm.Page.getAttribute("new_opportunity").getValue()[0].id;
5. req.open("GET", Xrm.Page.context.getClientUrl() +"/api/data/v9.1/opportunities?$select=_customerid_value&$filter=opportunityid eq ("+opportintytId+")", true);

6. req.setRequestHeader("OData-MaxVersion", "4.0");
7. req.setRequestHeader("OData-Version", "4.0");
8. req.setRequestHeader("Accept", "application/json");
9. req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
10. req.onreadystatechange = function () {
11. if (this.readyState === 4) {
12. req.onreadystatechange = null;
13. if (this.status === 200) {
14. var result = JSON.parse(this.response);
15. var account = result["=_customerid_value"];
16. }
17. else {
18. alert(this.statusText);
20. }
21. }
22. };
23. }

The code is getting executed till line 9. But after line 10 it goes out of code and not getting executed at all.

I am new to web api and not able to figure out what else is needed. Please help.

Thanks 

Aparna

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Aparna,

    You need to pass GUID without bracs( {} ).

    var opportintytId = Xrm.Page.getAttribute("opportunity").getValue()[0].id; // Make sure you put correct field name ,  if its custom field should be with  publisher name  , or use  Xrm.Page.data.entity.getId() for same entity id
    opportintytId = opportintytId.replace("{", "").replace("}", "");   // Pass opportintytId  after replace "{"  , "}"

    Try with this  -

        function setAccount()
        {
            var opportintytId = Xrm.Page.getAttribute("opportunity").getValue()[0].id;
            opportintytId = opportintytId.replace("{","").replace("}","");
            var req = new XMLHttpRequest();
            req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/opportunities?$select=_contactid_value,_customerid_value&$filter=opportunityid eq "+opportintytId+"", 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 results = JSON.parse(this.response);
                        for (var i = 0; i < results.value.length; i++) {
                            var _contactid_value = results.value[i]["_contactid_value"];
                            var _contactid_value_formatted = results.value[i]["_contactid_value@OData.Community.Display.V1.FormattedValue"];
                            var _contactid_value_lookuplogicalname = results.value[i]["_contactid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                            // Here is customer Value
                            var _customerid_value = results.value[i]["_customerid_value"];
                            var _customerid_value_formatted = results.value[i]["_customerid_value@OData.Community.Display.V1.FormattedValue"];
                            var _customerid_value_lookuplogicalname = results.value[i]["_customerid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                        }
                    } else {
                        Xrm.Utility.alertDialog(this.statusText);
                    }
                }
            };
            req.send();
        }
    }


  • Aparna1 Profile Picture
    222 on at

    Hi Goutam,

    I missed the prefix while uploading the description now I have added it.

    The problem is the control is not going inside req.onreadystatechange = function()

    Do I need to add any library before using this function.

    Thanks

    Aparna

  • gdas Profile Picture
    50,091 Moderator on at

    From where you are calling the API ? if  its in the form event you can directly get the control using Xrm.Page.

  • Suggested answer
    Kokulan Profile Picture
    18,054 on at

    Hi

    The request is async and the onreadystatechange  will not be hit straight away.

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/opportunities?$select=_contactid_value,_customerid_value&$filter=opportunityid eq "+opportintytId+"", true);

    Could you try putting your alert or a console.log("success") inside the 200 block

    13. if (this.status === 200)

    {

      alert("Success");

       var result = JSON.parse(this.response);
      var account = result["=_customerid_value"];
     }

    If you get Success alert, that means the request has been successfully executed.

  • Aparna1 Profile Picture
    222 on at

    Goutam,

    I want to populate the name of respective account if we select the opportunity. These fields i.e. "Account" and "Opportunity" are present on case form and both of them are lookup. I have added the javascript on case form and added event on change of opportunity field.

    I need to populate the account field on change of opportunity before saving the record because account field is the customer.

    Thanks.

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Aparna,

    Try with this -

    function setAccount()
    {
        var opportintytId = Xrm.Page.getAttribute("new_opportunity").getValue()[0].id; // Replace opportunity lookup name
        opportintytId = opportintytId.replace("{","").replace("}","");
        var req = new XMLHttpRequest();
        req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/opportunities?$select=_contactid_value,_customerid_value&$filter=opportunityid eq "+opportintytId+"", 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 results = JSON.parse(this.response);
                    for (var i = 0; i < results.value.length; i++) {
                        var _contactid_value = results.value[i]["_contactid_value"];
                        var _contactid_value_formatted = results.value[i]["_contactid_value@OData.Community.Display.V1.FormattedValue"];
                        var _contactid_value_lookuplogicalname = results.value[i]["_contactid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                        
                        // Here is customer Value
                        var _customerid_value = results.value[i]["_customerid_value"];
                        var _customerid_value_formatted = results.value[i]["_customerid_value@OData.Community.Display.V1.FormattedValue"];
                        var _customerid_value_lookuplogicalname = results.value[i]["_customerid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                    
                        var lookupValue = new Array();
                        lookupValue[0] = new Object();
                        lookupValue[0].id =_customerid_value; // GUID of the lookup id
                        lookupValue[0].name = _customerid_value_formatted; // Name of the lookup
                        lookupValue[0].entityType = _customerid_value_lookuplogicalname; //Entity Type of the lookup entity
                        Xrm.Page.getAttribute("new_account").setValue(lookupValue); // Replace Account Lookup Id 
                    
                    
                    }
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send();
    }


  • Aparna1 Profile Picture
    222 on at

    Goutam,

    You are amazing. Thanks it worked for me.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans