Hi,
Javascript code to retrieve account name from related table case
.Can we use Xrm.WebApi.retrieveRecord or fetchxml
Hi,
Javascript code to retrieve account name from related table case
.Can we use Xrm.WebApi.retrieveRecord or fetchxml
Hello,
Replace the line
"<condition attribute='associatedcase' operator='eq' uiname='' uitype='incident' value='caseid' />"+
with line
"<condition attribute='associatedcase' operator='eq' uiname='' uitype='incident' value='" + caseid + "' />"+
and try it again.
Hello EKL,
You can use both of them. But Xrm.WebApi.retrieveRecord will provide the result little bit faster.
Thanks!
Ashok.
Hi,
The below code is not giving the name value.
var caseid = formContext.getAttribute("associatedcase").getValue()[0].id;
var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'> latematerialize='true'"+
"<entity name='service'>"+
"<attribute name='serviceid' />"+
"<filter type='and'>"+
"<condition attribute='associatedcase' operator='eq' uiname='' uitype='incident' value='caseid' />"+
"</filter>"+
"<link-entity name='incident' from='incidentid' to='associatedcase' link-type='inner' alias='aa'>"+
"<attribute name='customerid' />"+
"<filter type='and'>"+
"<condition attribute='customerid' operator='not-null' />"+
"</filter>"+
"</link-entity>"+
"</entity>"+
"</fetch>"
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/service?fetchXml=" + encodeURIComponent(fetchXml),true);
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);
var name = results[0];
} else {
alert(this.statusText);
}
}
};
req.send();
Hi EKL,
Please find the below code snippet with the retrieveMultipleRecords:-
// Get the related record's ID
var relatedRecordId = Xrm.Page.getAttribute("new_relatedrecordid").getValue()[0].id;
// Set the related entity name (e.g. "account" or "contact")
var relatedEntityName = "account";
// Set the query string to retrieve the related record's account name
var queryString = "?$select=name&$expand=accountid($select=name)&$filter=" + relatedEntityName + "id eq " + relatedRecordId;
// Retrieve the related record's account name using the Xrm.WebApi
Xrm.WebApi.retrieveMultipleRecords("new_relatedentityname", queryString).then(
function success(results) {
// Get the account name from the first result's accountid attribute
var accountName = results.entities[0]["accountid"]["name"];
// Use the account name as needed (e.g. set a field value)
formContext.getAttribute("new_accountname").setValue(accountName)
},
function error(error) {
console.log(error.message);
}
);
Thanks,
Manoj Mane.
Please mark this as VERIFIED if it helps.
Hello,
You can use both depending on what your scenario is. I would recommend using Dataverse Rest Builder for that purpose - github.com/.../DRB
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