Populate customfield using post functions in scriptrunner

Joshi Shushruth June 20, 2016

Note:

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


Context:

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: 

checklist.png

 

 

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).

Issue:

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/4.1.3.14/jira/builtin-scripts.html) 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.

 

Cheers

 

1 answer

0 votes
JamieA
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 21, 2016

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?

Joshi Shushruth June 22, 2016

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.

JamieA
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 23, 2016

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.

Joshi Shushruth June 23, 2016

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: 

jirachecklist.png

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

checklist-element.png

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 F Behnke
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 27, 2016

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

Joshi Shushruth June 27, 2016

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');
log.setLevel(org.apache.log4j.Level.DEBUG);
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.

Joshi Shushruth June 27, 2016

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

prt.png

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 F Behnke
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 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.

JamieA
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 29, 2016

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 F Behnke
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 29, 2016

Oh cool! Thanks for the loopback.

Joshi Shushruth June 29, 2016

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events