Inherit Initial Estimate from Epic to Portfolio Epic

📘

How to apply this rule?

When a Initial estimate id made in Epic, then inherit it to Portfolio Epic

1129
[
  {
    "type": "source:targetprocess:EntityChanged",
    "entityTypes": [
      "epic"
    ],
    "modifications": {
      "created": true,
      "deleted": true,
      "updated": [
        "InitialEstimate",
        "PortfolioEpic"
      ]
    }
  },
  {
    "type": "action:JavaScript",
    "script": "let peIds = [];\nif (args.Current && args.Current.PortfolioEpic) { peIds.push(args.Current.PortfolioEpic.Id); }\nif (args.Previous && args.Previous.PortfolioEpic) { peIds.push(args.Previous.PortfolioEpic.Id); }\nif (!peIds.length) { return }\n\nconst api = context.getService(\"targetprocess/api/v2\");\nconst epics = await api.queryAsync(\"Epic\", {\n  select: \"{pe:PortfolioEpic.Id,initialEstimate}\",\n  where: \"PortfolioEpic.Id in \" + JSON.stringify(peIds)\n}) || [];\n\nreturn peIds.map(peId => {\n  let effort = epics\n    .filter(epic => epic.pe == peId)\n    .map(epic => epic.initialEstimate)\n    .reduce((prev, next) => prev + next, 0);\n  return {\n    command: \"targetprocess:UpdateResource\",\n    payload: {\n      resourceType: \"PortfolioEpic\",\n      resourceId: peId,\n      fields: {\n        InitialEstimate: effort\n      }\n    }\n  };\n});"
  }
]