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
email notification in Jira Service Desk. You can adapt it for other custom email use cases as well.
On this page: |
---|
Script parameters
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 two asset custom fields and only asset names are sent as email. Attributes can be fetched as well. For more information, refer to Sample Groovy Scripts.
Email is sent to multi-select users custom field values. To send a single user custom field (for example, reporter), please modify
getRecipientEmails
method. This returns one email, so there is no need to have a loop fortoEmails
.
Script functionality
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).
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.
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
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;