Sometimes we are so much busy in our day to day consulting stuffs that we frequently miss those small little gems in Dynamics 365 which are scattered throughout but we tend to ignore them more often than not.
And one of them is the Attribute dependency feature in webresources.
How many of you faced a situation where you needed to just put an attribute on the form so that the code inside your form events and field events can access the field to get its value and take logical decisions depending on the field value? And attribute dependencies just helps you to get around it. But trust me, still in 95% of the cases where a requirement like this arises, we go ahead and put the field on the form and hide it so that our scripts can access it. Now that you understand, let’s see how it is done.
We will use account entity attribute “industrycode” of type optionset as dependent attribute to the web resource which is being referred in the form load of account entity.
Create a web resource and go to Dependencies tab. Click Add to choose your entity and fields
Select Account entity and choose your dependent fields from the available fields
Now save and publish the web resource. I have added the lookup field, parentaccountid as well as dependency
Note: Make sure you save & publish your web resource to establish the configured dependencies. It’s very important to first click on Save and then publish to reflect the changes.
Open the Account entity form make sure the “Industry” field is not there on the form. All our customizations are in place and let’s see what magic we can see now
Open any existing account record. I will use Developer Tools window to test this.
P.S – I am using the deprecated Xrm.Page here since I am demoing it in developer tools window. For real scenarios, use the formContext to access your controls and attributes.
Let us now verify whether the industry control is available on the form. To do that just type this command “Xrm.Page.getControl(“industrycode”)”.
The control is unavailable which makes sense but the great thing is that we can still get the value of the industry field. To get the value of field just type following commands
Value : Xrm.Page.getAttribute(“industrycode”).getValue()
Not only this, you can even set the value and save the record.
In this way we can set value of fields which are not placed on the form. And also use their value to arrive at business decisions. Before this feature we would need to add the field on the form and hide the control. The dependent attributes are not limited to single entity, we can even select attributes from multiple entities and attach to web resource and the same webresource can be used in multiple entity form and field events.
So unless you need to work with the control, use Attribute dependency and don’t unnecessarily drag and drop the controls on the form and increase the form clutter.
Hope this helps!
Our product offerings:
CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)
Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)
Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)