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

Email this issue to participants

Martin Kohout April 4, 2023

Dear Community,

 

could some1 please help me. We are trying to sent a notification to participants. We already have a script to add participants to issue when field contains value.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import java.util.ArrayList
import com.atlassian.jira.user.ApplicationUser
import org.apache.log4j.Logger
//def log = Logger.getLogger("com.acme.XXX")

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def REQUEST_PARTICIPANTS_FIELD = "customfield_10101"
def REQUEST_CURRENCY_FIELD = "customfield_11800"
//def PRIMARY_USER_FIELD = "customfield_10913"

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager issueManager = ComponentAccessor.getIssueManager()

MutableIssue myIssue = issue

// For testing in the Script Console, hard-code an issue object.
//MutableIssue myIssue = issueManager.getIssueObject("SFIN-24")

// Get custom field objects.
def requestParticipantsField = customFieldManager.getCustomFieldObject(REQUEST_PARTICIPANTS_FIELD)
def requestCurrencyField = customFieldManager.getCustomFieldObject(REQUEST_CURRENCY_FIELD)
def requestCurrencyFieldValue = myIssue.getCustomFieldValue(requestCurrencyField)


if ("${requestCurrencyFieldValue}" != 'CZK')
{

// add defined users as participants
ApplicationUser user_a = ComponentAccessor.getUserManager().getUserByName("annawais")
ApplicationUser user_b = ComponentAccessor.getUserManager().getUserByName("jhobziko")

// Add initial Reporter to array.
ArrayList<ApplicationUser> participants = []
participants.add(user_a)
participants.add(user_b)

    // Update the Request Participants field.
    myIssue.setCustomFieldValue(
        requestParticipantsField,
        participants
    )

    // Store to database.
    try {
        issueManager.updateIssue(
            currentUser,
            myIssue,
            EventDispatchOption.ISSUE_UPDATED,
            false
        )
    } catch (Exception e) {
        log.debug "Exception: " + e
    }
}
Anyone have an idea how to add an email this issue to this script?
Thank you
KD
Martin

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Ram Kumar Aravindakshan _Adaptavist_
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 6, 2023

Hi @Martin Kohout

For your requirement, I suggest looking at this Adaptavist Library Example which uses the REST Endpoint to get the email addresses of the participants.

Once the Rest Endpoint is set, you can run something like this to send the emails:-

import com.adaptavist.hapi.jira.issues.Issues
import com.atlassian.jira.component.ComponentAccessor
import com.adaptavist.hapi.jira.mail.Mail
import groovy.json.JsonSlurper

final def restEndpointName = 'getParticipantsEmail'

def applicationProperties = ComponentAccessor.applicationProperties
def baseUrl = applicationProperties.getString('jira.baseurl')
def issue = Issues.getByKey('ST-3')

def hostUrl = "${baseUrl}/rest/scriptrunner/latest/custom/${restEndpointName}?issueKey=${issue.key}"


Mail.send {
def response = hostUrl.toURL().text
def json = new JsonSlurper().parseText(response)
def artifacts = json.collect().sort()
setHtml()
setTo(artifacts.toString().replace('[','').replace(']','').trim())
setFrom('reminder@mail.com')
setFromName('Jira')
setSubject('Reminder')
setBody("<html><h3>Summary: ${issue.summary}</h3><br/><h3>Description: ${issue.description}</h3>")
}

 Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

For more information on how to use the Mail class, please refer to the ScriptRunner HAPI Documentation.

Please also ensure that you upgrade your ScriptRunner to the latest release, i.e. 7.13.0 to be able to use HAPI.

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

0 votes
Levente Réti April 4, 2023

Hi Martin.


as I see, you are already firing an ISSUE_UPDATED event; therefore you can configure a notification to be sent out in your JIRA notification scheme.

Or if you would like to have more customization options, you can try the Email This Issue app.

If you are already trying that app and would like to keep everything in script you are going to need the EmailService Interface: com.metainf.jira.plugin.emailissue.api.EmailService

This Interface has a sendEmail method that requires an EmailDefinition as a parameter that contains all the necessary data for your email such as recipients subject and email body.

This definition uses String Lists to store the recipients, so you would need to extract the email addresses from each of your participants.

You can see the full documentation of this feature here.

Cheers,
Levi

TAGS
AUG Leaders

Atlassian Community Events