About
If you want to add a new value to an existing customer attribute where LIST is set as a data type without overwriting existing values in a list, you may use Jinja code with the function "append."
It is possible to use the simple Jinja code below, where you will create a temporary list called tempList
and assign existing values from a customer attribute, which we call attribute_list
in this case. You can call the variables as you wish.
How to
Depending on your use case, we can append the string value "new" to the temporary list or use any other value, including a dynamic value like a customer property.
In the last part of the code, we'll call the temporary list we created. It will be done using the "Set attribute" node using the code editor, where the JSON format needs to be applied.
{% set tempList = customer.attribute_list %}
{% append 'new' to tempList %}
{
"attribute_list": {{tempList |replace("'",'"')}}
}
The replace
filter is necessary to convert single quotes to double quotes in the tempList
JSON output because JSON format requires all keys and string values to be enclosed in double quotes. The JSON would be invalid without this conversion, causing errors in the set_attribute node and making the entire operation fail. Ensuring the JSON adheres to the correct format prevents issues related to improper syntax.
🚧 Limitation
The set_attribute node's standard UI editor will not work, as it will simply overwrite the entire list instead of appending the new value.
Practical demonstration
1. Creating the scenario flow
2. Using the Jinja inside the code editor of the set_attribute node (at the top right corner)
3. Starting the scenario.
After running the scenario, a new value should be appended to the list of the customer attribute: