With Dynamics CRM 2015, one of the new features added is a new field type called Calculated Fields. We can see an evolution pattern here. First, we started with JScript, taking us up to CRM 2011. Later, in CRM 2013 we’ve got Business Rules to help. Now Calculated Fields is providing more specialized functionality, and enhanced features. But let’s look at the details.
I’ve created for this example two Decimal Number fields named very creatively Field Value 1 and Field Value 2, and added them to the form of a new entity. You can do this on any existing entity also.
Next I’m adding a third field of type Decimal Number. Let’s name it in the same creative fashion to Calculated Field.
In Field Type select from the drop-down Calculated as the above screenshot shows, and click on the Edit button. In the new pop-up window, create your formula. For this example we use basic addition.
Your completed action will look like this:
Save your field, add all fields to the form, Save and Publish. Now try your customization. You will get something like this:
Why JScript will not die?
While now you don’t have to write a line of JScript to achieve this functionality, it is still not that user friendly. I mean, end-user. The calculated field appears to be only calculated when the form saves. Thus, if you want the total to be displayed as soon as you tab out of one of the fields part of the formula, you are back to the scripting board.
What else does it do?
The example above was sort of the “Hello World!” type example. But the calculated fields can do more. Let’s have a quick look at the screen you get when hitting the Edit button on a Calculated Field.
Using the equivalent of IF…ELSE statements, we can define complex branching in our calculation.
NOTE: The Else branch only becomes available to create once you add a condition.
Working in conjunction with branching, we can execute snippets of logic based on specific business rules.
As long as you stay within the specific data types used, you have a variety of actions you can take. You have access to the fields in the current entity, basic mathematical operations, along with a few gems. They include:
You can perform operations with dates, adding or subtracting days, hours, months, weeks and years with a simple selection,
And it will look like this:
(record was created on 2/4/2015)
You can join the string values of multiple string fields, along with your custom strings.
NOTE: You can not use fields of other data types in the Concat function, as it does not convert to string automatically (this could have been be useful).
And it will look like this:
You can trim from either end of a string.
This results in the formula
and displays the following results
What does not work?
There are a few limitations when using calculated fields. They include:
- You can not use lookup fields in the calculations
- The system does not do implicit casting, or any casting
- You can not mix and match data types in calculations (because casting is not supported)
- You must match the data types for the formulas to work (validation exists in place to not allow you to save an incorrect formula)
- Processing is not instant