Microsoft Dynamics CRM 365 - 3 JavaScript Bugs
We are working with Dynamics 365 V 8.2, and in the last year we found 3 bugs when trying to insert data into a record with the Client API.
One of the most frequently used methods of the client API is:
Xrm.Page.getAttribute('attributeName').setValue(newValue);
But you have to know 3 limitations when using it, depending on the attribute type:
1. OptionSetValue:
Changing the attribute value will trigger its 'OnChange' event method (if there is one). According to the SDK, the event should occur only in the following situations:
- Data in a form field has changed and focus is lost. There is an exception to this behavior that applies to Two-Option (Boolean) fields that are formatted to use radio buttons or check boxes. In these cases the event occurs immediately.
- Data changes on the server are retrieved to update a field when the form is refreshed, such as after a record is saved.
- The attribute.fireOnchange method is used.
One way to avoid this side effect is to set the value asynchronously:
setTimeout(() => Xrm.Page.getAttribute('attributeName').setValue(newValue), 100)
2. Lookup:
The structure if the data should be:
[{id: '{DMG73HG3-FAQ4-45B5-VGKD-H46H3MHW48N6}', entityType: 'contact', name: 'Ziv'}]
The 'id' property must have curly braces? ('{', '}') and must be in Uppercase, otherwise, the 'OnChange' event method (if have one) will be triggered when saving the record (!).
3. Date:
If your date attribute is in the 'Date only' format:
The format of the attribute must be:
new Date('yyyy-MM-ddT00:00')
Or shorter:
new Date('yyyy-MM-dd')
The hour and minutes must be '0'.
If you need to clone a date you can use this function to get it:
function getFormatedDate (date) {
return (Date.parse(date = new Date(date))) &&
new Date(date.getFullYear(),
date.getMonth(),
date.getDate());
}
const date = getFormatedDate(new Date());
Xrm.Page.getAttribute('dateAttribute').setValue(date);
Otherwise, the 'OnChange' event method (if there is one) will be triggered when saving the record!
I hope this will help you figure out the bugs you get and how to avoid bugs in the future.
Thanks,
Ziv Ben-Or
+972545882011
Microsoft Dynamics 365 CE | Power Platform | Senior Consultant at Avanade
3 年Thanks Ziv Ben-Or for the workaround w.r.to date field. Saved my hairs ??