Script Post Function Create Subtask

Junaid Shah
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 Leaders.
November 26, 2015

Hi,

 

I'm wondering whether anyone can help with a script for scriptrunner plugin using the Script Post function Create Subtask.

 

I am creating a subtask of a linked issue, under where you set the field values, how can I transfer field values of the linked issue into the newly created subtask using the post function Create Subtask ..

 

there is a template built for the current issue but nothing for linked issues.. If possible does anyone have any hints on how to write the script or something?

 

Thank you.

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Jeff Louwerse
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 Leaders.
November 26, 2015

Here is some code I use.  may not work as is as my script is more complex than just creating a subtask so i cut out only what I think you need.

MutableIssue issue = (MutableIssue) issue;
Issue ParentIssue = (MutableIssue) issue;
def issueMgr = ComponentAccessor.getIssueManager()
IssueFactory issueFactory = ComponentAccessor.getIssueFactory()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
SubTaskManager subTaskManager = ComponentAccessor.getSubTaskManager()
IssueIndexManager indexManager = ComponentAccessor.getIssueIndexManager()

    //Create the copy based on the current issues data.  Does not include custom fields.
    def newissue = issueFactory.cloneIssue(issue)  ;

    //Copy the Custom Fields.
    for (CustomField cf : customFieldManager.getCustomFieldObjects(issue)) {
        if (issue.getCustomFieldValue(cf))
        {   newissue.setCustomFieldValue(cf, issue.getCustomFieldValue(cf))        }
    }
    
    //  Clear fields that should not be copied.
    newissue.setCreated(null)
    newissue.setResolutionObject (null)
    newissue.setTimeSpent(null)
    newissue.setAssignee(currentUserObj)
    newissue.setEstimate(null)
    newissue.setOriginalEstimate(null)
    
    //Find Sub Task Issue Type Object .. Replace "Sub Task" with what ever your name is, or just hard code the ID.
    def newIssueType = ComponentAccessor.issueTypeSchemeManager.getIssueTypesForProject(issue.projectObject).find {it.name == "Sub Task")}
    newissue.setIssueTypeId(newIssueType.id);
    
    // Set the Parent ID
    newissue.setParentId(issue.getId());
    
	// Add SUB_TASK to the beginning of the summary to indicate it is a copy.
	if (issue.summary =~ /SUB TASK - .*/) {newissue.setSummary("$issue.summary")} 
	else {newissue.setSummary("SUB TASK - $issue.summary")}

    //  Create the issue!!
    issueMgr.createIssueObject(currentUserObj, newissue)
    
    //Add Sub task link 
    subTaskManager.createSubTaskIssueLink(ParentIssue, newissue, currentUserObj)
    
    //may or may not be necessary.
     indexManager.reIndex(issue)
    indexManager.reIndex(newissue);
Junaid Shah
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 Leaders.
November 26, 2015

Thanks I will give this a go and get back, appreciated.

Junaid Shah
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 Leaders.
November 26, 2015

Hi, Had a go and couldn't get it to work. I don't think you're going into the Linked issue are you? anyway thanks :)

Thanos Batagiannis [Adaptavist]
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 Leaders.
November 26, 2015

Sy, Jeff's script is nicely documented and I think it covers the key 'create sub task' aspects. In case you need the linked issues {code} def issueLinkManager = ComponentAccessor.getIssueLinkManager() def linkCollection = issueLinkManager.getLinkCollection(issue, applicationUser) def allLinkedIssues = linkCollection?.getAllIssues() {/code}

Junaid Shah
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 Leaders.
November 26, 2015

perhaps I've been misunderstood - Thank you to both for your answers however. I have got the creation of the subtask, that is fine, I am using a scripted post function for that using the scriptrunner plugin. My issue is that I have fields which I want to set in the NEW subtask. These fields are located on the linked issue. I have the linked issue ID in a custom field too. Currently. I use the code import com.atlassian.jira.ComponentManager import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.issue.IssueManager import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.MutableIssue //The linked Issue ID def cf = cfValues['Linked Issue'] //Retrieving a custom field called "Services" CustomField request_type = customFieldManager.getCustomFieldObjectByName('Services') //retrieving the value for "Services" from current issue NOT linked issue def request_type_value = transientVars["issue"].getCustomFieldValue(request_type) //Setting the value of "Services" issue.setCustomFieldValue(request_type, request_type_value)

Thanos Batagiannis [Adaptavist]
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 Leaders.
November 26, 2015

If I understand correct you want to set a value in a custom field under the issue 'Linked Issue' If that is the case then: //I suppose that the cf has the linked Issue key, therefore you can retrieve the issue from key def issueManager = ComponentAccessor.getIssueManager() def targetIssue = issueManager.getIssueObject(cf) // set the value of 'Services' in the targetIssue's customField targetIssue.setCustomFieldValue(request_type, request_type_value)

Junaid Shah
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 Leaders.
November 26, 2015

Hi thanks for this, I just needed to retrieve the value from the linked issue and then set it in the current issue. I will give your code a go by swapping it. Thanks.

Junaid Shah
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 Leaders.
November 26, 2015

By current issue I mean the new subtask that I am creating.

Junaid Shah
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 Leaders.
November 26, 2015

I was unable to get it to work..

Junaid Shah
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 Leaders.
November 29, 2015

After some messing around Thanos's answer worked. //I suppose that the cf has the linked Issue key, therefore you can retrieve the issue from key def issueManager = ComponentAccessor.getIssueManager() def targetIssue = issueManager.getIssueObject(cf) // set the value of 'Services' in the targetIssue's customField targetIssue.setCustomFieldValue(request_type, request_type_value)

TAGS
AUG Leaders

Atlassian Community Events