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.
×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 :)
Hi @Lars Swart I guess your job contains JQL? Or how do you collect the issues? I think you need
Am I correct? Can you share your job's script?
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Martin,
It works :) Thank you soooo much.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Great, you are welcome :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.