Hi All,
Can anyone help me with sample code to retrieve the lookup record entity attributes value using js using API calling.
Thanks.
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); }); };
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?
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
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);
});
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); }); }; },
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.
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
The field i am retriving from PQR entity is Lookup field.
Thanks ju for the code. but i am not getting attribute of PQR entity in result object
dont know why.
Any sugessions
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.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156