logo

Example of a script for calculating a field value

ELMA features a Field value calculation function. A C# script is used for calculations. There are many ways to use this functions. Let's take a look at some of them. By default, when you create an object field, the value calculation function is disabled. To enable it, go to the field settings, switch to the Advanced tab, and in the Value calculation type list select C# script.

In ELMA, there is the Contact object. Let's create a Surname and Initials property in and write a script that will calculate its value.

Script:

string.IsNullOrEmpty(Surname) || string.IsNullOrEmpty(Firstname) || string.IsNullOrEmpty(Middlename) ?
 " " : string.Format("{0} {1}. {2}.", Surname, Firstname.Substring(0,1), Middlename.Substring(0,1))
 
 Publishing and restarting
After adding a script, publish the object and restart the ELMA server

Let's see how it works:

After saving the contact: 

 
Important!
When working with the field value calculation, keep in mind that a script is a function that must always return a value.
Also, all the variables used in the script must be checked for being empty or null, otherwise an unhandled exception will occur when restarting the server.
You can use only the properties of the selected object in a script.
Since a field value is calculated each time when referring an object, you must exclude recursion from scripts, i.e. refer the field whose value is calculated at the moment. Otherwise, it will cause a system error.