Send Email Notification to an Approver.

Purpose

Hosting

Compatible Tools

Purpose

Hosting

Compatible Tools

Notify an approver of a form awaiting their approval.

Cloud

Any scripting tool, such as JMWE, Power Scripts or Scriptrunner, that uses Groovy.

Script

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 import groovy.xml.MarkupBuilder //// When using this script in the script console uncomment the next line: def issueKey = 'ISSUE-1' //// When using this script in a transition uncomment the next line: // def issueKey = issue.key String formName = "Form Name" String fieldName = "Users Field" Closure sendNotification = { def user -> def writer = new StringWriter() def markupBuilder = new MarkupBuilder(writer) markupBuilder.div { p { // update url below: a(href: "http://mycloudjira.atlassian.net/issue/" + issueKey) span("Please check approval action for form " + formName) } } def htmlMessage = writer.toString() def textMessage = new XmlSlurper().parseText(htmlMessage).text() def resp = post("/rest/api/2/issue/" + issueKey + "/notify") .header("Content-Type", "application/json") .body([ subject: "Form Approval for issue: " + issueKey, textBody: textMessage, htmlBody: htmlMessage, to: [ reporter: false, assignee: false, watchers: false, voters: false, users: [[ accountId: user.id, //accountId: user.accountId, active: true ]] ] ]) .asString() } Closure getFormId = { def allProperties, String name -> for(def formProperty : allProperties){ if(formProperty.key.equals("proforma.forms")){ for(def p : formProperty.value.forms) { if(p.name.equals(name)){ return p.id } } } } } Closure<String> getFieldId = { def property, String name -> for (def question : property.value.design.questions){ if(name.equals(question.value.label)){ return question.key } } } Closure getUsersFieldValue = { def property, String name -> def fieldId = getFieldId(property, name) for (def answer : property.value.state.answers){ if(answer.key.equals(fieldId)){ return answer.value.users } } } def proformaPropertyKeys = [] def allIssueProperties = get('/rest/api/3/issue/' + issueKey + '/properties/') .header('Content-Type', 'application/json') .asObject(Map) if (allIssueProperties.status == 200){ for(def property : allIssueProperties.body.keys){ if(property.key.contains("proforma.forms")){ proformaPropertyKeys.add(property.key) } } } def formProperties = [] if(!proformaPropertyKeys.isEmpty()){ for(def key : proformaPropertyKeys){ def property = get('/rest/api/3/issue/' + issueKey + '/properties/' + key) .header('Content-Type', 'application/json') .asObject(Map) if(property.status == 200){ formProperties.add(property.body) } } } def formId if(!formProperties.isEmpty()){ formId = getFormId(formProperties, formName) for(def formProperty : formProperties){ if(formProperty.key.equals("proforma.forms" + ".i" + formId) && formProperty.value.schemaVersion.equals(8)){ def userList = getUsersFieldValue(formProperty, fieldName) //def userList = get('/rest/api/2/issue/' + issueKey).header('Content-Type', 'application/json').asObject(Map).body.fields.customfield_10036 if(userList.getClass().toString().contains("java.util.ArrayList")){ for(def user : userList){ sendNotification(user) } } else { sendNotification(userList) } } } }

Installation instructions

  • If used in a transition:

    • Comment line 3

    • Uncomment line 5

    • Change formName in line 7 to the name of the form you want to notify approvers about.

    • Change the url in line 16 to the url of your Jira cloud instance.

    • Change the notification message in line 17 to the message you want to send in the notification.

    • If using a ProForma multiselect/single select user field:

      • Change fieldName in line 8 to the name/label of the ProForma field with the approvers

    • If using Jira approvers field:

      • Comment line 8.

      • Comment line 35 and uncomment line 36.

      • Comment line 103 and uncomment line 104

      • Change the customfield_10000 value in line 104 to reflect the ID of your Jira approvers field (example: customfield_10123).

  • If used in the script console:

    • Change issueKey in line 3 to the issue that has the form you want to notify approvers about.

    • Change formName in line 7 to the name of the form you want to notify approvers about.

    • Change the url in line 16 to the url of your Jira cloud instance.

    • Change the notification message in line 17 to the message you want to send in the notification.

    • If using a ProForma multiselect/single select user field:

      • Change fieldName in line 8 to the name/label of the ProForma field with the approvers

    • If using Jira approvers field:

      • Comment line 8.

      • Comment line 35 and uncomment line 36.

      • Comment line 103 and uncomment line 104

      • Change the customfield_10000 value in line 104 to reflect the ID of your Jira approvers field (example: customfield_10123).

Possible use cases

Use this script to email completed forms to approvers, or other team members receiving notifications.Include a link to a completed form in an email.

Limitations

  • This script does not work with next-gen projects.

  • Notifications will only be sent if allowed by Jira’s notification scheme and the user’s notification preferences. To test this script, you may need to alter your preferences to allow notifications for changes you make to the issue, or set the script to run as “Scriptrunner add-on user”.

  • If using the Jira Approvers field, the script will not check to confirm that the form is present. You may want to configure this to fire after adding a specific form to ensure that the script does not arbitrarily send notifications even if the form hasn’t been added to the issue.

  • If the approver is listed in ProForma field, then the script may not work if the form is over 32 KB. You can limit the size of your forms by using multiple smaller forms, and by including character/word limits on your text fields.