Announcements
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.
Thank you to everyone for the recommendations and assistance!
I ended up using WebApi as shown below and that works great.
function TheMagicFunction(executionContext, MyVariable, ExtraqParam) { /*STUFF HERE*/ var a = 1; if (client == "Web") { if (/*STUFF HERE == THE STUFF*/ a == a) { if (/*MORE STUFF HERE == MORE STUFF*/ a == a) { Xrm.WebApi.online.retrieveMultipleRecords("myentity", "?$select=myid&$filter=fieldname eq 'valuethatineed'").then( function success(results) { for (var i = 0; i < results.entities.length; i ) { M/*ore Stuff Here*/ // Open the form Xrm.Navigation.openForm(entityFormOptions, parameters).then( function (success) { console.log(success); }, function (error) { console.log(error); }); } }, function (error) { Xrm.Utility.alertDialog(error.message); }); } } } else if (client == "Mobile"); { if (/*STUFF HERE == THE STUFF*/ a == a) { if (/*MORE STUFF HERE == MORE STUFF*/ a== a) { Xrm.WebApi.online.retrieveMultipleRecords("myentity", "?$select=myid&$filter=fieldname eq 'valuethatineed'").then( function success(results) { for (var i = 0; i < results.entities.length; i ) { //More Stuff Here // Open the form Xrm.Navigation.openForm(entityFormOptions, parameters).then( function (success) { console.log(success); }, function (error) { console.log(error); }); } }, function (error) { Xrm.Utility.alertDialog(error.message); }); } } } }
You can try to test it, it should work.
Hi Jim,
Please refer the given modified code. I have copied your code and modified it accordingly
function MyFn(executionContext) { var recordname = "Sample Record Name"; var formContext = executionContext.getFormContext(); var globalContext = Xrm.Utility.getGlobalContext(); var finalpathwithquery = Xrm.Page.context.getClientUrl() "/api/data/v9.1/cpp_franchiseedefaulttype?$select=cpp_franchiseedefaulttypeid,cpp_name&$filter=name eq 'Sample Record Name'"; //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 results = JSON.parse(this.response); data = results; } 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); }
t
t
Why do you want to use XMLHTTP? I will use the xrm.webapi for this scenario.
Will the JavaScript code generated this way also execute within the D365 Mobile App?
Hi Jim,
Why not using record name as filter directly?
You can try to use CRM REST Builder to create XmlHttpRequest to retrieve data, which can make build request more easily:
Download page: https://github.com/jlattimer/CRMRESTBuilder
For example, I want to get one account named ‘A. Datum Corporation (sample)’ .
1.Import zip file to the solution (Go Settings > Solutions), then open the tool.
2.Set it as following screenshot:
3.Click ‘create request’ button to get code, then run it.
4.Result:
Regards,
Leah Ju
Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.
André Arnaud de Cal... 291,359 Super User 2024 Season 2
Martin Dráb 230,370 Most Valuable Professional
nmaenpaa 101,156