Forums

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

Scriptrunner - Script Field - Custom Email

Marco Hasenfratz
Contributor
July 14, 2021

Hi

i created a scripted field "Multi User Picker". This is working fine, and its filled with users as it should.

Now i try to setup a Post Function "Send Custom Email (Scriptrunner).

The issue is now, i can not choose this "Custom Field" in the "To issue fields" context...

I assume thats because the field is not a User Picker Typ, but a "Scripted Field" type.

Is there a solution, workaround for that?

Thank you in advance

Marco

 

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 15, 2021 edited

Hi @Marco Hasenfratz

You can filter the email from the Scripted Field by converting the Scripted Field into a List of Application Users for your requirement.

Below is a print screen of the Scripted Field configuration:-

scripted_field_config.png

In the sample print screen above, the name of the Scripted Field being used is Recipients.

Below is a code I have used for the Scripted Field:-

import com.atlassian.jira.component.ComponentAccessor

def groupManager = ComponentAccessor.groupManager
def groupA = groupManager.getGroup("jira-administrators")
groupManager.getUsersInGroup(groupA)

And for the post function, you will need to use the Custom script post-function

Below is a print screen of the Post-Function configuration:-

1) First add a Post-Function as shown in the image below:-

post_function_1.png

2) Select the Custom script post-function and click on the Add button shown below:-

post_function_2.png

3) Next, you will need to add a custom script to it, as shown below:-

post_function_3.png

Below is the sample post-function script for your reference:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.mail.Email
import com.atlassian.jira.user.ApplicationUser

def customFieldManager = ComponentAccessor.customFieldManager
def mailServerManager = ComponentAccessor.mailServerManager

def receipients = customFieldManager.getCustomFieldObjectsByName("Recipients")[0]
def applicationUsers = receipients.getValue(issue) as List<ApplicationUser>

def mailServer = mailServerManager.defaultSMTPMailServer

applicationUsers.each {
def emailAddress = it.emailAddress
def subject = "Sample Mail"
def body = "The is a test mail"
def email = new Email(emailAddress.toString())
email.setSubject(subject)
email.setBody(body)
email.setMimeType("text/html")
def threadClassLoader = Thread.currentThread().contextClassLoader
Thread.currentThread().contextClassLoader = mailServer.class.classLoader
mailServer.send(email)
Thread.currentThread().contextClassLoader = threadClassLoader
}

Please note, the sample scripts provided are not 100% exact to your environment. Hence, you will need to make the required modifications.

Once you have configured the post-function, publish the changes.

Below are some test print screens.

1) First, when the ticket is created, the Recipients is automatically filled with the users from the jira-administrators group as shown in the image below:-

issue_created.png

2) In this example, the Post-Function is added for the In Progress transition. So, once In Progress transition is triggered, the email is sent to the users.

issue_in_progress.png

3) The emails are successfully triggered as shown below:-

mail_triggered.png

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

Marco Hasenfratz
Contributor
July 19, 2021

Hi Ram, thank you very much... in my case, the reason for the scripted field is that i get the users based on e Elements Checklist, and the status there, here the code i already have:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.plugin.PluginAccessor
import com.atlassian.jira.user.ApplicationUser

import com.valiantys.software.elements.api.content.*;
import com.valiantys.software.elements.api.model.*;

PluginAccessor pluginAccessor = ComponentAccessor.getPluginAccessor()
Class panelContentServiceClass = pluginAccessor.getClassLoader().findClass("com.valiantys.software.elements.api.content.PanelContentService")
Class panelRefClass = pluginAccessor.getClassLoader().findClass("com.valiantys.software.elements.api.model.PanelRef")
Class attributeRefClass = pluginAccessor.getClassLoader().findClass("com.valiantys.software.elements.api.model.AttributeRef")
def userManager = ComponentAccessor.getUserManager()

def panelContentService = ComponentAccessor.getOSGiComponentInstanceOfType(panelContentServiceClass)

// Constants to customize
def PANEL_NAME = "Employee Checklist"
def CHECKBOX_DONE = "Done"
def CHECKBOX_REQ = "Required"
def USER_ATTRIBUTE = "Main responsibility"

// Define Elements panel
def elementsPanel = panelContentService.getPanel(issue, panelRefClass.byName(PANEL_NAME))

// Get Elements panel items
Set<ApplicationUser> usersSet = new HashSet<ApplicationUser>()

for (def panelItem : elementsPanel.getPanelItems()) {
def taskRequired = panelItem.getAttributeContent(attributeRefClass.byName(CHECKBOX_REQ))?.getValue()
def taskDone = panelItem.getAttributeContent(attributeRefClass.byName(CHECKBOX_DONE))?.getValue()
if (taskRequired) {
if(!taskDone) {
def owner = panelItem.getAttributeContent(attributeRefClass.byName(USER_ATTRIBUTE))?.getValue()
if(owner != null) {
usersSet.add(userManager.getUserByName(owner))
}
}
}
}

 

....in your sample, you get the users from a Jira Group (jira-administrators), do i get that right? Can i directly work with the "usersSet" in my case?

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 19, 2021 edited

