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.setAssigneeId(user.name) 
newSubTask.setParentObject(parentIssue) 
newSubTask.setFixVersions(Arrays.asList(versionn)) 
newSubTask.setProjectObject(parentIssue.getProjectObject()) 
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']
post('/rest/api/2/issue')
        .header('Content-Type', 'application/json')
        .body(
        [
                fields: [
                        summary    : 'Task Summary',
                        description: "Don't forget to do this!.",
                        project    : [
                                key: projectKey
                        ],
                        issuetype  : [
                                id: taskType
                        ]
                ]
        ])
        .asString().body

This page details the differences between ScriptRunner Server and Cloud: http://scriptrunner-docs.connect.adaptavist.com/jiracloud/migrating.html

Regards, Jon

0 votes
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)) ){
            ++addedDays;
        }
    }
    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.setIssueTypeId("11104")
issueInputParameters.setProjectId(issue.getProjectId())
issueInputParameters.setSummary("Согласование документа")
issueInputParameters.setDescription(issue.getDescription());
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)){
    issueInputParameters.setAssigneeId( user.name);
    issueInputParameters.addCustomFieldValue(soglasovantId, user.name)
    createValidationResult = issueService.validateSubTaskCreate(curUser, issue.getId(), issueInputParameters);
    if(createValidationResult.isValid()){
        issueResult = issueService.create(curUser, createValidationResult);
        //return "" + issueResult.getErrorCollection();
        subTaskManager.createSubTaskIssueLink(issue, issueResult.getIssue(), user.directoryUser);
        issueIndexManager.reIndex(issue);
        issueIndexManager.reIndex(issueResult.getIssue());
    }
}

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?

JIRA cloud

 

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 Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,353 views 14 19
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