Hi,
can anyone advised on the code to get a field value from a related entity using JavaScript if this is possible?
*This post is locked for comments
Hi,
can anyone advised on the code to get a field value from a related entity using JavaScript if this is possible?
*This post is locked for comments
Hi,
I´m new to CRM and I trying to get the related entity ID with a request and I also used Rest Builder, but I'm not quite understanding how do i use the odata.nextlink to retrieve the related entity record ID so I can use it as an input in another function. Could you provide me with way to save that related record ID inside a variable so I could return it and use it as input? I thank in advance
Hi,
You can use CRM Rest Builder to generate complex code for retrieving entities data with nice UI.
Alternatively, please refer below code snippet
//Retries Route entity fields and its related contact and account fields
var Routes = $.ajax({ type: "GET", contentType: "application/json; charset=utf-8", datatype: "json", url: orgUrl + webAPIPath + "/new_routeitems?$select=new_days,new_address,new_title,new_todate,new_fromdate,new_hours,new_minutes&$expand=new_customer_contact($select=address1_city,address1_country,address1_line1,address1_line2,address1_line3,address1_postalcode,address1_stateorprovince,fullname,entityimageid),new_customer_account($select=address1_city,address1_country,address1_line1,address1_line2,address1_line3,address1_postalcode,address1_stateorprovince,name,entityimageid)&$filter=_new_route_value eq " + routeId + "&$orderby=new_order asc", beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0"); XMLHttpRequest.setRequestHeader("OData-Version", "4.0"); XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, async: false, success: function (data, textStatus, xhr) { var results = data; for (var i = 0; i < results.value.length; i++) { var new_routeitemid = results.value[i]["new_routeitemid"]; //Use @odata.nextLink to query resulting related records var new_customer_account_NextLink = results.value[i][ "new_customer_account@odata.nextLink" ]; var new_customer_contact_NextLink = results.value[i][ "new_customer_contact@odata.nextLink" ]; } }, error: function (xhr, textStatus, errorThrown) { toastr.error(textStatus + " " + errorThrown); } });
Hi ,
You can use rest api call.
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts()?$expand=account_primary_contact($select=accountid,address1_city,address1_country)", 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.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response);
var contactid = result["contactid"];
for (var a = 0; a < result.account_primary_contact.length; a++) {
var account_primary_contact_accountid = result.account_primary_contact[a]["accountid"];
var account_primary_contact_address1_city = result.account_primary_contact[a]["address1_city"];
var account_primary_contact_address1_country = result.account_primary_contact[a]["address1_country"];
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
Hi,
below code will help you to retrieve related record fields
function retrieveAddress() {
debugger;
var id = Xrm.Page.data.entity.getId();//get your parent record id
var context = Xrm.Page.context;
var serverUrl = context.getClientUrl();
var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
var retrieveResult = new XMLHttpRequest();
//pass your parent record guid to related entity primary key
retrieveResult.open("GET", ODataPath + "/new_productSet?$select=new_Country,new_State&$filter=new_AvailPdtId/Id eq guid'" + id + "'", false);
retrieveResult.setRequestHeader("Accept", "application/json");
retrieveResult.setRequestHeader("Content-Type", "application/json; charset=utf-8?");
retrieveResult.send();
if (retrieveResult.readyState == 4 /* complete */) {
if (retrieveResult.status == 200) {
var retrieved = this.parent.JSON.parse(retrieveResult.responseText).d;
var Result = retrieved.results[0];
if (typeof Result !== "undefined") {
var lookupValue = new Array();
lookupValue[0] = new Object();
lookupValue[0].id = Result.new_Country.Id;
lookupValue[0].name = Result.new_Country.Name;
lookupValue[0].entityType = Result.new_Country.LogicalName;
Xrm.Page.getAttribute("new_country").setValue(lookupValue);
Xrm.Page.getAttribute("new_country").setSubmitMode("always");
var lookupValue1 = new Array();
lookupValue1[0] = new Object();
lookupValue1[0].id = Result.new_State.Id;
lookupValue1[0].name = Result.new_State.Name;
lookupValue1[0].entityType = Result.new_State.LogicalName;
Xrm.Page.getAttribute("new_state").setValue(lookupValue1);
Xrm.Page.getAttribute("new_state").setSubmitMode("always");
}
}
}
}
Hi,
If you want to bring data from related entity based on it's primary key, check following sample code
If you are using >9.0 Check our sample: http://himbap.com/blog/?p=2874
If you are below <9.0 check this: http://himbap.com/blog/?p=1997
Finally you can also write your own script using Rest Builder: github.com/.../CRMRESTBuilder
Let us know if you are facing any issue
Thanks
Hi,
here are some sample: msdn.microsoft.com/.../gg334279.aspx
msdn.microsoft.com/.../mt770370.aspx
Or you can build them with the CRMRestBuilder: github.com/.../CRMRESTBuilder
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,269 Super User 2024 Season 2
Martin Dráb 230,198 Most Valuable Professional
nmaenpaa 101,156