General
This article collects the most common known errors related to field formulas.
The formula attempts to create a string that is too long for the field
Each field in the CRM system has a maximum length. This can cause problems in cases where the formula imports data into a field that exceeds the maximum length allowed for that field. This can happen, for example, when a picture collects data from several different fields into one field.
The solution to this is to wrap the formula in the following formula:
{capture assign="calculate_length"}EXISTING FORMULA{/capture}{$calculate_length|truncate:<field_length_as_number>:""}
In the example, there is a variable calculate_length that calculates the length of the result produced by the formula. Instead of the variable calculate_lenght, you can also use a variable you have named yourself, as long as it is not the name of any field and does not contain any special characters (e.g., väliaikaispituus is not acceptable, but valiaikaispituus is). This is handled at the end using the truncate variable, whose value is the length of the field (as a number).
The truncate variable truncates the text entered in the field to the length specified in the formula. The variable must be followed by :””, as in the example formula, otherwise three dots (…) will appear after the truncation point, which will increase the number of characters by three. However, if you want the dots to appear as a truncation mark, you can subtract these three characters from the field length to make room for the dots.
So, if we try to put the word “sandwich cake” in a field with a maximum length of 10 characters, we could get the following results:
Example 1: sandwich cake|trucate:10:”” = sandwich c
Example 2: sandwich cake|truncate:7 = sandwic…
Mathematical calculations with empty or non-numeric values
When doing any kind of mathematical calculations in formulas, all used values must contain a numeric value. If a field needed in the calculation does not yet contain a value, like when the entity is first created, a default value can be assigned to a variable with the `default` modifier.
{math equation="a+b" a=$field1|default:0 b=$field2|default:0}
Also, note that the value of a field might change during the execution from a previous formula and it’s extremely important to ensure the result is a valid numeric value. Most common errors happen when a formula results in a value with trailing whitespace.
Division by zero error
Always ensure your formula does not result in division by zero. The easiest way to avoid this is by adding a condition around the calculation.
{if $my_field != 0}{math equation="a/b" a=1 b=$my_field}{else}1{/if}
Field name variants
Depending on where the dynamic field formula is used, field names may have variants. So if the dynamic field formula is not working, you might want to check the article Field name variants Legacy vs REST/CRM2.