Hi @Marco Hasenfratz

To answer your question, you should first get the list of users in the Scripted Field list and pass that value to a post-function or listener and trigger the email from there.

In your case, you could try something like:-

def customFieldManager = ComponentAccessor.customFieldManager

def users = customFieldManager.getCustomFieldObjectsByName("Users")[0]

def usersSet = issue.getCustomFieldValue(users) as Set<ApplicationUser>

usersSet.each {
def emailAddress = it.emailAddress
def subject = "Test"
def body = "This is a test message"
def email = new Email(emailAddress)
email.setSubject(subject)
email.setBody(body)
email.setMimeType("text/html")
def threadClassLoader = Thread.currentThread().contextClassLoader
Thread.currentThread().contextClassLoader = mailServer.class.classLoader
mailServer.send(email)
Thread.currentThread().contextClassLoader = threadClassLoader
}

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

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram 

Marco Hasenfratz
Contributor
August 18, 2021

Hi Ram,

thanks for the feedback, i tried the following:

import com.atlassian.mail.Email;
import com.atlassian.mail.server.MailServerManager;
import com.atlassian.mail.server.SMTPMailServer;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser

def customFieldManager = ComponentAccessor.customFieldManager

def users = customFieldManager.getCustomFieldObjectsByName("Action Required From")[0]

def usersSet = issue.getCustomFieldValue(users) as Set<ApplicationUser>

usersSet.each {
def emailAddress = it.emailAddress
def subject = "Test"
def body = "This is a test message"
def email = new Email(emailAddress)
email.setSubject(subject)
email.setBody(body)
email.setMimeType("text/html")
def threadClassLoader = Thread.currentThread().contextClassLoader
Thread.currentThread().contextClassLoader = mailServer.class.classLoader
mailServer.send(email)
Thread.currentThread().contextClassLoader = threadClassLoader
}

where "Action Required From" is the field which contains the "Users"

I'm not familliary with the email funcationality of jira - i get the error that "mailServer is not declared.

I assume i have to configure this things in Jira somehow?! Can you help me out here?

Thank you in advance

Marco

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
August 19, 2021

Hi @Marco Hasenfratz

So, this error is appearing because the Outgoing Mail Server has not been configured yet.

You can refer to this  Atlassian Documentation, where it provides the steps to configure the outgoing mail server.

I hope this helps to answer your question. :)

Thank you and Kind regards,

Ram

Marco Hasenfratz
Contributor
August 19, 2021

I thought so too. But the mail server is configured. And still I get this message when I run it: 

2021-08-19 13:31:46,354 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed on issue ISSP-2784 for user 'lih03'. View here....
groovy.lang.MissingPropertyException: No such property: mailServer for class: Script126
at Script126$_run_closure1.doCall(Script126.groovy:21)
at Script126.run(Script126.groovy:12)
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
August 19, 2021 edited

Hi @Marco Hasenfratz

Thank you for sharing your log. It provides a clearer picture.

After checking your latest code against the log, it appears that the Mail Server has not been declared in the code i.e.

def mailServerManager = ComponentAccessor.mailServerManager

def mailServer = mailServerManager.defaultSMTPMailServer

So your code to work, you should update it to:-

import com.atlassian.mail.Email
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

def customFieldManager = ComponentAccessor.customFieldManager

def mailServerManager = ComponentAccessor.mailServerManager

def mailServer = mailServerManager.defaultSMTPMailServer

def users = customFieldManager.getCustomFieldObjectsByName("Action Required From")[0]

def usersSet = issue.getCustomFieldValue(users) as Set<ApplicationUser>

usersSet.each {
def emailAddress = it.emailAddress
def subject = "Test"
def body = "This is a test message"
def email = new Email(emailAddress)
email.setSubject(subject)
email.setBody(body)
email.setMimeType("text/html")
def threadClassLoader = Thread.currentThread().contextClassLoader
Thread.currentThread().contextClassLoader = mailServer.class.classLoader
mailServer.send(email)
Thread.currentThread().contextClassLoader = threadClassLoader
}

Please give this a try.

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram 

Marco Hasenfratz
Contributor
August 19, 2021

Hi Ram, now its running fine!!

Thank you!

Kind Regards

Marco

Ricardo Martinez
Contributor
August 31, 2022

Hi

Does anyone have this core adapted to Jira Cloud?

 

Thanks in advance

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
atlassian, confluence, loom, atlassian intelligence, ai notes, ai-powered meeting notes, atlassian community events, ace, confluence ai, loom ai integration, ai note-taking, atlassian ai features, team '25, atlassian live learning, confluence automation

Unlock AI-powered meeting notes: Join our live learning session! 📹

Did you catch the news at Team ‘25? With Loom, Confluence, Atlassian Intelligence, & even Jira 👀, you won’t have to worry about taking meeting notes again… unless you want to. Join us to explore the beta & discover a new way to boost meeting productivity.

Register today!
AUG Leaders

Atlassian Community Events