Page tree
Skip to end of metadata
Go to start of metadata

In this example, we will define a post function to send a custom Approve e-mail with Jira Service Desk. You can also use it as an example to send custom e-mails with Jira. Some notes about the script:

  • Please change ENVIRONMENT SPECIFIC VALUES according to your setup.
  • issue object is already attached to script, you can use it directly.
  • assetCustomFieldAndValueList is attached to script which holds asset custom field values with detailed assets information. See AssetCustomFieldAndValue for class details.
  • Screen has 2 asset custom fields and only asset names are sent as e-mail. Attributes can be fetched as well, for more info please see Sample Groovy Scripts 
  • E-mail is sent to multi-select users custom field values, to send a single user custom field (i.e. reporter), please modify getRecipientEmails method: returns one e-mail and  no need to have a loop for "toEmails".


import com.atlassian.jira.mail.Email
import com.atlassian.jira.user.ApplicationUser
import groovy.transform.Field
import inventoryplugin.entity.JipInventory
import inventoryplugin.workflow.function.genericscript.dto.AssetCustomFieldAndValue
import org.apache.commons.lang3.StringUtils

import java.text.DateFormat
import java.text.SimpleDateFormat

/********** ENVIRONMENT SPECIFIC VALUES: Change according to your environment ********/
@Field String portalId = '2';
@Field String approvalsCustomFieldId = 'customfield_10402';
@Field String assetCustomFieldName_1 = 'Assets';
@Field String assetCustomFieldName_2 = 'Assets2';
/*************************************************************************************/

String getJiraBaseUrl() {
    return ComponentAccessor.getApplicationProperties().getString("jira.baseurl")
}

String getIntro() {
    return '<div>This request created by ' + issue.getReporter().getDisplayName() + ' is awaiting approval.</div>'
}

String getCreateDateFormatted() {
    DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");
    return dateFormat.format(issue.getCreated());
}

String getAssetCustomFieldText(fieldName) {
    List<String> assetNames = new ArrayList<>();
    for (AssetCustomFieldAndValue assetCustomFieldAndValues : assetCustomFieldAndValueList) {
        if (fieldName == assetCustomFieldAndValues.assetCustomField.fieldName) {
            for (JipInventory inventory : assetCustomFieldAndValues.getAssetList()) {
                assetNames.add(inventory.name) // ' #' + inventory.ID
            }
        }
    }
    return StringUtils.join(assetNames, ', ')
}

static String getField(fieldName, fieldValue) {
    return '<div style="padding-top: 20px;">' +
            '<div style="font-weight: bold;">' + fieldName + '</div>' +
            '<div>' + fieldValue + '</div>' +
            '</div>'
}

String getViewRequest() {
    def url = getJiraBaseUrl() + '/servicedesk/customer/portal/' + portalId + '/' + issue.getKey() + '?sda_source=notification-email';
    return '<a href="' + url + '">View request</a>';
}

// Create an email
def sendEmail(String emailAddr, String subject, String body) {
    def mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
    if (mailServer) {
        def email = new Email(emailAddr)
        email.setSubject(subject)
        email.setBody(body)
        email.setMimeType("text/html");
        mailServer.send(email)
        log.debug("Mail sent")
    } else {
        log.warn("Please make sure that a valid mailServer is configured")
    }
}

List<String> getRecipientEmails() {
    List<String> emails = new ArrayList<>();
    def cField = customFieldManager.getCustomFieldObject(approvalsCustomFieldId)
    Collection<ApplicationUser> usersList = issue.getCustomFieldValue(cField);
    for (ApplicationUser applicationUser : usersList) {
        emails.add(applicationUser.getEmailAddress());
    }
    return emails;
}

String messageBody = '<div style="font-family:verdana; font-size:14px">'

messageBody += getIntro();

messageBody += getField('Summary', issue.getSummary());
messageBody += getField('Description', StringUtils.replace(issue.getDescription(), '\n', '<br />'));
messageBody += getField(assetCustomFieldName_1, getAssetCustomFieldText(assetCustomFieldName_1));
messageBody += getField(assetCustomFieldName_2, getAssetCustomFieldText(assetCustomFieldName_2));
messageBody += getField('Created', getCreateDateFormatted());

messageBody += '<div style="padding-top: 20px">';
messageBody += '<span style="font-size: 16px; font-weight: bold;">' + getViewRequest() + '</span> to approve or decline.';
messageBody += '</div>';

messageBody += '<div style="padding-top: 20px">';
messageBody += '<span style="color:#7a869a; font-size:12px">Help Center, powered by <a href="' + getJiraBaseUrl() + '/servicedesk">Jira Service Desk</a>, sent you this message.</span>';
messageBody += '</div>';

messageBody += '</div>';

List<String> toEmails = getRecipientEmails();
for (String oneEmail : toEmails) {
    sendEmail(oneEmail, issue.getKey() + ' ' + issue.getSummary(), messageBody);
}

return messageBody;

E-mail sent by the script:

  • No labels