Forums

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

Workaround to get a custom field value in a post function before issue is created

Alexandra Steinberg
April 20, 2018

Does anyone know how to retrieve the value of a custom field (String) in a custom script post function before the issue is actually created?

I am trying to add request participants based on the value of the custom field.

When I create a ticket with the LOB custom field value as CIO, it should add "alexandra" and "Service_Desk_Testing" as request participants.

However, when I created a ticket, it added "Service_Desk_Testing" and "Service_Desk_Testing2" as participants meaning it's not properly matching the value of the CIO field.

Does anyone know how I could retrieve the custom field value of the LOB field?

Thanks!

Here's what I have so far. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import java.util.ArrayList
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.index.IssueIndexManager
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def userManager = ComponentAccessor.getUserUtil()
def requestParticipantsField = customFieldManager.getCustomFieldObject("customfield_10001")
def lobField = customFieldManager.getCustomFieldObject("customfield_12345")
def issueManager = ComponentAccessor.getIssueManager()
def loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def getUsersAsArray = {
def users = ["Service_Desk_Testing"]
def lob = issue.getCustomFieldValue(lobField)
ArrayList<ApplicationUser> userList = new ArrayList<ApplicationUser>()
ArrayList<String> lobUserNames = new ArrayList<String>()

if (lob == 'CIO')
{ lobUserNames.add("alexandra") }
else {lobUserNames.add("Service_Desk_Testing2")}

lobUserNames.each {
def lobuser = userManager.getUserByName(it)
if (lobuser)
{ userList.add(lobuser)}
}//lobUserNames.each

users.each {
def user = userManager.getUserByName(it)
if(user)
{ userList.add(user) }
}//users.each

return userList
}//getUsersAsArray
MutableIssue myIssue = issue
ArrayList<ApplicationUser> applicationUsers = getUsersAsArray()
myIssue.setCustomFieldValue(requestParticipantsField, applicationUsers)

 

 

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
PD Sheehan
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 Champions.
July 6, 2020

HTTPBuilder already takes care of the Json parsing for you since you specified ContentType.JSON. You should not need JsonSlurper here.

But like you said, if the API is giving you a single item, then you can get the id for that item like this:

_userId = json[0].id
TAGS
AUG Leaders

Atlassian Community Events