I have attempted to use the code example from this This Community Forum Post to retrieve related records, but am not getting the same result in the post.
My function is shown below:
function CheckEstimateChange(executionContext) {
var formContext = executionContext.getFormContext();
var myId = formContext.data.entity.getId();
var FetchXML = "<fetch>" +
" <entity name='cpp_estimate' >" +
" <attribute name='statecode' />" +
" <attribute name='cpp_estimateid' />" +
" <attribute name='cpp_jobid' />" +
" <filter type='or' >" +
" <condition attribute='statuscode' operator='eq' value='923190001' />" +
" <condition attribute='statuscode' operator='eq' value='1' />" +
" </filter>" +
" <link-entity name='cpp_job' from='cpp_jobid' to='cpp_jobid' link-type='inner' >" +
" <filter type='and' >" +
" <condition attribute='cpp_jobid' operator='eq' value='"+ myId +"' />" +
" </filter>" +
" </link-entity>" +
" </entity>" +
"</fetch>";
debugger;
FetchXML = "?fetchXml=" + encodeURIComponent(FetchXML);
Xrm.WebApi.retrieveMultipleRecords("cpp_estimate", FetchXML).then(
function success(result) {
for (var RecordsCount = 0; RecordsCount < result.entities.length; RecordsCount++) {
outputText += result.entities[RecordsCount].id + "\t\t" + result.entities[RecordsCount] + "\n";
}
Xrm.Utility.alertDialog(outputText, null);
},
function (error) {
// Handle error conditions
Xrm.Utility.alertDialog(error.message, null);
});
}
When I run it, the code does not enter the success callback nor does it enter the failure callback, which is very strange.
The screen shot below shows that it appears to jump out of the function and not execute the Xrm.WebApi.retrieveMultipleRecords command.
Has anyone seen this before? Does anyone have any recommendations or advice on how to correct this issue?
I need to use this code to retrieve the GUID of a related record via front end/UI JavaScript.
*This post is locked for comments
Hi jim,
In Network Tab of chrome developer tools select "XHR" instead of "JS" to view APIs calls.
Regards,
Ketan
Hi Jim,
Here is the sample code in JavaScript:
function CheckEstimateChange(executionContext) {
var formContext = executionContext.getFormContext();
var myId = formContext.data.entity.getId(); // if its not work replace with var myId = formContext.data.entity.getId().replace("{","").replace("}","")
var FetchXML = "<fetch>" +
" <entity name='cpp_estimate' >" +
" <attribute name='statecode' />" +
" <attribute name='cpp_estimateid' />" +
" <attribute name='cpp_jobid' />" +
" <filter type='or' >" +
" <condition attribute='statuscode' operator='eq' value='923190001' />" +
" <condition attribute='statuscode' operator='eq' value='1' />" +
" </filter>" +
" <link-entity name='cpp_job' from='cpp_jobid' to='cpp_jobid' link-type='inner' >" +
" <filter type='and' >" +
" <condition attribute='cpp_jobid' operator='eq' value='"+ myId +"' />" +
" </filter>" +
" </link-entity>" +
" </entity>" +
"</fetch>";
debugger;
var encodedFetchXml = encodeURI(FetchXML);
var queryPath = "/api/data/v9.0/cpp_estimate?fetchXml=" + encodedFetchXml; // should be replaced with the entity name you are trying to query and also update version.
var requestPath = Xrm.Page.context.getClientUrl() + queryPath;
var req = new XMLHttpRequest();
req.open("GET", requestPath, true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
if (this.readyState === 4) {
this.onreadystatechange = null;
if (this.status === 200) {
var returned = JSON.parse(this.responseText);
var results = returned.value;
if (results.length > 0) {
//---- WRITE LOGIC HERE-----------
//TODO: Implement logic for handling results as desired
}
} else {
alert(this.statusText);
}
}
};
req.send();
}
Or If you don't want to Change any code of yours please try this line :
<fetch mapping='logical' version='1.0' output-format='xml-platform' distinct='false'> instead of <fetch>
Hope this help
Thanks
Ajyendra Singh
Please Mark as verified if this answer is helpful for you.
Replacing the code var myId = formContext.data.entity.getId()" with " var myId = formContext.data.entity.getId().replace("{","").replace("}","") does not fix the issue.
The same result occurs where it jumps out of the function and does not execute the Xrm.WebApi.retrieveMultipleRecords command.
When looking on the Network tab in the dev/debug window, nothing jumps out at me as being problematic unless I am not looking in the right place.
Hi,
Try removing curly braces around GUID.
i.e.. Replace line "var myId = formContext.data.entity.getId()" with " var myId = formContext.data.entity.getId().replace("{","").replace("}","")
Also try checking network tab in chrome dev tool, it will show you exact error for this webapi call.
Regards,
Ketan
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,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156