Populate customfield using post functions in scriptrunner


Duplicated to https://answers.atlassian.com/questions/39233766


I am working on a feature in JIRA which will tidy up the Release process for projects. Currently there are three stages to a Release; Pre-Release, Release and Post-Release, each stage has a number of tasks that need to checked off before proceeding to the next release stage. And instead of writing these tasks into the description field and adding the tick or cross next to each task (which most issues with releases now have), a tidier way is to use the JIRA Checklist Add-On (https://marketplace.atlassian.com/plugins/com.okapya.jira.checklist/server/overview) which creates a customfield for the issue and displays each task as a checklist, and once each task is completed, the release stage will move to the next stage.

An example of the JIRA Checklist Plugin: 




Shows three tasks that need to be ticked off before moving to the next stage. 

Current Progress:

I am also using Scriptrunner to automatically create subtasks based on the Release and Populate the JIRA Checklist plugin in with values (which works fine).


The Issue arises when im trying to populate the JIRA checklist customfield with values. I am not sure if I am even able to populate a customfield value from a JIRA Add-on using scriptrunner. From looking at the built-in scripts for scriptrunner (https://scriptrunner.adaptavist.com/ it looks like you can manipulate custom field values, but im not sure how. I am also on a trial version of scriptrunner with version 3.0.16.

Any suggestions or ideas on this would be much appreciated.




1 answer

You definitely can, it's just you need to know what sort of object to store in the custom field. But that's specific to the checklist plugin, I'm surprised you didn't use that plugin's label.

> I am also using Scriptrunner to automatically create subtasks based on the Release and Populate the JIRA Checklist plugin in with values (which works fine).

How... is this part of the checklist plugin? If not that implies that you are able to read the field?

Cheers for the reply Jamie,

Do you have code that shows an example of populating a customfield with a particular object, i'm not able to figure out what kind of object the JIRA checklist plugin needs.

Examples for many types here: https://scriptrunner.adaptavist.com/latest/jira/recipes/workflow/postfunctions/set-issue-attributes.html - but you should ask the plugin developer. Or get the value from the field, and see what it is.

Thanks for the link.

Im having a look at the way the JIRA checklist plugin is structured, and from the screenshot below this is how you enter values into the plugin: 


and from inspecting the HTML element, i see it takes an object that is passed into an input field:


is this the type of object i can pass into the groovy script? 


PS i have added the JIRA checklist plugin as a label here too. 

Steven Behnke Community Champion Jun 27, 2016

You need to know what type of java object it is, in order to handle it in a JAVA or groovy script.

How do you find out what the Java Object is? i don't have any logging working for this script even though i added the neccessary code for it to work, the code im using is currently similar to the code mentioned here: How can I add or remove a option to/form the todolist via a groovy script

My Code: 

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def issueService = ComponentAccessor.getIssueService();
def authContext = ComponentAccessor.getJiraAuthenticationContext();
def log = Category.getInstance('com.onresolve.jira.groovy.PostFunction');
def checklist = customFieldManager.getCustomFieldObjectByName("Pre-Release Task");
def newOptions = [];
checklist.getValue(issue).each {
	oldValue = it.getValue().split('-');
    oldValue[1] = '0';
    newOptions << oldValue.join('-');
def user = authContext.getUser().getDirectoryUser();
def issueParams = issueService.newIssueInputParameters();
issueParams.addCustomFieldValue(checklist.getIdAsLong(), *newOptions);
validationResults = issueService.validateUpdate(user, issue.getId(), issueParams);
issueService.update(user, validationResults);

Nothing happens when i run this command, I expect it to create a new subtask with pre populated JIRA checklist values.


Any suggestions would be great.

So according to the field schema that Pre-Release Task takes an Array of Strings: 


So i tried adding that to the groovy script as follows:

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def checklist = customFieldManager.getCustomFieldObjectByName("Pre-Release Task")
def options = ["name":"Test 5", "checked":true, "mandatory":true]
issue.setCustomFieldValue(checklist, options)

 But still nothing seems to happen, any ideas on what im doing wrong?

Steven Behnke Community Champion Jun 29, 2016

Well, you can use getClass to get the java class for comparisons, or you can log them...? There's a lot of ways to determine or objects in JAVA but this isn't really a scriptrunner question.

The real problem here is I don't think Jamie or myself have ever used this checklist plugin, so we're not just sure how to answer the questions.

Exactly, which is why I was trying to get the OP to add the checklist plugin's tags. But he asked a new question, which has the right answer: https://answers.atlassian.com/questions/39233766

Steven Behnke Community Champion Jun 29, 2016

Oh cool! Thanks for the loopback.

Hi, I have sorted it with the Plugin developers, thanks for you help smile

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Mar 13, 2019 in Marketplace Apps

Marketplace Spotlight: Marketing apps for Confluence to keep your teams working on the same page


242 views 0 6
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you