Forums

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

[Scriptrunner] create a linked issue on another project during post function transition

Lisbon Cruz July 19, 2023

I have a transition that creates a bunch of sub tasks. when a particular subtask is created, I want to create a link issue using the "Clones an issue, and links [ScriptRunner]" post function. Under the condition section, I have this:

issue.summary.matches("MM conducts Table 3 Training(.*)") && issue.issueType.name == "PCT Sub-Task"

 this works fine when I tested it with additional issue actions and set the summary to below

issue.summary = 'Test'

 Now, I have a custom field that I want to copy from the parent task that triggered the subtask and this linked task, so I created the script below

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.MutableIssue

import com.atlassian.jira.user.ApplicationUser

Issue parentIssue = issue.getParentObject()

def proj = ComponentAccessor.getProjectManager().getProjectByCurrentKey("PCT").id

def pct = parentIssue.issueType.name as String

def cfMgr = ComponentAccessor.getCustomFieldManager()

def newCHAOCf = cfMgr.getCustomFieldObjects(proj, pct).find {it.name == 'New CH/AO'}

def newCHAO = parentIssue.getCustomFieldValue(newCHAOCf) as ApplicationUser

MutableIssue mm_task = ComponentAccessor.getIssueFactory().getIssue()

mm_task.summary = ('Conduct ILT for New CH/AO' + ' [' + newCHAO.getDisplayName() + ']') as String

I am getting the error below: 

java.lang.NullPointerException: Cannot get property 'issueType' on null object 

it seems like it is not getting the parent issue object properly. Any input would be much apprecaited. Thank you.

2 answers

1 accepted

0 votes
Answer accepted
Lisbon Cruz July 20, 2023

Update: the code below is what ended up working for me:


import com.atlassian.jira.user.ApplicationUser

def
cf = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'New CH/AO'}

def ch_ao_val = sourceIssue.getCustomFieldValue(cf) as ApplicationUser

def summary = ('Conduct ILT for New CH/AO' + ' [' + ch_ao_val.getDisplayName() + ']') as String

def description = ('MM Team, AE roles submitted on' + ' [' + ch_ao_val.getDisplayName() + ']') as String

issue.summary = summary

issue.description = description

I am not sure why issue.summary = 'some string' works but

issue.setSummary('some string') doesn't. Is it because I am using a macro "Clones an issue, and links [ScriptRunner]" rather than using a custom post function script?
0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 20, 2023

Hi @Lisbon Cruz

For your requirement, you must first validate whether the current issue is a Sub-task. This is missing in your code, which is why it is failing.

Below is a sample working code for your reference:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customFieldManager = ComponentAccessor.customFieldManager
def sampleTextField = customFieldManager.getCustomFieldObjectsByName('Sample Text Field').first()

if (issue.isSubTask()) {
def parent = issue.parentObject
def parentFieldValue = parent.getCustomFieldValue(sampleTextField) as String
issue.setCustomFieldValue(sampleTextField, parentFieldValue)
issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

 

The approach above uses the old ScriptRunner approach. I recommend upgrading your ScriptRunner plugin to the latest release, i.e. 8.7.0 or at least version 7.11.0, so you can use the HAPI feature.

Using the HAPI feature, all you have to do is:-
if (issue.subTask) {
def parent = issue.parentObject
issue.set {
setCustomFieldValue('Sample Text Field', parent.getCustomFieldValue('Sample Text Field'))
}
}

Below is a screenshot of the configuration using HAPI:-

test1.pngtest2.png

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

Lisbon Cruz July 20, 2023

Good Morning,

It looks like I do have scriptrunner 8.7.0. Do I have to add the custom script post function in addition to clones issues? Here is my current configuration:

config.PNG

Would I have to add the "HAPI" Script above in addition? Also, the reason I am doing it this way, rather than just using project automation is to be able to grab the custom field value of the "users" custom field.

Lisbon Cruz July 20, 2023

I tried the first code and it worked. But I need to set the summary and description to something custom, hence why I am using scriptrunner. Here's the code that I have:

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.event.type.EventDispatchOption

def issuemgr = ComponentAccessor.issueManager

def log_in_usr = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def cus_field_mgr = ComponentAccessor.customFieldManager

def new_ch_ao = customFieldManager.getCustomFieldObjectsByName('New CH/AO').first()

if (issue.isSubTask()) {

 def parent = issue.parentObject

 def ch_ao_val = parent.getCustomFieldValue(new_ch_ao) as String

 issue.setCustomFieldValue(new_ch_ao, ch_ao_val)

 issue.setDescription('MM Team, AE roles submitted on' + ' [' + ch_ao_val + ']') as String

 issue.setSummary('Conduct ILT for New CH/AO' + ' [' + ch_ao_val + ']') as String

 issuemgr.updateIssue(log_in_usr, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

}

I just put this under the "Additional issue actions" in "Clones an issue, and links [ScriptRunner]" post function. I am getting the subtasks summary, despite setting the summary to something custom.

Suggest an answer

Log in or Sign up to answer