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

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

Alexandra Steinberg Apr 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

1 vote
Answer accepted
Ivan Tovbin Community Leader Apr 20, 2018 • edited

Hi Alexandra,

It seems to me that your code is way too complicated for a task as simple is this. Try using this code instead:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.event.type.EventDispatchOption

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

UserManager usrMgr = ComponentAccessor.getUserManager()

ArrayList<ApplicationUser> requestParticipants = new ArrayList<ApplicationUser>()
requestParticipants.add(usrMgr.getUserByName("Service_Desk_Testing"))

CustomFieldManager cfMgr = ComponentAccessor.getCustomFieldManager()
CustomField reqPartCf = cfMgr.getCustomFieldObject((Long) 11111) //request participants field
CustomField lobCf = cfMgr.getCustomFieldObject((Long) 22222) // lob field

if (issue.getCustomFieldValue(lobCf) == "CIO"){
requestParticipants.add(usrMgr.getUserByName("alexandra"))
}else{
requestParticipants.add(usrMgr.getUserByName("Service_Desk_Testing2"))
}

issue.setCustomFieldValue(reqPartCf, requestParticipants)
ComponentAccessor.getIssueManager().updateIssue(currentUser, issue, EventDispatchOption.ISSUE_UPDATED, false)

Also make sure you place this post function LAST in the list of your 'Create' transition post functions. 

Alexandra Steinberg Apr 20, 2018 • edited

