Assign Project's Teams to a newly created item

📘

How to apply this rule?

910
[
  {
    "type": "source:targetprocess:EntityChanged",
    "entityTypes": [
      "UserStory",
      "Bug",
      "Task",
      "Feature",
      "Epic",
      "PortfolioEpic"
    ],
    "modifications": {
      "created": true,
      "deleted": false,
      "updated": [
        "Project"
      ]
    }
  },
  {
    "type": "action:JavaScript",
    "script": "const assignableId = args.ResourceId;\nconst projectId = args.Current.Project.Id;\n\nconst api = context.getService(\"targetprocess/api/v2\");\nconst teams = await api.queryAsync(\"TeamProject\", {\n  select: \"team.id\",\n  where: \"project.id = \" + projectId\n});\n\nreturn teams.map(teamId => {\n  return {\n    command: \"targetprocess:CreateResource\",\n    payload: {\n      resourceType: \"TeamAssignment\",\n      fields: {\n        Assignable: { Id: assignableId },\n        Team: { Id: teamId }\n      }\n    }\n  }\n});"
  }
]