Send Custom Email Script

Hello guys, im trying to sendo a custom email via script , at the moment my script is :

 

import com.atlassian.jira.component.ComponentAccessor
import java.util.Map
import com.atlassian.jira.issue.AttachmentManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue

def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Gestor Imediato")

def recipient = issue.getCustomFieldValue(cf).getEmailAddress()

mail.setTo(recipient)

 

but i still getting error msg in bold lines, someone know the problem? ty already

1 answer

1 accepted

0 votes
Answer accepted
Derek Fields _RightStar_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 11, 2019

What error message are you getting? Is it possible that the custom field is empty and returning null. This would cause an error when you call getEmailAddress. You could write this line as 

def recipient = issue.getCustomFieldValue(cf)?.getEmailAddress()

This would return null to recipient if the field is empty. Then you would need to handle the null value.

Bu the way, getCustomFieldObjectByName is deprecated, so you might want to get into the habit of using getCustomFieldObjectsByName("Gestor Imediato")?.getAt(0), which does the same thing. 

RichardA July 6, 2020

Hi @Derek Fields _RightStar_ ,

i have one similar requirement, "need to send the notifications to users in a multi picker field

Thanks.

Vladislav
Contributor
March 31, 2021

Hi @RichardA 

Thy this:

final String cf = "UserPickerMultiSelect"
def customFieldManager = ComponentAccessor.customFieldManager
def cfObject = customFieldManager.getCustomFieldObjects(issue).find { it.name == cf }
def cfValList = issue.getCustomFieldValue(cfObject)

def emailAddr = cfValList*.getEmailAddress().join(",") // provides email address of users selected in UserPickerMultiSelect custom field.

Suggest an answer

Log in or Sign up to answer