Update Data
In the Workflow Builder, you can add Update Data as a service.
A VM Execution can be used here for each field.
The basic function of the VM is as follows, whereby the return value must always be adapted to the corresponding field:
(function(data){
return "";
})
The data object contains the complete relevant data set for which the update is being carried out.
For example, if you want to compose a salutation field from title, first name and surname, it would look like this:
(function(data){
let salutationParts = [];
if (data.title && data.title.length) {
salutationParts.push(data.title);
}
if (data.firstname && data.firstname.length) {
salutationParts.push(data.firstname);
}
if (data.lastname && data.lastname.length) {
salutationParts.push(data.lastname);
}
return salutationParts.join(" ");
})
In addition to the data set, further information can be transferred in data. This information depends on where and how the action is triggered.
When a data record is created or updated, two of these additional pieces of information are available.
data._triggerData: _triggerData contains the information that triggered an action. If the list settings mean that data is not updated automatically, individual pieces of information from the data._triggerData can be checked here in order to create conditions and update fields manually.
data._query: If data is transferred via the API or a form, the URL query parameters can be added, which do not affect the data record, but are available for further processing as data._query parameters. This can be helpful, for example, if data comes from different sources and you want to process it differently via a condition.
If you want to retrieve and process data via a webhook service, data._lastIntegrationData is available. It is important that the action Run Services Sequentially is activated. This allows the services to run in the order displayed and ensures that the data is available before it is processed.
For example, after a store user has logged in, the API of the store can be addressed with a webhook to generate a voucher and this voucher can then be added to the user with an update data.
Application example: The store's API is addressed with a webhook to generate a voucher after a user has logged in. The voucher is added to the user with Update Data.
Condition: Code Execution
In the conditions, you can also use the VM to execute Javascript based on the data set that returns a true/false to confirm or deny the condition.
(function(data){
return (data.email.substr(-3)==="gov");
})
In this VM, you also have access to data._query and data._triggerData.
Additional Information (Metrics & Tags)
There is additional information in the data object, which contains the user's activities and the internal JUNE tags.
data._metrics = {
activity: {
website_tracking: [
{
action:"goal",
created_at:"2023-07-19T11:38:04.408Z",
name:"order-placed",
reference_type:"list",
reference_id:"649ea3eb410bdec9553884b3"
},
...
],
list_action: [
{
trigger:"on_website_goal",
id:"64a6c83489c9f9ef0984a7a9",
created_at:"2023-07-19T11:38:04.753Z"
},
...
],
campaign: [
{
action: "link",
created_at: "2023-07-19T14:46:10.742Z",
id: "64b7f69bc3ac24f1f1f909b8",
value: "https://juneapp.com/"
},
...
]
},
tags: [
"my-first-tag",
...
]
}
Working in a VM (Tips & Tricks)
Since you are working in a VM, you do not have access to "normal" globals and prototypes like in a browser. Try to write the code as simply as possible in the ES5 standard.
You cannot use console.log() or similar to debug/test the execution.
To call up chained variables, the optional chaining operator should be used.
let firstname = data?._triggerData?.user?.first_name;
Date fields are saved in ISO format. If you want to work with a date or save a new date, you should therefore restore the ISO format.
let now = new Date().toISOString();
The API behind the VM relies on the return value that is returned. For example, if a NULL or undefined is passed for the firstname:string, a string is cast from it. The data set will then contain "null" or "undefined" for the firstname.