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

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.