Skip to main content

Notifications

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Javascript to retrieve lookup records entity attributes value in Dynamics 365

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.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Javascript to retrieve lookup records entity attributes value in Dynamics 365

    Try to use following code:

    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) {
    			//use PEid here to set field or whatever
    			formContext.data.entity.save();
    		}).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
    Mona Chavan 255 on at
    RE: Javascript to retrieve lookup records entity attributes value in Dynamics 365

    Hi Andrew I got the value in return but i want to save record if comparison is successful and din't want to save record when comparison is fail.

    can you please help me how can i achieve that?

  • Mona Chavan Profile Picture
    Mona Chavan 255 on at
    RE: Javascript to retrieve lookup records entity attributes value in Dynamics 365

    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

  • Mona Chavan Profile Picture
    Mona Chavan 255 on at
    RE: Javascript to retrieve lookup records entity attributes value in Dynamics 365

    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);

    });

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Javascript to retrieve lookup records entity attributes value in Dynamics 365

    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
    Mona Chavan 255 on at
    RE: Javascript to retrieve lookup records entity attributes value in Dynamics 365

    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.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Javascript to retrieve lookup records entity attributes value in Dynamics 365

    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
    Mona Chavan 255 on at
    RE: Javascript to retrieve lookup records entity attributes value in Dynamics 365

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

  • Mona Chavan Profile Picture
    Mona Chavan 255 on at
    RE: Javascript to retrieve lookup records entity attributes value in Dynamics 365

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

    dont know why.

    Any sugessions

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Javascript to retrieve lookup records entity attributes value in Dynamics 365

    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.

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans