How to update the ToDo Checklist with groovy?

Hi - 

I'm trying to update the ToDo checklist programmatically based on some values contained in other custom fields - we use the checklist as a "new hire task list", and I need the ability for the user to automatically customize the list based on some pre-ordained options. 

When I got the output of the field, it looked like it was in JSON notation - I've managed to combine my custom options into the same format, but now I'm having an issue with the update step. I can't figure out what type I should be casting the object into to get the field to accept the data. Any thoughts? I've included my code below.

I'm running it via a post function script upon issue creation. The custom field is linked into our Jira Service Desk, so I want the ability to update the list based on a different field that's visible to the Service Desk Portal

 

(had to edit the question because Atlassian answers only lets you comment once every 24hrs for a new user? Kind of offputting...)

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import groovy.json.JsonBuilder;
import groovy.json.JsonSlurper;
import groovy.json.JsonOutput;


CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();



MutableIssue mi = (MutableIssue) issue;

CustomField checklist_name = customFieldManager.getCustomFieldObjectByName( "SD - NewHireChecklist" );
CustomField accessreq_name =  customFieldManager.getCustomFieldObjectByName( "SD - New Hire Access Request");


JsonSlurper jsonSlurper = new JsonSlurper();
JsonOutput jsonOutput = new JsonOutput();

checklist_data = jsonSlurper.parseText(issue.getCustomFieldValue(checklist_name));
accessreq_data = issue.getCustomFieldValue(accessreq_name);




if(accessreq_data) {
    accessreq_data.each {
        checklist_data.push(jsonSlurper.parseText('{ "id":"'+it+'","type":"todo"}'));

    }
}
JsonBuilder json = new JsonBuilder(checklist_data);


mi.setCustomFieldValue(checklist_name, jsonOutput.toJson(json.toString()));

3 answers

Is this script running on a workflow transition, or elsewhere? What plugin are using to run it, ScriptRunner or some other?

Hi Jamie - thanks for the response. This is a post function on a workflow transaction after the "Create" transition. I'm using Scriptrunner.

I think it's just a problem with your json handling. It would help if you pasted the raw json that the field stores.

I presume it's a list of maps but haven't installed it to see.

So this: jsonSlurper.parseText will give you a List object. So to add it you would do:

checklist_data.add([id: it, type: "todo"])

Finally you seem to be encoding the json twice before setting it. Change the last two lines:

def json = new JsonBuilder(checklist_data);
log.warn("About to set ${json.toPrettyString()}")
 
mi.setCustomFieldValue(checklist_name, json.toString())

Any issues please provide the log messages. Your code as you have pasted it should be failing.

If it works you can remove the log.warn line.

 

 

Part of the problem is I'm not 100% sure what the field will accept. When I grab the data from the field, it looks like this: OUTPUT IS: [{"id":"E-mail / Domain Access","type":"todo"},{"id":"Share Access (if required)","type":"todo"},{"id":"Hardware Setup","type":"todo"},{"id":"Shoretel Setup (if required)","type":"todo"},{"id":"Keyfob / Alarm Codes","type":"todo"},{"id":"Parking Permit (if required)","type":"todo"},{"id":"Background Check","type":"todo"}] The code you provided above (with some added debug statements) produces 2015-10-08 09:23:24,842 http-bio-8443-exec-22095 WARN kwise 563x3651172x1 1jxlu2 198.160.163.128 /secure/QuickCreateIssue.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] OUTPUT is[[id:E-mail / Domain Access, type:todo], [id:Share Access (if required), type:todo], [id:Hardware Setup, type:todo], [id:Shoretel Setup (if required), type:todo], [id:Keyfob / Alarm Codes, type:todo], [id:Parking Permit (if required), type:todo], [id:Background Check, type:todo], [id:GoToAssist, type:todo], [id:HR Hosted (SacSupport), type:todo], [id:Ascentis Payroll Support, type:todo]] The GoToAssist, Hr Hosted, and Payroll Support items were added correctly by the code (the others are the default checklist values), but the issue just kind of doesn't create - I get a spinning wheel and my output statement happens in the logs, but then the issue doesn't create. No error, just the "Create Issue" screen again. I had been playing around with getting items to be in the same format as the field output (first log statement above), and my original code was returning: 2015-10-07 11:58:29,905 http-bio-8443-exec-21856 WARN kwise 718x3591730x1 1jfv5pc 198.160.163.128 /secure/QuickCreateIssue.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] OUTPUT IS: [{"id":"E-mail / Domain Access","type":"todo"},{"id":"Share Access (if required)","type":"todo"},{"id":"Hardware Setup","type":"todo"},{"id":"Shoretel Setup (if required)","type":"todo"},{"id":"Keyfob / Alarm Codes","type":"todo"},{"id":"Parking Permit (if required)","type":"todo"},{"id":"Background Check","type":"todo"},{"id":"GoToAssist","type":"todo"},{"id":"HR Hosted (SacSupport)","type":"todo"}] But when it came to updating the MutableIssue I was getting a combination of issue creation failures, or blank fields. The code I posted originally just seemed to blank out the custom field I was trying to update.

Also with the code you posted above, it looks like the JSONBuilder object isn't able to do what it needs to do log.warn("OUTPUT is" + checklist_data); JsonBuilder json = new JsonBuilder(checklist_data); log.warn("JSON is" + json.toString()); I don't get the second log statement, the create issue action just kind of "ends" and doesn't complete.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Marketplace Apps

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,157 views 0 8
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