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()
Text: Xrm.Page.getAttribute(“industrycode”).getText()
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!
Debajit Dutta
(Dynamics MVP)
For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com
Our product offerings:
CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)
Notes Manager (https://debajmecrm.com/add-metadata-to-your-notes-and-attachments-in-dynamics-notes-metadata-manager-from-xrmforyou-com/)
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)
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.
All this new feature goodness with the deprecated Xrm.Page references? May want to comment on that :p
Nice catch 🙂 and honestly didn’t notice. Actually was too lazy so tried out with developer tools. Will update the blog now..
Thanks
Hi Debajit,
Will this also work for fields that are on BPF? I tried doing it but it didn’t worked. I do not want the field to be present on form that is also needed on the BPF process. Is there any way to exclude keeping field on form but on BPF it will be. And on that field change I am calling some javascript.
As of now if I hide that field on the form it is working as expected but I do not want the form to clutter with unwanted fields.
-Prashant
Good Question. I really need to check this out.
I will do it and ping back.