Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

I am looking for scriptrunner code to add the multi select fields value in a custom email

tazeenn April 25, 2019

I have created a custom email post function to send an email to approvers for approving an access. This access field is a multiselect field in JIRA.

How to add the script to display multi-select field value on  email. 

Here is my email code that works fine

import com.atlassian.jira.mail.Email
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.ComponentManager

def groupManager = ComponentAccessor.getComponent(GroupManager.class)
def groupUsers = groupManager.getUsersInGroup("NPPMO-PRV-APPROVAL-GROUP")
def baseurl = ComponentAccessor.getApplicationProperties().getString("jira.baseurl")
//Email body template
def body=" Hello, <br> <br> Privilege Access Approvers, <br> <br> Please note that a new member account request has been created and Privielege Access is requested. Please log in to JIRA to review the details and click on Approve or Reject transition accordingly"
body+="<br><br>The link for Ticket details in Jira is provided below. <br> <br> Thanks. <br><br><br><br>"
body+="--------------------------------------------------------<br>"
body+="<br>$issue.key<br>"
body+="--------------------------------------------------------<br>"
body+="<br>Summary: ${issue.summary}"
body+="<br>Issue Key: <a href=$baseurl/browse/$issue.key> ${issue.key}</a>"
body+="<br>URL: ${baseurl}/browse/$issue.key"
//body+="<br>Project:$issue.project.name"
body+="<br>Issue Type: $issue.issueType.name"
body+="<br>Reporter: $issue.reporter.displayName"
body+="<br>Assignee: $issue.assignee.displayName"
//body+="<br>PrivilegeAccess: $issue.privilegeaccess.displayName"

body+="<br>--------------------------------------------------------<br>"
//Email subject
def subject="($issue.key) ${issue.summary} | Your Approval Required for Privilege Access"

//def thetext = ""
groupUsers.each() {
// thetext += it.getDisplayName() + it.getEmailAddress()+ "(" + it.getName() + ")<br>"
sendEmail(it.getEmailAddress(), subject, body)
}

// send email
def sendEmail(String emailAddr, String subject, String body) {
def mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
if (mailServer) {
Email email = new Email("temporary@example.com")
email.setTo(emailAddr)
//email.setReplyTo("dojosupport@publicis.sapient.com")
// email.setFrom("dojosupport@publicis.sapient.com")

//log.error "Email address:" + emailAddr
email.setSubject(subject)
//log.error "Subject: " + subject
email.setBody(body)
//log.error "Body: " + body
email.setMimeType("text/html")
mailServer.send(email)
//log.error("Mail sent")

} else {
log.error("Please make sure that a valid mailServer is configured")
}
}
//return thetext

-------------------

Just want to display the multiselect field value to show the tool list that needs access approval

 

 

 

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 29, 2019

To get custom field value you must

  1. first, get a custom field object
  2. then get the value from the issue (array of "options)
  3. then collect the value from those options
  4. and finally, join the elements in the array for the email body 
def customFieldManager = ComponentAccessor.customFieldManager
def multiSelectField = customFieldManager.getCustomFieldObjectByName("name of your multi select")
//def multiSelectField = customFieldManager.getCustomfFieldObject(123456) //id of the custom field

customFieldValues = issue.getCustomFieldValue(multiSelectField).collect{it.value}.join("<br>"

Also, did you know that you can do this in groovy? Makes the code/email body much easier to read and visualize the final format.

def body="""
   Hello,
   Privilege Access Approvers,
   Please note that a new member account request has been created and Privielege Access is requested. Please log in to JIRA to review the details and click on Approve or Reject transition   accordingly
   The link for Ticket details in Jira is provided below.

   Thanks.

   --------------------------------------------------------
   $issue.key
   --------------------------------------------------------
   Summary: ${issue.summary}
   Issue Key: <a href=$baseurl/browse/$issue.key> ${issue.key}</a>
   URL: ${baseurl}/browse/$issue.key
   Issue Type: $issue.issueType.name
   Reporter: $issue.reporter.displayName
   Assignee: $issue.assignee.displayName

   --------------------------------------------------------
""".stripIndent().replaceAll("\n","<br>")
tazeenn April 29, 2019

thanks a lot. looks so clean.

I will follow the steps above and try to add in email body. thankyou

TAGS
AUG Leaders

Atlassian Community Events