Thanks Ivan for your reply! I implemented your solution (changing the custom field ID's) and it did not add any request participants to the ticket.

Here's the failure information:

2018-04-20 16:13:30,079 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-04-20 16:13:30,079 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: SIX-73, actionId: 1, file: <inline script>
java.lang.NullPointerException
 at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
 at com.atlassian.jira.issue.IssueImpl.setCustomFieldValue(IssueImpl.java:906)
 at com.atlassian.jira.issue.MutableIssue$setCustomFieldValue$9.call(Unknown Source)
 at Script970.run(Script970.groovy:25)

 

Ivan Tovbin Community Leader Apr 20, 2018

Sorry, my mistake, I've actually made a typo in this line:

issue.setCustomFieldValue(reqParCf, requestParticipants)

It should be like this:

issue.setCustomFieldValue(reqPartCf, requestParticipants)

 For posterity I've also fixed this typo in my initial answer.

Alexandra Steinberg Apr 20, 2018

Hey Ivan,

The typo isn't causing the error, apologies, I failed to mention I fixed that. Still getting the following output

2018-04-20 21:03:43,909 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-04-20 21:03:43,909 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: SIX-76, actionId: 1, file: <inline script>
java.lang.NullPointerException
 at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
 at com.atlassian.jira.issue.IssueImpl.setCustomFieldValue(IssueImpl.java:906)
 at com.atlassian.jira.issue.MutableIssue$setCustomFieldValue$9.call(Unknown Source)
 at Script970.run(Script970.groovy:25)
Ivan Tovbin Community Leader Apr 20, 2018

That's really odd. Just checked, works fine on my end. Can you please show the complete code as you have it on your end?

Ivan Tovbin Community Leader Apr 20, 2018 • edited

Also, can you double check that custom field IDs that you have used are valid? I've managed to reproduce this error by intentionally using a non-existing custom field ID for 'request participants' field.

Alexandra Steinberg Apr 23, 2018

Hey Ivan, you are right about the custom field ID for request participants being invalid. I fixed that, and the script executes; however, it is still not working as expected. It is not matching the value I chose for the custom lob field. Instead of adding user "alexandra" to the ticket when I selected "CIO", it added "Service_Desk_Testing2".

Here's the complete code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.event.type.EventDispatchOption

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

UserManager usrMgr = ComponentAccessor.getUserManager()

ArrayList<ApplicationUser> requestParticipants = new ArrayList<ApplicationUser>()
requestParticipants.add(usrMgr.getUserByName("Service_Desk_Testing"))

CustomFieldManager cfMgr = ComponentAccessor.getCustomFieldManager()
CustomField reqPartCf = cfMgr.getCustomFieldObject((Long) 10001) //request participants field
CustomField lobCf = cfMgr.getCustomFieldObject((Long) 12345) // lob field

if (issue.getCustomFieldValue(lobCf) == "CIO"){
requestParticipants.add(usrMgr.getUserByName("alexandra"))
}else{
requestParticipants.add(usrMgr.getUserByName("Service_Desk_Testing2"))
}

issue.setCustomFieldValue(reqPartCf, requestParticipants)
ComponentAccessor.getIssueManager().updateIssue(currentUser, issue, EventDispatchOption.ISSUE_UPDATED, false)

Ivan Tovbin Community Leader Apr 23, 2018

From your description it seems that this whole condition is not working:

if (issue.getCustomFieldValue(lobCf) == "CIO")

Can you double check and confirm that your "Lob" field type is a free text field and not a select list or anything? 

Alexandra Steinberg Apr 23, 2018

Yes agreed. It is a select list. How do I handle it?

Ivan Tovbin Community Leader Apr 23, 2018

It's easy, change the condition to this:

if (issue.getCustomFieldValue(lobCf).getValue() == "CIO")
Alexandra Steinberg Apr 23, 2018

That worked, thanks a lot Ivan!

Alexandra Steinberg Apr 23, 2018

Hey @Ivan Tovbin, why it is important that this post function is last in the Create Issue post functions? What are the negative implications of moving the post function up earlier, if any?

Ivan Tovbin Community Leader Apr 23, 2018

If we are talking about 'Create' transition if you would place this post function first in the list of post functions then it simply wont work, because the issue won't exist yet when the post function would be triggered.

An issue is created via a system post function "Creates the issue originally" which you can see in the list of post functions on your 'Create' transition. That's why you place any other post functions that call the issue AFTER it so that you don't end up calling a non-existing object.

Alexandra Steinberg Apr 23, 2018

Oddly, it does create the ticket and adds the correct request participants if I have it first in the order of "Create Issue" post functions...with "Create" being the last. Although everything works successfully, it does log an error after it is fired (which I pasted as an FYI below).

I modified the parameter in "updateIssue()" from "false" to "true" which sends an email notifying the request participants after they have been added, so I can leave this post function last in the list after the "Create" transition.

Thanks for your explanation, this information has been really helpful!

2018-04-23 14:22:09,483 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-04-23 14:22:09,484 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
java.lang.IllegalArgumentException: Source GenericValue can not be null.
 at com.atlassian.jira.association.NodeAssociationStoreImpl.getSinksFromSource(NodeAssociationStoreImpl.java:34)
 at com.atlassian.jira.project.version.DefaultVersionManager.getVersionsByIssue(DefaultVersionManager.java:750)
 at com.atlassian.jira.project.version.DefaultVersionManager.getFixVersionsFor(DefaultVersionManager.java:595)
 at com.atlassian.jira.project.version.DefaultVersionManager.updateIssueFixVersions(DefaultVersionManager.java:565)
 at com.atlassian.jira.issue.fields.FixVersionsSystemField.updateIssueValue(FixVersionsSystemField.java:98)
 at com.atlassian.jira.issue.fields.AbstractVersionsSystemField.updateValue(AbstractVersionsSystemField.java:368)
 at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:704)
 at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:669)
 at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:655)
 at com.atlassian.jira.issue.managers.RequestCachingIssueManager.updateIssue(RequestCachingIssueManager.java:214)
 at com.atlassian.jira.issue.IssueManager$updateIssue$2.call(Unknown Source)
 at Script1066.run(Script1066.groovy:50)
Ivan Tovbin Community Leader Apr 23, 2018

Cheers for update @Alexandra Steinberg. Frankly I wasn't aware that it would actually work. Good to know.

Incidentally, I think the reason why your original code didn't work is because you treated your 'lob' field as a free text field and not a select list. Thing is, getCustomFieldValue() method returns different object types depending on the custom field type. In case of a select list it returns a LazyLoadedOption object, so you can't just compare it to a String, hence the condition not working. You need to retrieve the option's value first (which is a String) with the getValue() method so that you can safely compare it to another String.

Alexandra Steinberg Apr 24, 2018

Thanks @Ivan Tovbin, agreed. Valuable lessons learned here :)

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Opsgenie

Handling Opsgenie Alerts

Hi everyone, welcome to the kb articles for Opsgenie FAQs. I'm one of the Technical Support Engineers for Opsgenie who will be providing weekly posts on FAQs from customers.   All alerts are ...

72 views 0 3
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