Why is my custom field empty during the "Create" Post Function?

Kenneth McClean June 4, 2021

Edit: It turns out that I wasn't doing anything wrong. This is simply the nature of Forms and Jira. The value isn't written to the custom field until well after the ticket is created. 

The solution is to put your Groovy code in a listener, instead of as part of a workflow transition.

I wrote a little explanation for anyone who needs it:

 

https://www.kennethmcclean.com/blog/the-forest-for-the-trees-custom-field-null-values-during-workflow-transitions/

****************************************

 

We have a groovy script that takes the value stored in a custom field on a ProForma form(the field is a single-user picker).

On create the script should move that value to a different field (and does a bunch of other things).

However it fails every time, as Jira insists that the initial value of the custom field is null.

If we set the script to run on any transition after create, it works fine.  The script also works fine on existing tickets, using the Scriptrunner Console.

It’s as though the value of the custom field is not being set until well after the create transition has finished.


I simplified the script to just grabbing the value stored in the custom field, and logging still returns null.

Is there a way to ensure that the custom field value populates during the create transition?


import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager


log.setLevel(Level.DEBUG)

UserManager usrMgr = ComponentAccessor.getUserManager()

CustomFieldManager cfMgr = ComponentAccessor.getCustomFieldManager()

CustomField lobCf = cfMgr.getCustomFieldObject("customfield_12345")

log.debug "customfield value is"+ issue.getCustomFieldValue(lobCf)

1 answer

0 votes
Peter Preston
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.
June 6, 2021

Hi @Kenneth McClean - Pete from the ProForma team here. Due to the order of events, it’s common for this kind of automation to read null data from ProForma. I know this can be avoided in ScriptRunner by adding a sleep() call to the beginning of the script to give enough time for ProForma data to save to Jira. I’m not as familiar with groovy, but think a similar approach could work. 

Kenneth McClean June 7, 2021

Hi Pete:

 

Thank you very much for your answer.  I'll give the sleep call a try.

Cheers,

Ken

Kenneth McClean June 7, 2021

@Peter PrestonI tried adding a sleep() of up to 15 seconds at the start of my script. It certainly delayed the running but it didn't seem to make a difference otherwise. Is there anything else I need to do to make this work?  I tried sticking the sleep call in different places in my script.

Thanks!

Ken

Peter Preston
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.
June 7, 2021

Hi @Kenneth McClean -- hmm. I've asked around and don't have a great answer for you at the moment. I suggest connecting with support.thinktilt.com and seeing they can shed more light on the order of operations.

Suggest an answer

Log in or Sign up to answer