As we know in CRM we can only access fields in JavaScript which are on the form. If we remove such fields from the form, we will end up with error “Unable to get value of the property ‘getValue’: object is null or undefined”. While removing fields CRM does not validate whether fields are being referred in any JavaScript.
With Dynamics 365 V9.0, new feature is introduced to tag dependent attributes with a JavaScript file. Once you tag an attribute with JavaScript file, you do not need to have it on form and you cannot delete that attribute too. Dynamics 365 automatically loads all tagged attributes in attribute collection when form is loaded. Please note tagged attributed are loaded in attribute collection only not in control collection. You will be able to use Xrm.Page.getAttribute but not Xrm.Page.getControl for tagged attributes.
Let’s see how we can make use of this new feature:
- I have one JavaScript file “sample_library_main.js”. Double click to open it
- You will see 2 tabs: Generic and Dependencies. Click on Dependencies tab (Available with Dynamics 365 V9.0)
- You will see 2 grids to add dependencies. Grid1 allows you to add web resources and Grid2 allows you to add attributes as dependencies. Click on Add button for Grid2
- Select entity and attribute. Click Ok. I have selected Sample as entity and First Name as attribute below
- Save & Publish your JavaScript file
- Let’s try to delete “First Name” attribute from Sample entity. Error will be thrown “Cannot Delete Component” as below:
Conclusion:
- After tagging an attribute to a JavaScript, it is not required to have the attribute on the form and still, we can use the value of the attribute.
- Tagging also makes sure that the attribute cannot be deleted.
- Xrm.Page.getControl functions do not work for tagged attributes if not on form.
References: