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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

0 votes
Answer accepted

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>")

thanks a lot. looks so clean.

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Asked in Jira Service Desk

Calling all Insight users, we need your help!

Hello Insight users,  As part of our (Mindville's) acquisition by Atlassian, our training team is looking to build some new Insight training materials. It would really helpful if you can ...

134 views 1 0
View question

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you