Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

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

Scriptrunner Jobs: Get customfield value of currently found issues

Lars Swart
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.
February 24, 2021

Hi,

 

I have a Scriptrunner job that runs every 24h. If the JQL Statement finds issues, for everyone of them an email should be send. That works fine for me.

 

But every issue has a customfield with a user stored within. 

 

How can I get this username for the issue that the jql statement just found?

 

I got this code, but here I need to specify the issue myself.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.Project


def issueManager = ComponentAccessor.issueManager
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11402")
def issue = issueManager.getIssueByCurrentKey("PER-289")
def value = (String) issue.getCustomFieldValue(customField)

log.warn(value)

 

And the second question is:

How do I get the email address from the user I just found in the customfield?

 

 

For project role users I used this very good working code. But the role stay the same over the whole project. So that was "easy".

// Get all the mail addresses from the role members of a project
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def projectRole = projectRoleManager.getProjectRole("Sales")
def project = ComponentAccessor.getProjectManager().getProjectObjByKey("PER")
def usersInRole = projectRoleManager.getProjectRoleActors(projectRole, project).getApplicationUsers()*.emailAddress.join(",")

 

Thanks for reading my stuff :) 

2 answers

1 accepted

1 vote
Answer accepted
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2021

Hi @Lars Swart I guess your job contains JQL? Or how do you collect the issues? I think you need

  1. select issues using JQL
  2. go through the issues selected by JQL
  3. get user from custom field
  4. get its email address

Am I correct? Can you share your job's script?

Lars Swart
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.
February 25, 2021

Hi Martin,

 

Here is the JQL Statement which selects the issues

 

project = PER AND issuetype = Contract AND status = "7-18 months" AND "Real Startdate" <= -68w

 

And here the current code, which works fine. But I also want to inform the users from the customfield with this mail

 

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript
import com.atlassian.jira.issue.issuetype.IssueType
import javax.ws.rs.core.Response
import javax.ws.rs.core.MultivaluedMap
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.config.IssueTypeManager;
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.mail.Email
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequest;

// Get all the mail addresses from the role members of a project
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def projectRole = projectRoleManager.getProjectRole("Sales")
def project = ComponentAccessor.getProjectManager().getProjectObjByKey("PER")
def usersInRole = projectRoleManager.getProjectRoleActors(projectRole, project).getApplicationUsers()*.emailAddress.join(",")


// Create an email
def emailAddr = usersInRole
def subject = "It's been 17 months"
def body = "Hi, it's been 17 months since the start.\n\nAll Details can be found here:\n\nhttps://jira.xxx.de/browse/${issue.key} - ${issue.summary}. \n\n\"

def sendEmail(String emailAddr, String subject, String body) {
SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer();
if (mailServer) {
Email email = new Email(emailAddr);
email.setSubject(subject);
email.setBody(body);
mailServer.send(email);
} else {
// Problem getting the mail server from JIRA configuration, log this error
}
}

sendEmail (emailAddr, subject, body)
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2021

Ok, I think following code is sufficient :). I added few comments too..

def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11402") // getting custom field of Single user picker
def user = issue.getCustomFieldValue(customField) // getting value of Single user picker field as ApplicationUser (https://docs.atlassian.com/software/jira/docs/api/7.1.0/com/atlassian/jira/user/ApplicationUser.html)
def userEmailAddr = user.getEmailAddress()
Like Vladislav likes this
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2021

Hi @Lars Swart it is only static check error. It occurs because you are not using Java "types" in groovy code. It should not be problem. You can save script with these errors and test it.

Lars Swart
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.
February 25, 2021

Hi Martin,

 

It works :) Thank you soooo much.

Like 2 people like this
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2021

Great, you are welcome :)

Like johnson reddy likes this
Vladislav
Contributor
March 31, 2021

Hi @Martin Bayer _MoroSystems_ s_r_o__ 

Please advise how do you get emails from custom field of Multiple user picker?

 

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

recipients= cfValList.getEmailAddress()

 

Unfortunately for it did not work:

Script console script failed: groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.getEmailAddress() is applicable for argument types: () values: [] at Script1523.run(Script1523.groovy:27)

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 31, 2021

Hi @Vladislav could you create a new topic, so this ONE topic remains as clear as possible? Just feel free to mention me and I will try to get back to you ASAP...

0 votes
Lars Swart
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.
February 25, 2021

Hi Martin

thank you for your kind support.

I added 

import com.atlassian.jira.user.ApplicationUser

and your code to my job script. But I get an error.

error.jpg

 

I use JIRA 8.2.2. - the current documentation version for this version seems to be the same as the one you linked.

https://docs.atlassian.com/software/jira/docs/api/8.5.12/

 

So I think the name of the method is not the problem.

 

Have you a suggestion?

Suggest an answer

Log in or Sign up to answer