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?
Community moderators have prevented the ability to post new answers.
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
Hi Vijay, This script did not work? I am not sure why, but I will try to see. Thanks for your effort
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
What was the error?
Vijay
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 ;)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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) }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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) }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
didnt see your latest comment. Good that it works now ;)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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) }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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) }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks, Works GREAT!!! What did I miss?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I think it was not finding the componentManager. thats the only difference I see.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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'
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Do not use ComponentManager. Use ComponentAccessor when dependency injection isn't available.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Community moderators have prevented the ability to post new answers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.