How to create issues inside methods in postfunctions?

I have a groovy script and in it I create issues several times under different conditions. I have copied pasted the below code several times in my script. I want to write a function to do that. But when i write these lines in a function, nothing is accessible. The issueFactory, the functions of MutableIssue, nothing. They give an error. Why is that?!

MutableIssue newSubTask = issueFactory.getIssue() 
newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{it.getName() == "Sub-task"}.id)   

6 answers

This widget could not be displayed.

Hi Shagufta,

The following code will create a issue in the project TP of type task:

def projectKey = 'TP'
def taskType = get('/rest/api/2/issuetype').asObject(List).body.find { it['name'] == 'Task' }['id']
        .header('Content-Type', 'application/json')
                fields: [
                        summary    : 'Task Summary',
                        description: "Don't forget to do this!.",
                        project    : [
                                key: projectKey
                        issuetype  : [
                                id: taskType

This page details the differences between ScriptRunner Server and Cloud:

Regards, Jon

This widget could not be displayed.
Vasiliy Zverev Community Champion Nov 28, 2016

Here is code example for subtask creation. Here created a cubtask for each user into multyuser select field:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueInputParameters;
import com.atlassian.jira.issue.index.IssueIndexManager;
import com.atlassian.jira.config.SubTaskManager;
import java.text.SimpleDateFormat;
import java.text.FieldPosition;
import java.sql.Timestamp;
//Issue issue = ComponentAccessor.getIssueManager().getIssueObject("OEBSR12-3631")
if(issue.getDueDate() == null){
    Calendar dueDate = Calendar.getInstance();
    int addedDays = 0;
    while(addedDays < 2 ){
        dueDate.add(Calendar.DAY_OF_YEAR, 1)
        if( !((dueDate.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) || (dueDate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)) ){
    issue.setDueDate(new Timestamp(dueDate.getTimeInMillis()))
IssueService issueService = ComponentAccessor.getIssueService();
IssueService.IssueResult issueResult;
IssueService.CreateValidationResult createValidationResult;
IssueInputParameters issueInputParameters =	issueService.newIssueInputParameters();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yy");
issueInputParameters.setDueDate(simpleDateFormat.format(new Date( issue.getDueDate().getTime()), new StringBuffer(), new FieldPosition(0)).toString());
issueInputParameters.setSummary("Согласование документа")
curUser = ComponentAccessor.getJiraAuthenticationContext().getUser();
SubTaskManager subTaskManager = ComponentAccessor.getSubTaskManager();
IssueIndexManager issueIndexManager = ComponentAccessor.getIssueIndexManager();
long soglasovantId = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Согласовант").getIdAsLong();
for(user in  ComponentAccessor.getCustomFieldManager().getCustomFieldObject(14600L).getValue(issue)){
    createValidationResult = issueService.validateSubTaskCreate(curUser, issue.getId(), issueInputParameters);
        issueResult = issueService.create(curUser, createValidationResult);
        //return "" + issueResult.getErrorCollection();
        subTaskManager.createSubTaskIssueLink(issue, issueResult.getIssue(), user.directoryUser);

Is it possible to use methods ?

Given that this is ScriptRunner for JIRA Cloud the above code will not work

This widget could not be displayed.

Are you using JIRA Cloud or JIRA Server?

This widget could not be displayed.

JIRA cloud


This widget could not be displayed.

As you are using JIRA Cloud, there are some differences in the usage. You will have to use JIRA's REST API to create new issue. Please have a look at the differences in Script Runner for JIRA Cloud documentation on the differences between Server and Cloud and JIRA REST API how to create an issue. In case it is suitable for your use case you can use Workflow Post function to create subtask described here.

This widget could not be displayed.

I know and i have done all of it already. I am just wondering if it is possible to put it in a user defined method, to enhance code reusability

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted yesterday in Jira

Atlassian Research Workshop opportunity on Sep. 28th in Austin, TX

We're looking for participants for a workshop at Atlassian! We need Jira admins who have interesting custom workflows, issue views, or boards. Think you have a story to sha...

49 views 2 2
Join discussion

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