Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner Listener to send email notification to custom people picker field

kspeight October 3, 2024

I am trying to create a Scriptrunner Listener that will send an email notification to the user that is in a custom people picker field called "SIM Champion".   I have tried multiple options but cannot get the notification to be sent.   If I replace this custom field with a standard field like Assignee or Reporter then the notification gets sent.  I can't figure out what the issue is with the custom people picker field.   The following is my latest script.  Multiple users can be selected in the SIM Champion field but even if I restrict it to one selection I still receive errors.

 

 

{
    final summary = issue.fields.summary.toString();
    final issueKey = issue.key;

    final simChampionFieldId = "customfield_11084";

    final simChampionaccountId = issue.fields[simChampionFieldId]?.accountId?.toString();

    // Prepare the notification email with dynamic issue key and summary
    def resp = post("/rest/api/3/issue/${issueKey}/notify")
        .header("Content-Type", "application/json")
        .body([
                subject: "New SIM Champion Assignment for Issue ${issueKey}",
                htmlBody: """<p>Hello,</p>
                         <p>Please be advised that you have been assigned as SIM Champion for the issue ${issueKey}.</p>
                         <p>Issue Summary: ${summary}</p>
                         <p>Thank you,<br>Your Jira Team</p>""",
                 to: [
                        users: [
                                [accountId: simChampionaccountId],
                        ]
                ]
        ])
        .asString();
}

1 answer

0 votes
Marcelo Ulloa
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 3, 2024

Hello

 

The reporter and assigned fields are handled differently than a multiple selector, I leave you the script that can work with several. And add some loggers for better visualization in case of error

Just change the custom field ID and KEY


final issueKey = "PROJ-123"; // Replace this with the correct issue key

// Fetch issue details including summary and custom field (SIM Champion)
def issueResponse = get("/rest/api/3/issue/${issueKey}?fields=summary,customfield_10715")
    .header('Content-Type', 'application/json')
    .asString()

if (issueResponse.status != 200) {
    logger.error("Error fetching issue details. Status: ${issueResponse.status}")
    return
}

// Parse the response to get the issue's summary and the SIM Champion field
def issueDetails = new groovy.json.JsonSlurper().parseText(issueResponse.body)
def summary = issueDetails.fields.summary.toString()
def simChampionUsers = issueDetails.fields.customfield_10715 // This is a list of users

// Check if there are any users in the SIM Champion field
if (simChampionUsers) {
    // Iterate through each user and send a notification
    simChampionUsers.each { user ->
        def simChampionaccountId = user.accountId.toString();
       
        // Send notification email to each user in SIM Champion
        def resp = post("/rest/api/3/issue/${issueKey}/notify")
            .header("Content-Type", "application/json")
            .body([
                    subject: "New SIM Champion Assignment for Issue ${issueKey}",
                    htmlBody: """<p>Hello,</p>
                                 <p>You have been assigned as SIM Champion for issue ${issueKey}.</p>
                                 <p>Issue Summary: ${summary}</p>
                                 <p>Thank you,<br>Your Jira Team</p>""",
                     to: [
                            users: [
                                    [accountId: simChampionaccountId],
                            ]
                    ]
            ])
            .asString();
       
        if (resp.status != 204) {
            logger.error("Failed to send notification to user ${simChampionaccountId}. Response: ${resp.status}")
        } else {
            logger.info("Notification successfully sent to ${simChampionaccountId}")
        }
    }
} else {
    logger.error("No users found in the SIM Champion field for issue ${issueKey}")
}

 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events