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

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

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

Are you using JIRA Cloud or JIRA Server?

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.

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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,175 views 13 19
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot