Reminders in comments
It is not possible to send emails directly from Automation Rules, so we have to use this workaround and add a comment with @mention, which would eventually result in the actual email.
Remind the owner that Planned End Date is coming
[
{
"type": "source:schedule",
"schedule": {
"kind": "interval",
"unit": "day",
"value": 1
}
},
{
"type": "action:JavaScript",
"script": "// how many days before the deadline this reminder should be send?\nconst days = 3;\nconst type = \"Task\";\n\nconst today = new Date();\nconst dow = today.getDay();\nif (dow == 6 || dow == 0) { return; } // it's a week-end. No notifications\nconst daysCount = (dow + days <= 5)\n ? days // it's the beginning of the week\n : days + 2; //it's the end of the week. We add 2 days to compensate the weekend\nconst date = `DateTime.Today.AddDays(${daysCount})`;\n\nconst api = context.getService(\"targetprocess/api/v2\");\nconst items = await api.queryAsync(type, {\n where: `EntityState.IsFinal=false and PlannedEndDate<=${date}`,\n select: '{id,ownerName:Owner.fullname,ownerLogin:Owner.login,deadline:PlannedEndDate}'\n});\n\nreturn items.map(item => {\n let mentions = `@user:${item.ownerLogin}[${item.ownerName}]`;\n let plannedEnd = new Date(item.deadline);\n return {\n command: \"targetprocess:CreateResource\",\n payload: {\n resourceType: \"Comment\",\n fields: {\n General: { Id: item.id },\n Description: mentions + \", this item should be finished on \" + plannedEnd.toDateString()\n }\n }\n };\n})"
}
]
Send a reminder to Assigned Users on a custom date
[
{
"type": "source:schedule",
"schedule": {
"kind": "interval",
"unit": "day",
"value": 1
}
},
{
"type": "action:JavaScript",
"script": "const dateField = \"Notify On\";\nconst type = \"Task\";\nconst role = \"Reminder\";\n\nconst api = context.getService(\"targetprocess/api/v2\");\nconst items = await api.queryAsync(type, {\n where: `CustomValues.Date(\"${dateField}\")=DateTime.Today`,\n select: `{id,users:Assignments.Where(Role.Name=\"${role}\").Select({User.fullname,User.login})}`\n});\n\nreturn items\n .filter(item => item.users && item.users.length) // do nothing if there is noone assinged with specified role\n .map(item => {\n let mentions = item.users.map(user => {\n return `@user:${user.login}[${user.fullname}]`;\n }).join(\" and \");\n return {\n command: \"targetprocess:CreateResource\",\n payload: {\n resourceType: \"Comment\",\n fields: {\n General: { Id: item.id },\n Description: `A reminder for ${mentions}.<br/>To snooze: change \"${dateField}\" field to a later date.<br/>To stop: unassign yourself from the role ${role}.`\n }\n }\n };\n})"
}
]
Updated over 5 years ago