Derived Attribute
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.
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 lib
- ```moment``` - moment - JS datetime lib
- ```moment_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```
- ```javascript
function calculateNormalScore(payload) {
return _.get(payload, 'data.lead_score') / 100
}
calcualteNormalScore(PAYLOAD);
### LogsThe Chakra Scripts page gives you access to logs of previous executions with the payload info as well.Log data legend:1. Started At - The time at which the script started running2. Script - Name of the script3. Status - Success/Error - Completion status of the script4. Payload - the input data provided to the script during the execution5. Response - The response output provided the script6. Log - The log outputted by the script during a test execution7. Exception Log - Exception log if any when the script errors out8. Duration(ms) - Duration in Millie's taken for the script to be executed9. Debug - false indicates the execution happened in-situ while true indicates a test run initiated by the user### TestYou 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