Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Use Groovy to set fields on sub-task?

Warren McInnes
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.
June 24, 2014

Hi,

Using the script runner post-function > Create subtask during a workflow transition I would like to do the following:

1. Take a value from a select list customfield and add it to the subtask summary, here is my code:

def Emergency = customFieldManager.getCustomFieldObject('customfield_12601')

def newSummary = 
if (Emergency == "Emergency") {
return issue.getCustomFieldValue(Emergency) + issue.getSummary()
 
} else (Emergency == "None") {
return issue.getSummary()
} else {
    return null
}

issue.setSummary(newSummary)

2. when the subtask gets created - change the priority to Blocker

Please assist?

5 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
Vijay Khacharia
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.
June 26, 2014

Hi,

This script should work in the create sub-task post function.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.priority.Priority
import com.atlassian.jira.config.ConstantsManager

def issueEmergency = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'ITO Emergency Change'}
def issueSummary = issue.getSummary()
def issuePriority = issue.getPriorityObject()

emergencyValue = issue.getCustomFieldValue(issueEmergency).toString().trim()

newSummary = emergencyValue +  " - " + issueSummary

if(emergencyValue.equals("Emergency") {
issue.setPriorityObject((Priority) ComponentAccessor.getConstantsManager().getIssueConstantByName(ConstantsManager.PRIORITY_CONSTANT_TYPE, "Blocker"))
issue.setSummary(newSummary)
}

The script copies the value from the single select list and prepends it to the parent summary. It sets the priority to "Blocker". You need to have the single select field available on both sub-task and parent for this to work.

Vijay

Warren McInnes
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.
June 29, 2014

Hi Vijay, This script did not work? I am not sure why, but I will try to see. Thanks for your effort

Vijay Khacharia
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.
June 29, 2014

Hi,

What was the error?

Vijay

Warren McInnes
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.
June 29, 2014

It just didn't create the sub-task

But by using this code it worked perfectly:

def issueEmergency = customFieldManager.getCustomFieldObject('customfield_12601')
def issueSummary = issue.getSummary()
def issuePriority = issue.getPriorityObject()
 
emergencyValue = issue.getCustomFieldValue(issueEmergency).getValue()
 
newSummary = "(" + emergencyValue +  ") " + issueSummary + " - approval task."
 
if(emergencyValue.equals("Emergency")){
issue.setPriorityId("1")
issue.setSummary(newSummary)
}

Thanks for all your help!

Vijay Khacharia
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.
June 29, 2014

I see the priority object might have been an problem as I have "Blocker" as priority in my system and it might be differnt for you.

Any ways, priorityId works well and all is good ;)

Warren McInnes
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.
June 29, 2014

Sorry Vijay,

So the above code I added works, but only if "ITO Change Control" > "Emergency" is selected. If nothing is selected/The default value "None" is selected it does not create the subtask?

I will try alter the code with an if else statment? Please advise?

Warren McInnes
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.
June 29, 2014

I have tried:

def issueEmergency = customFieldManager.getCustomFieldObject('customfield_12601')
def issueSummary = issue.getSummary()
def issuePriority = issue.getPriorityObject()
 
emergencyValue = issue.getCustomFieldValue(issueEmergency).getValue()
 
newSummary = "(" + emergencyValue +  ") " + issueSummary + " - approval task."
oldSummary = issueSummary + " - approval task."
 
if(emergencyValue.equals("Emergency")){
issue.setPriorityId("1")
issue.setSummary(newSummary)
} 
else(!emergencyValue.equals("Emergency")){
issue.setSummary(oldSummary)
}

AS WELL AS:
else(emergencyValue = "None"){
issue.setSummary(oldSummary)
}

But for some reason it doesn't even create the suv-task unless "Emergency" has been selected?

I have no idea and there are no errors?

Warren McInnes
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.
June 29, 2014

Nevermind, It seems I was trying to get to complex. The following worked for me:

def issueEmergency = customFieldManager.getCustomFieldObject('customfield_12601')
def issueSummary = issue.getSummary()
def issuePriority = issue.getPriorityObject()
 
emergencyValue = issue.getCustomFieldValue(issueEmergency).getValue()
 
newSummary = "(" + emergencyValue +  ") " + issueSummary + " - approval task."
oldSummary = issueSummary + " - approval task."
 
if(emergencyValue.equals("Emergency")){
issue.setPriorityId("1")
issue.setSummary(newSummary)
}
else{
issue.setSummary(oldSummary)
}

Vijay Khacharia
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.
June 29, 2014

Where are you putting the code? In the condition box or Additional Issue actions?

It should go in Additional Issue Actions.

Updated code that works is here.

def issueEmergency = customFieldManager.getCustomFieldObject('customfield_12601')
def issueSummary = issue.getSummary()
def issuePriority = issue.getPriorityObject()
  
emergencyValue = issue.getCustomFieldValue(issueEmergency).getValue()
  
newSummary = "(" + emergencyValue +  ") " + issueSummary + " - approval task."
oldSummary = issueSummary + " - approval task."
  
if(emergencyValue.equals("Emergency")){
issue.setPriorityId("1")
issue.setSummary(newSummary)
} 
else{
issue.setSummary(oldSummary)
}

Vijay Khacharia
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.
June 29, 2014

didnt see your latest comment. Good that it works now ;)

Warren McInnes
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.
June 29, 2014

Thanks man...

It's so strange - I had to add an option "Not an Emergency" inorder of it to work, won't work on the default value of "None"

But it's fine, as long as it some somehow, I am happy :)

Warren McInnes
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.
June 29, 2014

In doing all of this, Can we also set the assignee? I would prefer something like:

set assignee from project role "Approvers" (1 user)? Can you assist to add to the above CODE?

Vijay Khacharia
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.
June 30, 2014

The script to read the user in project role is available here.

https://answers.atlassian.com/questions/13140/how-to-list-all-members-of-a-certain-project-role

What you can do is to use the script to get the Approver and do issue.setAssignee("approver_user")

This should work but not tested though

Warren McInnes
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.
June 30, 2014

Yes, I added it in as follows, But didn't work - Once again just doesn't do anything and not even create the subtask? I may have put it together incorrectly?

ProjectRoleManager projectRoleManager = componentManager.getComponentInstanceOfType(com.atlassian.jira.security.roles.ProjectRoleManager.class)
ProjectManager projectManager = componentManager.getProjectManager()

def projectRole =  projectRoleManager.getProjectRole("Approvers")
def actors = projectRoleManager.getProjectRoleActors(projectRole, issue.getProjectObject())
def approver = projectRoleManager.getProjectRoleActors(projectRole, issue.getProjectObject()).roleActors.find { true }

def issueEmergency = customFieldManager.getCustomFieldObject('customfield_12601')
def issueSummary = issue.getSummary()
def issuePriority = issue.getPriorityObject()
 
emergencyValue = issue.getCustomFieldValue(issueEmergency).getValue()
 
newSummary = "(" + emergencyValue +  ") " + issueSummary + " - approval task."
oldSummary = issueSummary + " - approval task."
 
if(emergencyValue.equals("Emergency")){
issue.setPriorityId("1")
issue.setSummary(newSummary)
issue.setAssignee(approver.user)
}
else{
issue.setSummary(oldSummary)
issue.setAssignee(approver.user)
}

Vijay Khacharia
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.
June 30, 2014

This worked for me.

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.security.roles.ProjectRole
import com.atlassian.jira.security.roles.ProjectRoleActors
import com.atlassian.jira.security.roles.ProjectRoleManager

ComponentManager componentManager = ComponentManager.getInstance()
ProjectManager projectManager = componentManager.getProjectManager()
ProjectRoleManager projectRoleManager = ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class) as ProjectRoleManager

