latest Post

CRM 2016 - Editable Calculated fields

They are times when there is a need to have calculated fields in CRM, prior to CRM 2015 this had to be done manually using JavaScript or with the use of business rules to a lesser extent depending on how complicated the calculations were that's why calculated fields introduced in CRM 2015 were a major plus. 

However, today I came across a calculated field that had been setup in our system by another developer, the field was simple enough it was calculating the end date of a contract when given the start date and length in months. 

The calculation has been working fine however, the problem arose when our customer wanted a modification to give them the ability to override the calculation in certain scenarios.  
To be honest I assumed this would be a simple configuration change but it wasn't quite as simple as that and I ended up having to remove the calculated field and create a new field and calculate the end date via JavaScript. 

 Prior to doing this I tried the following configurations changes which I assumed would work:

 1.) Modify calculation to only calculate if the field is null - At first I thought I could wrap the calculation in a condition to only perform the calculation if the end date field was null. However, Microsoft prevent you from selecting the current field in the list.  I.E. having a condition on the same field as the calculated field. I guess it kind of makes sense to ensure no circular references.


2.) Create boolean field "Override Date" - I then decided I would create a boolean field that the user could select from and then use this field to prevent the field from calculating in the calculation condition.  Although this worked I quickly realised that I could not then make the calculated field editable. I tried using built in configuration, business rules and custom JavaScript. However, It appears the option to edit calculated fields is disabled regardless of which method is used to do this. 

3.) Create additional date field and if set update calculated field - I then tried to use the above boolean field and if set to "allow override" an additional date field was displayed. The thought process here was the user could use this field to store the new date and then either a workflow, business rule or JavaScript would copy the field value into the calculated field. However, again it was not possible to  set a calculated field in a business rule action or workflow the field is greyed out. I then tried with JavaScript and although this worked on save the calculated field was set back to null even when setting submit mode to always in the code.

Solution 
This left the only option to go back to the tried and tested method of manually performing the calculation with JavaScript..

To achieve this functionality I created a new "End Date" field and used the above boolean override field to set whether the field was editable or not. If the boolean override field was set to false the end date was calculated. I took advantage of the brilliant moment.js library to help peform the date calculations.  

The below gitgist shows the helper method  I wrote:

The below screenshot shows the UI with the field locked and date calculated as expected. 


A simple solution in the end but thought I would blog it in case it saves someone else sometime. it just goes to show things are not always as straight forward as you think they should be.  Maybe Microsoft will give us something out of the box in future releases.


Recommended Posts × +

0 comments:

Post a Comment