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 :
Customer experience | Sales, Customer Insights,...
Answered

JS to retrieve GUID using webapi

(0) ShareShare
ReportReport
Posted on by 238

Simple question for Dynamics CRM Version 8. I have a value (plain text field) on a record in an entity which we'll call entity1. I would like to use that value, to locate the GUID of a record in entity2 - who's record name matches that value. For example, the value of the text field in entity1 is "Test Text". Entity2 has a record named "Test Text". I need to return the GUID of that record in a variable (which i will then use to set a lookup filed). I can use something like the below, but it doesn't seem to work.

function GetGUID(FldValue) {
var RecGuid;
var req = new XMLHttpRequest(RecValue);
req.open("GET", Xrm.Page.context.getClientUrl()   "/api/data/v8.2/new_entitys2?$select=new_entity2id&$filter=new_name eq" ' ' "'" FldValue "'",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 RecGuid = results.value[i]["new_entity2id"];
            }
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();
}

I have the same question (0)
  • Suggested answer
    Justinjose Profile Picture
    2,707 on at

    Hi JoeO,

    Please make below changes to the code

    req.open("GET", Xrm.Page.context.getClientUrl()   "/api/data/v8.2/new_entitys2?$select=new_entity2id&$filter=new_name eq '"   FldValue   "'", true);

    Thanks
    Justin Jose

  • JO-30091535-0 Profile Picture
    238 on at

    Hi Justin,

    The query works fine, thats not the issue, i just dont know how to return from this query (and pass to a variable) "just" the guid?

  • Suggested answer
    Marco.P Profile Picture
    2,405 on at

    Hi,

    var RecGuid = results.value[i]["new_entity2id"];  seems fine to me.

    Are you sure you're actually retrieving data from that call?

    Could you try a console.log(results) to see what you're retrieving?

  • Verified answer
    Justinjose Profile Picture
    2,707 on at

    Hi JoeO,

    You are getting record id at line 16 (RecGuid). You can pass the id to any function after line 16 something like myfunction(RecGuid)

    or if you want to set the lookup field then try below code. 

    Should update filter "select=new_entity2id,new_name" to get the record name

    if (this.status === 200) {
        var results = JSON.parse(this.response);
        for (var i = 0; i < results.value.length; i  ) {
            var RecGuid = results.value[i]["new_entity2id"];
            var name = results.value[i]["new_name"];
    
            // Create lookup Object
            var lookupValue = new Array();
            lookupValue[0] = new Object();
            lookupValue[0].id = RecGuid; // GUID of the lookup id
            lookupValue[0].name = name; // Name of the lookup
            lookupValue[0].entityType = "new_entitys2"; //Entity Type of the lookup entity
    
            // Replace the lookup field Name
            //Xrm.Page.getAttribute("FieldName").setValue(lookupValue); // Xrm.Page.getAttribute is deprecated. So the below code is to set the lookup
            formContext.getAttribute("FieldName").setValue(lookupValue);
        }
    }

    Please let me know if you have any questions.

    Thanks
    Justin Jose

  • JO-30091535-0 Profile Picture
    238 on at

    Ok, my final script looks like the one below, but its not setting the lookup field. I call the "GetTextValue" function OnChange of the "new_txtfield" field, and i wish to set the "new_lookupfield" field. I followed the query its running OnChange of the "new_txtfield" field (Chrome DevTool) and the query is successfully executed. I follow the link of the query, and i see the expected jSon repose for the expected record

    function GetTextValue() {
        var TextValue = Xrm.Page.data.entity.attributes.get("new_txtfield").getValue();
        if (TextValue != null) {
            var Text = GetLookupFromText(TextValue);
            
        }
    }
    
    function GetLookupFromText(TextValue) {
        var req = new XMLHttpRequest();
        req.open("GET", Xrm.Page.context.getClientUrl()   "/api/data/v8.2/new_entitys2?$select=new_entity2id,new_name$filter=new_name eq" ' ' "'" TextValue "'",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 RecGuid = results.value[i]["new_entity2id"];
                        var name = results.value[i]["new_name"];
                        // Create lookup Object
                        var lookupValue = new Array();
                        lookupValue[0] = new Object();
                        lookupValue[0].id = RecGuid; // GUID of the lookup id
                        lookupValue[0].name = name; // Name of the lookup
                        lookupValue[0].entityType = "new_entitys2"; //Entity Type of the lookup entity
    
                        // Replace the lookup field Name
                        //Xrm.Page.getAttribute("FieldName").setValue(lookupValue); // Xrm.Page.getAttribute is deprecated. So be below code to set the lookup
                        formContext.getAttribute("new_lookupfield").setValue(lookupValue);
                    }
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send();
    }
    

  • Suggested answer
    Justinjose Profile Picture
    2,707 on at

    Hi JoeO,

    You are not passing executionContext. So for time being I have commented formContext at line 34 and uncommented line 33

    try below code

    function GetTextValue() {
        var TextValue = Xrm.Page.data.entity.attributes.get("new_txtfield").getValue();
        if (TextValue != null) {
            var Text = GetLookupFromText(TextValue);
            
        }
    }
    
    function GetLookupFromText(TextValue) {
        var req = new XMLHttpRequest();
        req.open("GET", Xrm.Page.context.getClientUrl()   "/api/data/v8.2/new_entitys2?$select=new_entity2id,new_name$filter=new_name eq" ' ' "'" TextValue "'",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 RecGuid = results.value[i]["new_entity2id"];
                        var name = results.value[i]["new_name"];
                        // Create lookup Object
                        var lookupValue = new Array();
                        lookupValue[0] = new Object();
                        lookupValue[0].id = RecGuid; // GUID of the lookup id
                        lookupValue[0].name = name; // Name of the lookup
                        lookupValue[0].entityType = "new_entitys2"; //Entity Type of the lookup entity
    
                        // Replace the lookup field Name
                        Xrm.Page.getAttribute("new_lookupfield").setValue(lookupValue); // Xrm.Page.getAttribute is deprecated. So be below code to set the lookup
                        //formContext.getAttribute("new_lookupfield").setValue(lookupValue);
                    }
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send();
    }

    Thanks
    Justin Jose 

  • JO-30091535-0 Profile Picture
    238 on at

    Ah of course! duh...

    Thanks Justin. Its working well.

  • Suggested answer
    Justinjose Profile Picture
    2,707 on at

    Hi JoeO,

    In addition

    With the latest version, the Xrm.Page object is deprecated, and you should use the getFormContext method of the passed in execution context object to return reference to the appropriate form or an item on the form but Xrm.Page still works.

    I have updated the script to support getFormContext

    Use the Pass execution context as first parameter 

     clientapi_2D00_passexecutioncontext.png

    then use following script

    unction GetTextValue(executionContext) {
        var formContext = executionContext.getFormContext(); 
        var TextValue =  formContext.getAttribute("new_txtfield").getValue();
        if (TextValue != null) {
           GetLookupFromText(TextValue);        
        }
    }
    
    function GetLookupFromText(TextValue,formContext) {
        var req = new XMLHttpRequest();
        req.open("GET", Xrm.Page.context.getClientUrl()   "/api/data/v8.2/new_entitys2?$select=new_entity2id,new_name$filter=new_name eq" ' ' "'" TextValue "'",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 RecGuid = results.value[i]["new_entity2id"];
                        var name = results.value[i]["new_name"];
                        // Create lookup Object
                        var lookupValue = new Array();
                        lookupValue[0] = new Object();
                        lookupValue[0].id = RecGuid; // GUID of the lookup id
                        lookupValue[0].name = name; // Name of the lookup
                        lookupValue[0].entityType = "new_entitys2"; //Entity Type of the lookup entity
    
                        // Replace the lookup field Name
                        //Xrm.Page.getAttribute("new_lookupfield").setValue(lookupValue); // Xrm.Page.getAttribute is deprecated. So be below code to set the lookup
                        formContext.getAttribute("new_lookupfield").setValue(lookupValue);
                    }
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send();
    }

    Thanks
    Justin Jose

  • JO-30091535-0 Profile Picture
    238 on at

    I know, that's what i did. I forgot to set it.

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 83 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans