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

Scriptrunner - Script Field - Custom Email

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

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

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?

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 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
Community showcase
Published in Trello

🏊‍♀️ Atlympic Event: Trello

Hello Community!  Quick disclaimer: We are running a contest on Community (The Atlympics!) from July 23rd - August 8th of 2021. If you are interested in participating in this contest (prizes! ...

82 views 0 8
Read article

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