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 365 | Integration, Dataverse...
Answered

Javascript to retrieve lookup records entity attributes value in Dynamics 365

(0) ShareShare
ReportReport
Posted on by 255

Hi All,

Can anyone help me with sample code to retrieve the lookup record entity attributes value using js using API calling.

Thanks.

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    In order to retrieve id of the lookup's record you can use something like

    function getLookupId(executionContext){

    var formContext = executionContext.getFormContext();

    var lookupValue = formContext.getAttribute("put your lookup logical name here").getValue();

    if (!lookupValue){

    //lookup is blank so there is nothing to retrieve

    return;

    }

    var recordId = lookupValue[0].id;

    //do retrieve logic using Xrm.WebApi here

    }

    To retreive the data you should use Xrm.WebApi - carldesouza.com/.../

  • Mona Chavan Profile Picture
    255 on at

    Thanks Andrew for reply . but i have one entity "ABC" with one lookup field of "PQR" entity. i need to retrieve one field value of  that lookup record  and need to compare it with one of the field on "ABC entity". which approach i should use.?

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi Mona,

    You need use first way to retrieve single record with the id of the lookup field.

    The following is example code:

    function compare(executionContext) {
        var formContext = executionContext.getFormContext();
        var test = formContext.getAttribute('new_test').getValue();//field of 'ABC' entity
        var lookupId = formContext.getAttribute('new_PQR').getValue();//'PQR' lookup field id
        var lookupId = serviceAccount[0].id.slice(1, -1);//remove{}
        if (lookupId != null) {
            Xrm.WebApi.online.retrieveRecord("PQR", ""   lookupId   "", "?$select=description").then(
                function success(result) {
                    var description = result["description"];//field of 'PQR' entity
                    //...do comparetion operation
                },
                function (error) {
                    Xrm.Utility.alertDialog(error.message);
                }
            );
        }
    }

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

  • Mona Chavan Profile Picture
    255 on at

    Thanks ju for the code. but i am not getting  attribute of PQR entity in result object

    dont know why.

    Any sugessions

  • Mona Chavan Profile Picture
    255 on at

    The field i am retriving from PQR entity is Lookup field.

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    The direction I gave is pretty much the same what Leah Ju posted.

    With lookup fields in webapi you will have to work in a different way. So if field's name is "lookup" query should look like ?$select=_lookup_value

    And generally speaking you can use CrmRestBuilder to prepare the query - github.com/.../2.6.0.0

  • Mona Chavan Profile Picture
    255 on at

    Thanks andrew it worked for me. but i am facing problem while returning the value to another function from this place .

    Below are my two functions

    PPayment_onChange: function(executionContext)

    {

    formContext = executionContext.getFormContext();

    if(formContext.getAttribute("id").getValue()!=null)

    {

    var p=formContext.getAttribute("rcm_pid").getValue()[0].id;

    var E=formContext.getAttribute("rcm_eid").getValue()[0].id;

    p = p.replace(/[{}]/g, "");

    var PEid=PPayment.RetriveData(executionContext,p,E);

    }

    },

       RetriveData: function(executionContext,p,E) {

    Xrm.WebApi.online.retrieveRecord("PEntity", "" + p + "", "?$select=name,_e_value")

                               .then(

                        function success(result) {

    return result._e_value;

               },

               function (error) {

                   Xrm.Utility.alertDialog(error.message);

               }

           );

    },

    i need the _e_value in  "var PEid" of first function

    i wrote above code but not getting anything.

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Xrm.WebApi calls are executed in async way. In order to utilize this namespace you will have to use Promise. Here is an example:

    PPayment_onChange: function(executionContext)
    {
    	var formContext = executionContext.getFormContext();
    	if(formContext.getAttribute("id").getValue()!=null) {
    		var p=formContext.getAttribute("rcm_pid").getValue()[0].id;
    		var E=formContext.getAttribute("rcm_eid").getValue()[0].id;
    		p = p.replace(/[{}]/g, "");
    		PPayment.RetriveData(executionContext,p,E)
    		.then(function(PEid) {
    			//you can use PEid here
    		}).catch(function(error){
    			alert(error.message);
    		});
    	}
    
    },
    RetriveData: function(executionContext,p,E) {
    	return new Promise(function(resolve, reject) {
    		Xrm.WebApi.online.retrieveRecord("PEntity", ""   p   "", "?$select=name,_e_value")
    		.then(function success(result) {
    			resolve(result._e_value);
    		},
    		function (error) {
    			reject(error);
    		});
    
    	};
    },

  • Mona Chavan Profile Picture
    255 on at

    sorry Andrew but i did not understood in which variable i will get value of "result._e_value" in first function. and if i want to use this value i need to write my code in below block?

    PPayment.RetriveData(executionContext,p,E)

    .then(function(PEid) {

    // how can i use my result._e_value in this block?

    }).catch(function(error){

    alert(error.message);

    });

  • Mona Chavan Profile Picture
    255 on at

    also i need to save the record in case of success and do not want to save record in case of fail.

    How can i achieve that

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 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 41 Most Valuable Professional

#2
iampranjal Profile Picture

iampranjal 39

#3
Satyam Prakash Profile Picture

Satyam Prakash 35

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans