Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Panel
panelIconIdatlassian-info
panelIcon:info:
panelIconText:info:
bgColor#F4F5F7

This page is about Assets & Inventory Plugin for Jira DC. Using Cloud? Click here.

This script defines a post function to send a custom Approve e-mail with email notification in Jira Service DeskManagement. You can also use it as an example to send custom e-mails with Jira. adapt it for other custom email use cases as well.

On this page:

Table of Contents
minLevel1
maxLevel6
outlinefalse
styledefault
typelist
printabletrue

Script parameters

Some notes about the script:

  • Please

    change ENVIRONMENT

    change ENVIRONMENT SPECIFIC VALUES according to your setup.

  • issue object is already attached to the script, you can use it directly.

  • assetCustomFieldAndValueList is attached to script which holds asset custom field values with detailed assets information. 

    See 

    See AssetCustomFieldAndValue

     for

     for class details.

  • Screen has

    2

    two asset custom fields and only asset names are sent as

    e-mail

    email. Attributes can be fetched as well

    , for more info please see Sample Groovy Scripts E-mail

    . For more information, refer to Sample Groovy scripts.

  • Email is sent to multi-select users custom field values

    , to

    . To send a single user custom field (

    i.e.

    for example, reporter), please

    modify getRecipientEmails

    modify getRecipientEmails method

    :

    . This returns one

    e-mail and 

    email, so there is no need to have a loop for

    "toEmails".

    toEmails.

Script functionality

  1. Retrieves Data:

    • Gathers email addresses from the approval custom field.

    • Extracts information from the issue object (summary, description, creation date).

    • Fetches asset names from relevant custom fields (if configured).

  2. Builds Email Body:

    • Constructs a well-formatted HTML email body with:

      • Introduction mentioning the reporter and request creation.

      • Details of the request (summary, description, assets, created date).

      • A link to view and approve/decline the request in the Jira Service Desk portal.

      • Signature mentioning Jira Service Management.

  3. Sends Email:

    • Loops through retrieved email addresses.

    • Sends an email with the request key, summary as subject, and constructed message body to each address.

Script code

Code Block
languagegroovy
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;

Email example

...