{
  "id": "b530df95-bce1-3fd9-a852-f01f3590a167",
  "title": "Problem Notification by Owner",
  "tasks": {
    "get_owners": {
      "name": "get_owners",
      "input": {
        "entityIds": "{{ event()[\"affected_entity_ids\"] }}"
      },
      "action": "dynatrace.ownership:get-ownership-from-entity",
      "position": {
        "x": 0,
        "y": 1
      },
      "description": "Retrieves owners from Entities and Team identifiers.",
      "predecessors": []
    },
    "send_message": {
      "name": "send_message",
      "input": {
        "message": "**Problem:** {{ event()['display_id']  }}\n\n{{ event()['event.name'] | default('Not available.', true)  }}\n\n**Impacting:**\n\n{{ event()['dt.davis.impact_level'] | join('\\n')  }}\\n\\n\n\n**Details:**\n\n{{ event()['event.description'] | default('Problem details not available.', true)  }}\n\n**More Info:**\n\n{% if event()['event.id'] | default(false) %}\n[👉 View Problem]({{environment().url}}/ui/apps/dynatrace.davis.problems/problem/{{ event()['event.id'] }})\n{% endif %}\n",
        "connectionId": "{{ connection('app:dynatrace.msteams:connection',  _.team ) }}"
      },
      "action": "dynatrace.msteams:send-message",
      "position": {
        "x": 0,
        "y": 3
      },
      "conditions": {
        "states": {
          "get_team_names": "OK"
        }
      },
      "withItems": "team in {{result('get_team_names').teams}}",
      "concurrency": 1,
      "description": "Send messages and Adaptive Cards to Microsoft Teams channels",
      "predecessors": [
        "get_team_names"
      ]
    },
    "get_team_names": {
      "name": "get_team_names",
      "input": {
        "script": "// optional import of sdk modules\nimport { execution, result } from '@dynatrace-sdk/automation-utils';\n\nexport default async function () {\n\n  var r = await result('get_owners');\n  //const teams = r.owners.map(owner => owner.name)\n  const teams = new Array();\n  \n  r.owners.forEach(owner => {\n    teams.push(owner.name);\n  });\n  \n  const uniqueTeams = [...new Set(teams)];\n  console.log(uniqueTeams)\n\n  return { teams: uniqueTeams };\n}"
      },
      "action": "dynatrace.automations:run-javascript",
      "position": {
        "x": 0,
        "y": 2
      },
      "conditions": {
        "custom": "{{ result('get_owners').owners | length > 0 }}",
        "states": {
          "get_owners": "OK"
        }
      },
      "description": "Run custom JavaScript code.",
      "predecessors": [
        "get_owners"
      ]
    },
    "run_javascript_1": {
      "name": "run_javascript_1",
      "input": {
        "script": "// optional import of sdk modules\nimport { execution } from '@dynatrace-sdk/automation-utils';\n\nexport default async function () {\n  // your code goes here\n  // e.g. get the current execution\n  const ex = await execution();\n  console.log('Event', ex.event());\n\n  return { event: ex.event };\n}"
      },
      "action": "dynatrace.automations:run-javascript",
      "position": {
        "x": -1,
        "y": 2
      },
      "conditions": {
        "custom": "{{ result('get_owners').owners | length == 0 }}",
        "states": {
          "get_owners": "OK"
        }
      },
      "description": "Run custom JavaScript code.",
      "predecessors": [
        "get_owners"
      ]
    },
    "add_problem_comment": {
      "name": "add_problem_comment",
      "input": {
        "script": "// optional import of sdk modules\nimport { execution, result } from '@dynatrace-sdk/automation-utils';\nimport { problemsClient } from '@dynatrace-sdk/client-classic-environment-v2';\n\nexport default async function () {\n  const ex = await execution();\n  console.log('Event', ex.event()[\"event.id\"]);\n  const r = await result('get_team_names');\n  const teams = r.teams\n  console.log(teams)\n  const data = await problemsClient.createComment({\n    problemId: ex.event()[\"event.id\"],\n    body: { message: \"Problem sent to Microsoft Teams: \" + teams },\n  });\n}"
      },
      "action": "dynatrace.automations:run-javascript",
      "position": {
        "x": 0,
        "y": 4
      },
      "conditions": {
        "states": {
          "send_message": "OK"
        }
      },
      "description": "Run custom JavaScript code.",
      "predecessors": [
        "send_message"
      ]
    },
    "ingest_business_event_1": {
      "name": "ingest_business_event_1",
      "input": {
        "from": "JSON",
        "json": "{ \n  \"problem_id\": \"{{ event()[\"display_id\"] }}\",\n  \"problem\": \"{{ event()[\"event.name\"] }}\",\n  \"affected_entity_ids\": {{ event()[\"affected_entity_ids\"] | to_json }}  \n}",
        "eventType": "no_owner_found",
        "eventProvider": "wf_owner_notification"
      },
      "action": "dynatrace.biz.explore:ingestbizevent",
      "position": {
        "x": -1,
        "y": 3
      },
      "conditions": {
        "states": {
          "run_javascript_1": "OK"
        }
      },
      "description": "This workflow action helps you ingest Business Events into GRAIL.",
      "predecessors": [
        "run_javascript_1"
      ]
    }
  },
  "description": "",
  "actor": "2a3563cd-9277-40ce-a9e9-5cb26667dca7",
  "owner": "2a3563cd-9277-40ce-a9e9-5cb26667dca7",
  "ownerType": "USER",
  "isPrivate": false,
  "trigger": {
    "eventTrigger": {
      "isActive": true,
      "filterQuery": "event.kind == \"DAVIS_PROBLEM\" AND event.status == \"ACTIVE\" AND (event.status_transition == \"CREATED\" OR event.status_transition == \"UPDATED\" OR event.status_transition == \"REOPENED\") AND (event.category == \"MONITORING_UNAVAILABLE\" OR event.category == \"AVAILABILITY\" OR event.category == \"ERROR\" OR event.category == \"SLOWDOWN\" OR event.category == \"RESOURCE_CONTENTION\" OR event.category == \"CUSTOM_ALERT\" OR event.category == \"INFO\")",
      "uniqueExpression": "{{ event()[\"event.id\"] }}-{{ \"open\" if event()[\"event.status_transition\"] in (\"CREATED\", \"UPDATED\", \"REOPENED\", \"REFRESHED\") else \"resolved\" }}-{{ event()[\"dt.davis.last_reopen_timestamp\"] }}",
      "triggerConfiguration": {
        "type": "davis-problem",
        "value": {
          "categories": {
            "info": true,
            "error": true,
            "custom": true,
            "resource": true,
            "slowdown": true,
            "availability": true,
            "monitoringUnavailable": true
          },
          "entityTags": {},
          "customFilter": "",
          "onProblemClose": false,
          "entityTagsMatch": "all"
        }
      }
    }
  },
  "schemaVersion": 3,
  "result": null,
  "input": {},
  "hourlyExecutionLimit": 1000,
  "type": "STANDARD"
}