I need to get the Guid of a record whose name value I specify via JavaScript.
I am using the code below, but am getting an error.
function MyFn(executionContext) {
var recordname = "Sample Record Name";
var formContext = executionContext.getFormContext();
var globalContext = Xrm.Utility.getGlobalContext();
//Retrieve Franchise Default Type GUID whose Name Equals "Sample Record Name" using WEB API
//Step-1 : create fetch xml in adv find and replace all double quotes with single quote inside properties to shape it as a string
var fetchXml = ''
''
''
''
''
''
' '
''
''
'';
//Step-2 : encode URI : var encodedFetchXML = encodeURI(fetchxml)
var encodedFetchXML = encodeURI(fetchXml);
//Step-3 : create a query path with query and odata partial url : var query = "/api/data/v9.2/entityname?fetchXml=" encodedFetchXML ;
var query = "/api/data/v9.2/cpp_franchiseedefaulttype?fetchXml=" encodedFetchXML;
//Step-4 : create complete path : var path = globalContext.getClientUrl() query ;
var finalpathwithquery = globalContext.getClientUrl() query;
//Step-5 : create a XmlHttpRequest to retrieve data
var data = null;
var isAsync = false;
var req = null;
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
req = new ActiveXObject("MSXML2.XMLHTTP.3.0");
}
req.open("GET", finalpathwithquery, isAsync);
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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response);
data = result;
}
else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
//Step-6 show return result values
var returnedrecord = null;
for (var i = 0; i < data.value.length; i ) {
returnedrecord = returnedrecord " | " data.value[i].id;
}
alert(returnedrecord);
}
The specific error I am getting is:
One of the scripts for this record has caused an error.
For more details, download the log file.
data is nullSession
Id: b6ed80ca-878b-4c99-9994-e79eab0a9fe7
Correlation Id: 2e3d6fe9-cf6e-4ff9-8289-4ec5cec1a746
Event Name: onload
Function Name: OnLoad
Web Resource Name: cpp_/entities/franchisedefault/franchisedefaultV9.js
Solution Name: Active
Publisher Name: DefaultPublisher
Time: Mon May 03 2021 18:08:27 GMT-0400 (Eastern Daylight Time)
If I pull the log file, there is not anything of relevance in there as shown below.
Where am I going wrong specifically?