Send personal Slack notifications about assignments

This rule will take SlackID from the corresponding custom field of the User in Targetprocess. Then rule sends a message to this SlackID. If such a User doesn`t exist in Slack or SlackID is incorrect then the rule catches an exception and sends a notification to #slack-test channel.

🚧

Pre-requisites

Users should have a custom field SlackID

📘

How to apply this rule?

1064

🚧

Warning: sample URLs!

Please make sure to replace https://hooks.slack.com/services/xxxxx/yyyyyy/zzzzzzzzztGRAKwHLfm4kw with your Slack Webhook URL and https://youraccount.tpondemand.com/ with your Targetprocess URL

[
  {
    "type": "source:targetprocess:EntityChanged",
    "entityTypes": [
      "Assignment"
    ],
    "modifications": {
      "created": true,
      "deleted": false,
      "updated": false
    }
  },
  {
    "type": "action:JavaScript",
    "script": "const api = context.getService(\"targetprocess/api/v2\");\nconst assignments = await api.queryAsync(\"Assignment\", {\n  select: \"{type:assignable.entityType.name,slackId:generalUser.slackId,userId:generalUser.Id}\",\n  where: `id=${args.ResourceId}`\n});\nif (!assignments.length) { return; }\n\nconst slackId = assignments[0].slackId;\nconst type = assignments[0].type;\nconst link = `<https://youraccount.tpondemand.com/entity/${args.Current.Assignable.Id}|${args.Current.Assignable.Name}>`;\n\nconst http = context.getService('http');\n\ntry {\n  await http.postAsync('https://hooks.slack.com/services/xxxxx/yyyyyy/zzzzzzzzztGRAKwHLfm4kw', {\n    body: {\n      \"text\": `${args.Author.FullName} has assigned you on ${type} ${link}`,\n      \"channel\": slackId,\n      \"icon_emoji\": \":muscle:\",\n      \"username\": \"TP\"\n    }\n  });\n}\n\ncatch (error) {\n  await http.postAsync('https://hooks.slack.com/services/xxxxx/yyyyyy/zzzzzzzzztGRAKwHLfm4kw', {\n    body: {\n      \"text\": `User #${assignments[0].userId} has incorrect SlackID `,\n      \"channel\": slackId,\n      \"icon_emoji\": \":muscle:\",\n      \"username\": \"TP\"\n    }\n  });\n  console.log(error);\n}"
  }
]