def projectRole =projectRoleManager.getProjectRole("Approvers")
def actors =projectRoleManager.getProjectRoleActors(projectRole, issue.getProjectObject())
def approver =projectRoleManager.getProjectRoleActors(projectRole, issue.getProjectObject()).roleActors.find { true }

def issueEmergency =customFieldManager.getCustomFieldObject('customfield_12601')
def issueSummary =issue.getSummary()
def issuePriority =issue.getPriorityObject()

emergencyValue =issue.getCustomFieldValue(issueEmergency).getValue()

newSummary ="(" + emergencyValue + ") " + issueSummary + " - approval task."
oldSummary =issueSummary + " - approval task."

if(emergencyValue.equals("Emergency")){
issue.setPriorityId("1")
issue.setSummary(newSummary)
issue.setAssignee(approver.user)
}
else{
issue.setSummary(oldSummary)
issue.setAssignee(approver.user)
}

Warren McInnes
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.
June 30, 2014

Thanks, Works GREAT!!! What did I miss?

Vijay Khacharia
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.
June 30, 2014

I think it was not finding the componentManager. thats the only difference I see.

Warren McInnes
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.
July 1, 2014

Sorry to bother, for some reason once the assignee is changed to the user in the "Approvers" role, If does not send an "Issue Created" Notification? Am I supposed to fire the event in the script? If so, How?

2 votes
Paresh Gandhi
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.
June 26, 2014

try:

import com.atlassian.jira.ComponentManager

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.fields.CustomField

ComponentManager componentManager = ComponentManager.getInstance()

def customFieldManager = componentManager.getCustomFieldManager()

def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Emergency"}

def cfgvalue = issue.getCustomFieldValue(cf)

issue.setSummary(cfgvalue + newSummary) 

issue.priorityId = '1'

crf
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 27, 2014

Do not use ComponentManager. Use ComponentAccessor when dependency injection isn't available.

Warren McInnes
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.
June 29, 2014

Hi Pareshkumar Gandhi,

Thanks for your effort, However I only need the priority to be set "IF" ITO Change Control is set to Emergency and then I need the value of the Customfield "ITO Change Control" if it's "Emergency" to be added to the summary on subtask creation?

Thanks

0 votes
Warren McInnes
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.
June 26, 2014

I would think thick couls work, but for some reason it does not?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager

def issueEmergency = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'ITO Emergency Change'}
def issueSummary = issue.getSummary()
def issuePriority = issue.getPriorityObject()

newSummary = issue.setCustomFieldValue(issueEmergency, "Emergency") + issueSummary

if (issueEmergency.equals("Emergency")){
issue.setPriorityObject("1")
issue.setSummary(newSummary)
}
else {
    return null
}

Please advise?

Naresh Gundu November 7, 2017

I have multiple subtasks under parent issue 
where i want to copy the attachment&links of subtasks to parent issue.
could you some help me of writing groovy script in postfunction.

0 votes
Warren McInnes
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.
June 26, 2014

I need something almost like this, but that works. Please assist:

def issueEmergency = customFieldManager.getCustomFieldObject('customfield_12601')


def issueEmergency = customFieldManager.getCustomFieldObjectByName('ITO Emergency Change')

def oldSummary = issue.getSummary()
def issuePriority = issue.getPriorityObject()

if (issueEmergency.equals("Emergency")){
return issue.getCustomFieldValue(Emergency) + issue.getSummary()
return issuePriority.setPriorityObject('1')

} else if(issueEmergency.equals("None")) {

return issue.getSummary()
} else {
    return null
}

0 votes
Warren McInnes
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.
June 25, 2014

Hi Guys, I need to change the Priority as well?

I was thinking along the lines of:

def issuePriority = issue.getPriorityObject()

if (Emergency == "Emergency") {
return issue.setPriorityObject('1')
} else (Emergency == "None") {
return issue.setPriorityObject(issue.getPriorityObject())
} else {
    return null
}

Would there be a way to merge the 2 pieces of code to work in one script?

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events