Set custom field for Request based on requester's domain
This example rule was created to update dropdown custom field called "Customer". You will need to replace custom field with its actual name and updated domains and custom field values in the mapping table of the Javascript section (example ones are Gmail, Yahoo and Microsoft).
{
"pipeline": [
{
"type": "source:targetprocess:EntityChanged",
"entityTypes": [
"request"
],
"modifications": {
"created": true,
"deleted": false,
"updated": false
}
},
{
"type": "filter:Relational",
"or": [
{
"and": [
{
"target": {
"type": "field",
"name": "VotesCount",
"target": {
"type": "field",
"name": "Previous",
"target": {
"type": "pipelineBlockOutput"
}
}
},
"value": {
"type": "constant",
"value": null
},
"operator": {
"type": "is"
}
}
]
}
]
},
{
"type": "action:JavaScript",
"script": "const api = context.getService(\"targetprocess/api/v2\");\nconst requestId = args.ResourceId;\n//Customers mappings by their domains. Syntax: \"customerdomain.com\": \"Custom Field Value\".\n//customerdomain.com should be in Lowercase ONLY! If this is not the last element, it should end with comma.\nconst customers = {\n \"gmail.com\": \"Gmail\",\n \"yahoo.com\": \"Yahoo\",\n \"microsoft.com\": \"Microsoft\"\n}\n\nconst requesters = await api.queryAsync(args.ResourceType, {\n select: \"requesters.select(email)\",\n where: \"id=\" + requestId\n});\nconst requester = requesters[0][0].replace(/^.+?@/i, '').toLowerCase();\n\nif (!customers[requester]) {\n console.log(\"There's no such value for this Customer.\")\n return;\n}\n\nconsole.log(`The request was assigned to ${customers[requester]}`);\n\nreturn {\n command: \"targetprocess:UpdateResource\",\n payload: {\n resourceType: args.ResourceType,\n resourceId: requestId,\n fields: {\n Customer: customers[requester]\n }\n }\n};\n"
}
]
}
Updated almost 3 years ago