The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Script Runner Create Subtask

KRC
Contributor
March 9, 2018

I want to create a subtask on parent issue create transition using Script post function>create a subtask.

In Additional issue actions, I want to pass two fields so that subtask gets created with them

1. cascading select(parent 1, child 6)

2. Due date

I tried different ways of code by its not helping me. 

Note:

If a user creates a subtask in a regular way, I have a validator to check due date and cascading select is required. just to satisfy this workflow function I need above-said fields to be prepopulated while creating a subtask automatically when a parent is created.

 

Sample code i tried and found some pieces of code from Atlassian community

import com.atlassian.jira.component.ComponentAccessor;
import java.sql.Timestamp
def optionsManager = ComponentAccessor.getOptionsManager();
def parentOptionObj = optionsManager.findByOptionId(14491);
def childOptionObj = optionsManager.findByOptionId(14494);
Map<String,Object> newValues = new HashMap<String, Object>();
newValues.put(null, parentOptionObj);
newValues.put("1", childOptionObj);
return newValues;

def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Due'}
issue.setCustomFieldValue(cf, new Timestamp(new Date(2001,01,01).getTime()))

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

1 vote
Alexey Matveev
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 Champions.
March 9, 2018

What is the error?

KRC
Contributor
March 9, 2018

for now this

Untitled.png

KRC
Contributor
March 9, 2018

Am trying to set two fields, maybe am wrong in putting the code together.

Alexey Matveev
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 Champions.
March 9, 2018

it must be like this

 

import com.atlassian.jira.component.ComponentAccessor;
import java.sql.Timestamp
def optionsManager = ComponentAccessor.getOptionsManager();
def parentOptionObj = optionsManager.findByOptionId(14491);
def childOptionObj = optionsManager.findByOptionId(14494);
Map<String,Object> newValues = new HashMap<String, Object>();
newValues.put(null, parentOptionObj);
newValues.put("1", childOptionObj);
return newValues;

def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Due'}
issue.setCustomFieldValue(cf, new Timestamp(new Date(2001,01,01).getTime()))

KRC
Contributor
March 9, 2018

updated as per your suggestion. I have this error now. Untitled.png

Alexey Matveev
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 Champions.
March 9, 2018
import com.atlassian.jira.component.ComponentAccessor;
import java.sql.Timestamp
def optionsManager = ComponentAccessor.getOptionsManager();
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def parentOptionObj = optionsManager.findByOptionId(14491L);
def childOptionObj = optionsManager.findByOptionId(14494L);
Map<String,Object> newValues = new HashMap<String,Object>();
newValues.put(null, parentOptionObj);
newValues.put("1", childOptionObj);
return newValues;


def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Due'}
issue.setCustomFieldValue(cf, new Timestamp(new Date(2001,01,01).getTime()))

 

If you want to use ScriptRunner you should learn Jira Java API and basic programming. Or use any other plugin like Automation for Jira or Workflow Toolbox.

KRC
Contributor
March 9, 2018

The code looks good and Thank you for the help. 

Yeah am bad at this java stuff, trying to get up to it.

If you still have patience..

Coming to results, I can see validator error.  This is what i was hoping to clear by code.

Caused by: [InvalidInputException: [Error map: [{}]] [Error list: [[Due Date is required., PT is required.]]]
 at com.googlecode.jsu.util.ValidatorErrorsBuilder.process(ValidatorErrorsBuilder.java:50)
 at com.googlecode.jsu.workflow.validator.GenericValidator.validate(GenericValidator.java:77)
 at com.atlassian.jira.workflow.SkippableValidator.validate(SkippableValidator.java:45)
 at com.opensymphony.workflow.AbstractWorkflow.verifyInputs(AbstractWorkflow.java:1466)
 at com.opensymphony.workflow.AbstractWorkflow.transitionWorkflow(AbstractWorkflow.java:1167)
 at com.opensymphony.workflow.AbstractWorkflow.initialize(AbstractWorkflow.java:606)
 at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(OSWorkflowManager.java:741)
 at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:580)
Alexey Matveev
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 Champions.
March 9, 2018

I see, the problem is that validators work before the post function. If you set a post function, then you do not need a validator. Your post function will fill all the fields anyway.

TAGS
AUG Leaders

Atlassian Community Events