Main functions - hdijkema/espocrm-hookedformulas GitHub Wiki
Functions on the main level
- Entity related functions
- Configuration
- Logging
- String shorthands
- Evaluating formulas
- Loops
- Converion between types
Entity related functions
Attr
Like entity\attribute, but works on a fetched entity.
VALUE = attr(ENTITY, ATTRIBUTE)
An attribute value of a fetched entity. Example:
$value = attr($entity, 'name');
SetAttr
Like entity\setAttribute, but works on a fetched entity.
setAttr(ENTITY, ATTRIBUTE, VALUE);
Example:
setAttr($entity, 'name', $my_new_name);
Configuration
To use these functions, you need to create an entity 'Config' with at least the following attributes:
- name
- type: enum of [ 'int', 'real', 'string', 'doc', 'script' ]
- valueInt: integer
- valueReal: floating point
- valueString: varchar
- valueDoc: file
- valueScript: script (custom type provided by the Scripts module). If you don't use the Scripts module, just choose varchar.
You can use these functions to fetch/store/increase configuration items of a given name. The configuration items are cached during a given Formula session. So if you trigger the recalculation of a lot of records from within your formula, these configuration items are not read from the database for each formula for the related records that is calculated.
Also create a formula on this entity that clears the config cache (see below).
ConfigGet
VALUE = ConfigGet(NAME)
Fetches the VALUE of the configuration item given by NAME. Throws an error if the configuration item NAME does not exist.
Example:
$default_email = ConfigGet('default-mail-address');
ConfigSet
ConfigSet(NAME, VALUE)
Updates the VALUE of the configuration item given by NAME. Throws an error if the configuration item NAME does not exist.
Example:
ConfigSet('default-mail-address', '[email protected]');
ConfigInc
NEXT_INCREMENT = ConfigInc(NAME)
Increases the VALUE of the integer
configuration item given by NAME with 1
. Throws an error if the configuration item NAME does not exist, or not of type integer
.
Example:
memberNumber = ConfigInc('contact-number-for-new-members');
ConfigExists
BOOL = ConfigExists(NAME)
Checks if the configuration item given by NAME exists.
Example:
ifThenElse(configExists('my-config-item'), $e = configGet('my-config-item');, $e = 'not-set';);
ConfigClearCache
ConfigClearCache()
Clears the global configuration items cache. Call this in the formula of your Config entity, to make sure that if a user manually changes a configuration item value, values are retrieved from the database. Otherwise one could get inconsistent values.
Logging
Log
Log('info|warning|error', MESSAGE-PART, ...)
Logs information to the EspoCRM Log ($GLOBALS['log']
). This is for debugging purposes.
LogAdd
LogAdd(VARIABLE, [TYPE], MESSAGE-PART, ...)
TYPE = info|warning|error
Logs information in a HTML format to the variable. This can be used e.g. to fill an HTML field (as provided by the Scripts module).
Example:
// clear log variable
LogAdd(log);
// log something as 'info'
LogAdd(log, 'Hi there!');
// log something as other
LogAdd(log, 'info', 'This is the', ' - ', 'info');
LogAdd(log, 'warning', 'This is the', ' - ', 'warning');
LogAdd(log, 'error', 'This is the', ' - ', 'error');
String shorthands
StrAdd
StrAdd(VARIABLE|ATTRIBUTE, STRING-PART1, ...)
A shorthand for $var = string\concatenate($var, 'part1, ...);
.
Example:
StrAdd($var, 'My name is ', name, '!');
Evaluating formulas
Fetch pre-configured formula scripts and evaluate them in place. Global scope for the formula in which they are evaluated.
Exec
Exec(SCRIPT)
Runs the given scripts in the current formula context.
Example:
$script = '$i = $i +1;logAdd(log, "increasing value to ", $i)';
$i = 0;
(...)
Exec($script);
(...)
Exec($script);
Loops
While
WHILE(CONDITION, BODY)
Executes BODY while CONDITION is true.
Example:
$i = 0;
$n = array\length($array);
while($i < $n,
logAdd(log, 'i = ', $i, ', value=', array\at($i));
$i = $i + 1;
);
Conversion between types
toInt
$a = toInt($b)
Converts $b to an integer value and type. If it is a string, e.g. 08
will convert to the integer value 8
. If it is a double, e.g. 10.2
, it will convert to the integer value 10
.
The resulting variable will be of type integer
.
toDouble
$a = toDouble($b)
Converts $b to a double value and type. If it is a string, e.g. 08.3
will convert to the double value 8.3
. If it is an integer, e.g. 10
, it will convert to the double value 10
.
The resulting variable will be of type double
.
toString
$a = toString($b)
Converts $b to a string value and type. If it is an integer, e.g. 8
will convert to the string value "8"
. If it is a double, e.g. 10.2
, it will convert to the string value "10.2"
.
The resulting variable will be of type string
.