Announcements
Hello,
I am facing the following problem:
There are two fields on a form of our system. The first is a lookup to an entity, the second is a date field. The lookup is on the entity "invoice" and this also has a date field on the form.
I would like to implement the functionality that when the first field (logical name: "invoiceid") is updated, the second field is filled with the value that is in the date field of the "invoice" entity. The date field has the logical name "invoicedate" on both forms.
I wrote the following code:
function Invoiceid_OnChange(executionContext) { let formContext = executionContext.getFormContext(); let invoiceNumber = formContext.getAttribute("invoiceid").getValue(); var invoiceNumberEntityType = invoiceNumber[0].entityType; var invoiceNumberId = invoiceNumber[0].id; Xrm.WebApi.retrieveRecord(invoiceNumberEntityType, invoiceNumberId.replace('{', '').replace('}', ''), "?$select=invoicedate").then( function success(result) { formContext.getAttribute("invoicedate").setValue(result[invoicedate]); }, function (error) { console.error(error.message); } ); }
The variables "invoiceNumberEntityType" and "invoiceNumberId" contain the correct values, I have already checked this.
Unfortunately, when I deploy the script, I get the following error: "TypeError: Cannot read properties of undefined (reading 'retrieveRecord') at Invoiceid_OnChange". I have checked "Pass execution context as first parameter" in the handler properties.
I am thankful for any advice.
Thank you for the detailed information!
The version query works now as well. Maybe the code will soon be needed on a system that is 8.1. Then no adjustment will be necessary for the time being.
regarding the version, it can be done, you can read a blog post I wrote while ago about this subject
www.crmanswers.net/.../xrmpagecontextgetversion-is-now.html
however if your code runs on a 9.1 version, the main problem is Xrm.Page, in a 9.x instance you should use formContext as Xrm.Page is considered deprecated in 9.x
Personally I would not spend too much time to create a javascript compatible with both 8.x and 9.x as usually when an instance is migrated to 9.x the scripts need to be rechecked to make compatible with such version.
hope it helps
Thank you very much! This works perfectly now! I just had to add "invoices".
I have one more question: could this code also be used on a 9.1 system? And is there a way to read the current version?
For example, would it be possible to have a variable called "version" that is inserted into my request so that the code still works when there is an update?
because your CRM version is 8.2 you can't use Xrm.WebApi (the methods were introduced with 9.0)
regarding your code, the main problem is with invoiceNumberEntityType
when you take the value from a lookup you get the logicalname (like account, contact, ...) but inside Web API calls (that are not using Xrm.WebApi) it should be inserted the collection name (like accounts, contacts, ...)
the Xrm.WebApi syntax uses the logicalname but you can't due to your version.
So if your lookup is pointing to invoice the collection name is invoices, there isn't a fixed rule to generate the collection name (the only check is to extract the collection name from the Metadata) but probably in your scenario is not necessary, just put invoices and you are ok.
Note that in your code the select is still pointing to "invoicedate" (a field that doesn't exist) make sure to change also there to "ebit_invoicedate"
hope it helps
Wow thank you very much for being so helpful.
I adapted the re.open line to the following:
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/"+invoiceNumberEntityType+"("+invoiceNumberId+")"+"?$select=invoicedate", true);
I changed the 9.1 to 8.2 since the system is of version 8.2.
"invoiceNumberEntityType" is "invoice" when I execute the program.
"invoiceNumberId" is the ID of the invoice when I execute the program.
"ebit_invoicedate" is the name of the field on the invoice-form.
However, when I debug the code the status is always 404. And the readystate is first 2, then 3 and then 4....
Hi,
You can generate entire code using below tool -
www.crmanswers.net/.../new-tool-dataverse-rest-builder.html
Please mark my answer verified if this is helpful!
Regards,
Bipin Kumar
Follow my Blog: xrmdynamicscrm.wordpress.com/
Hi,
okay understood now.
Try below code -
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/accounts(123455)?$select=accountnumber", 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.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);
var accountnumber = result["accountnumber"];
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
Modify entity name and field name.
Hello and thank you for your help!
If I replace it, I get this error message:
"TypeError: Cannot read properties of undefined (reading 'online') at Invoiceid_OnChange"
The probably much more important information is, that this environment is actually not a Online CRM-System as I first thought, but an On-Premise. Does this mean that the WebApi can not be used for this task? Is there an alternative for On-Premise-Systems?
Thanks in advance!
Hi,
Can you please replace Xrm.WebApi.retrieveRecord with Xrm.WebApi.online.retrieveRecord and check if it works.
Please mark my answer verified if this is helpful!
Regards,
Bipin Kumar
Follow my Blog: xrmdynamicscrm.wordpress.com/
André Arnaud de Cal... 291,359 Super User 2024 Season 2
Martin Dráb 230,370 Most Valuable Professional
nmaenpaa 101,156