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
2 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.
June 10, 2021

Since each field update event should trigger a change in the summary, I think the best solution is to store your script in your script root and apply the same script field in each field.

The alternative, is to force users to fill in the fields in a certain order and trigger the update on the last one only. I think that's even less elegant

So what I recommend is:

Initializer:

getFieldById('summary').setReadOnly(true).setDescription("""<span class="greenText">Populated automatically from Pod, Name and Role</span>""")

Now, your Pod field looks like a scriptrunner issue picker field, so I'd recommend the following in a script file (use script editor to create this):

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import com.atlassian.jira.component.ComponentAccessor
@BaseScript FieldBehaviours fieldBehaviours

def pod = getFieldByName("Pod").value ?: 'podTbd'
def name = getFieldByName("Name").value ?: 'nameTbd'
def role = getFieldByName("Role").value ?: 'roleTbd'
def podIssue = ComponentAccessor.issueManager.getIssueObject(pod as String)
def summary = getFieldById("summary")

summary.setFormValue("$podIssue.summary - $name - $role")

Add that script file to each of your fields with the method "run"

Brian LeTourneau
Contributor
June 10, 2021

This is brilliant Peter!

I knew I was on the right track but you took it over the finish line.  I like how concise your solution is.  I can easily understand what's going on here.

Having this as 1 script file and applying this server-side script is convenient.

I see how you are getting the Pod field (which is a ScriptRunner issue picker field, correct) as a string and then getting the Issue Object from that.  That allows us to then grab the summary of that Pod object..which is the info we want.  Very clever.

I was struggling with this syntax too ("$podIssue.summary - $name - $role") which now I see how it works.  I can call the different variables in just 1 parenthesis.

Like # people like this
TAGS
AUG Leaders

Atlassian Community Events