Introduction
Derived attributes allow you to define attributes for your Chakra Object based on automatic calculations. Following are the supported types:
-
Formula Based
-
Query Based
-
Chakra Script Based
As the name suggests, you cannot set the value of a derived attribute - it will be computed by the system based on the logic provided either in the form of a query, formula or script.
Derived attribute data shows up under the derivedData key of your object like:
{
"_data": {
"id": "ecd2f0fe-c755-4deb-9a97-f5bd8d9e9f02",
"createdAt": 1597212692774,
"updatedAt": 1597212693393,
"status": "OPEN",
"data": {
"name": "Lin Liu",
"source": "Form",
"google_coordinates": [
12.323333,
27.188833
],
"duplicate": true,
"phone_number": "4400000105",
},
"derivedData": {
"lead_source": "google",
"lead_priority": 5,
"lead_score": 50
},
...
}
In the sample process data shown above, the attributes under derived data - lead_score, lead_priority and lead_score are the derived attributes. The attributes under data are the standard attributes.
Attribute Type
Derive attributes follow the same type system as normal attributes. So when you define a derived attribute like lead_priority_score you will also need to define the attribute type like int, real, ... and ensure that the formula/scripts returns data in the same format.
Chakra Script Based DA
Chakra Script based derived attributes execute a user defined script to derive the final Script data. The attribute page for these attributes look something like below:
![image-20200812115316160](/Users/sahoo/Library/Application Support/typora-user-images/image-20200812115316160.png)
Runtime
The script must be a javascript script based on Nodejs 10.0 runtime. It should output a value after execution. The script has to be synchronous all async attempts are ignored.
Because they are strictly tied to Object creation and updates - these scripts would be kept light-weight. Recommended practice is to keep execution time under 100 ms.
Require
You cannot require external modules. A few utility modules are pre-included in the context and can be used directly:
_
- Lodash - JS utility libmoment
- moment - JS datetime libmoment_tz
- moment-timezone - JS datetime lib with timezone plugins
Console.log
Console.log is disabled and will not work as intended during actual runs. However the script logs will return console.log output during test runs.
Payload
The script is provided a json object called PAYLOAD as part of the global context that contains the chakra object for which the calculations are to be made.
Examples
All the following are valid examples of scripts
-
1+2
-
PAYLOAD.data.duplicate? true: false
-
function calculateNormalScore(payload) {
return _.get(payload, 'data.lead_score') / 100
}
calcualteNormalScore(PAYLOAD);
Logs
The Chakra Scripts page gives you access to logs of previous executions with the payload info as well.
![image-20200812120308218](/Users/sahoo/Library/Application Support/typora-user-images/image-20200812120308218.png)
Log data legend:
- Started At - The time at which the script started running
- Script - Name of the script
- Status - Success/Error - Completion status of the script
- Payload - the input data provided to the script during the execution
- Response - The response output provided the script
- Log - The log outputted by the script during a test execution
- Exception Log - Exception log if any when the script errors out
- Duration(ms) - Duration in Millie's taken for the script to be executed
- Debug - false indicates the execution happened in-situ whule true indicates a test run initiated by the user
Test
You can modify and test the script by using the test payload option. It is strongly recommended to use the payload of previous script executions to test a script before saving modifications