On issue creation, I'm wanting to set the priority based on a radio button custom field. The custom field options are: Priority 1, Priority 2, Priority 3, and Priority 4. If Priority 1 = set priority of Major, else set priority to Standard. Currently, I have the custom script as a post function after "Creates the issue originally" and before "Re-index an issue to keep indexes in sync with the database" but still creates issues with undefined priority. Here's what I have so far and I'm unable to see any errors in the log.
import org.ofbiz.core.entity.GenericValue import com.atlassian.jira.ComponentManager import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.IssueManager import com.atlassian.jira.issue.MutableIssue import com.atlassian.jira.issue.fields.CustomField import org.apache.log4j.Category log.info("[UpdateParentPriority] Script fired"); MutableIssue mutableIssue = issue; ComponentManager componentManager = ComponentManager.getInstance(); IssueManager issueManager = componentManager.getIssueManager(); CustomFieldManager customFieldManager = componentManager.getCustomFieldManager(); CustomField cf = customFieldManager.getCustomFieldObject("customfield_11671"); // "Incident Priority" def cfValue = issue.getCustomFieldValue(cf).getValue(); // .getValue() parses string value log.info("[UpdateParentPriority] Incident Priority value: " + cfValue); switch (cfValue) { case "Priority 1": mutableIssue.setPriorityId("2"); log.info("[UpdateParentPriority] Priority 1 set to Major"); break; case "Priority 2": mutableIssue.setPriorityId("3"); log.info("[UpdateParentPriority] Priority 2 set to Standard"); break; case "Priority 3": mutableIssue.setPriorityId("3"); log.info("[UpdateParentPriority] Priority 3 set to Standard"); break; case "Priority 4": mutableIssue.setPriorityId("3"); log.info("[UpdateParentPriority] Priority 4 set to Standard"); break; default: log.info("[UpdateParentPriority] ERROR - unable to set issue priority"); break; } log.info("[UpdateParentPriority] Script finished");
Any input would be greatly appreciated.
Edit: Here's the completed working code if anyone else coming across this needs help
import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.issue.MutableIssue import com.atlassian.jira.event.type.EventDispatchOption; import com.atlassian.jira.ComponentManager import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.fields.CustomField import org.apache.log4j.Category log.info("[UpdateParentPriority] Script fired"); MutableIssue mutableIssue = (MutableIssue) issue; ComponentManager componentManager = ComponentManager.getInstance(); CustomFieldManager customFieldManager = componentManager.getCustomFieldManager(); CustomField customField = customFieldManager.getCustomFieldObject("customfield_11671"); // 11671 = Incident Priority CF def customFieldValue = issue.getCustomFieldValue(customField); switch (customFieldValue) { case "Priority 1": mutableIssue.setPriorityId("3"); // 3 = Major log.info("[UpdateParentPriority] Priority 1 set to Major"); break; case "Priority 2": mutableIssue.setPriorityId("10000"); // 10000 = Standard log.info("[UpdateParentPriority] Priority 2 set to Standard"); break; case "Priority 3": mutableIssue.setPriorityId("10000"); // 10000 = Standard log.info("[UpdateParentPriority] Priority 3 set to Standard"); break; case "Priority 4": mutableIssue.setPriorityId("10000"); // 10000 = Standard log.info("[UpdateParentPriority] Priority 4 set to Standard"); break; default: log.info("[UpdateParentPriority] ERROR - unable to set issue priority"); break; } // Update Issue ComponentAccessor.getIssueManager().updateIssue(ComponentAccessor.getUserUtil().getUserObject('automation'), mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false); log.info("[UpdateParentPriority] Script finished");
Community moderators have prevented the ability to post new answers.
You are setting it but you are not saving it. This script worked for me and without the last line, it didn't actually update it. I left it all in long form so you didn't have to figure out what issueManager and userManager were.
import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.issue.MutableIssue import com.atlassian.jira.event.type.EventDispatchOption; MutableIssue mutableIssue = (MutableIssue) issue; mutableIssue.setPriorityId("2"); ComponentAccessor.getIssueManager().updateIssue(ComponentAccessor.getUserUtil().getUserObject('automation'), mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false);
This works perfectly! I was unaware of the event type EventDispatchOption. Thank you for your time.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello:
I am trying this equivalent script during a Create Issue event in the workflow. Getting an error: Source GenericValue cannot be null. It seems the mutableIssue variable is null in the IssueManager.updateIssue() call becuase the issue variable itself is null.
Is there a special case because this is the Create Issue event and perhaps the issue object is not yet populated? Does the logic for setting the priority on a Create Issue need to be different from above?
Thank you for any assistance
This is the stack trace in the log:
===========================================
[c.o.s.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: null, actionId: 1, file: <inline script>
java.lang.IllegalArgumentException: Source GenericValue can not be null.
at com.atlassian.jira.association.NodeAssociationStoreImpl.getSinksFromSource(NodeAssociationStoreImpl.java:34)
at com.atlassian.jira.project.version.DefaultVersionManager.getVersionsByIssue(DefaultVersionManager.java:750)
at com.atlassian.jira.project.version.DefaultVersionManager.getFixVersionsFor(DefaultVersionManager.java:595)
at com.atlassian.jira.project.version.DefaultVersionManager.updateIssueFixVersions(DefaultVersionManager.java:565)
at com.atlassian.jira.issue.fields.FixVersionsSystemField.updateIssueValue(FixVersionsSystemField.java:98)
at com.atlassian.jira.issue.fields.AbstractVersionsSystemField.updateValue(AbstractVersionsSystemField.java:362)
at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:704)
at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:669)
at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:655)
at com.atlassian.jira.issue.IssueManager$updateIssue.call(Unknown Source)
at Script3779.run(Script3779.groovy:173)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@William Rojas (Black Diamond), I know JIRA's API had some changes to the GenericValue and ComponentManager classes from v6 to v7 that effected the code above. Here's my most recent working code for the original question, should it be at all helpful for anyone.
This script gets custom field value from object "customfield_XXXXX" which is a set of 4 radio buttons, and assigns the default issue priority to either major or standard depending on that custom field value, all by a application user "adminUser" who has valid privileges.
import com.atlassian.jira.issue.MutableIssue; import com.atlassian.jira.issue.CustomFieldManager; import com.atlassian.jira.issue.fields.CustomField; import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.event.type.EventDispatchOption; import com.atlassian.jira.event.issue.AbstractIssueEventListener; import com.atlassian.jira.event.issue.IssueEvent; import com.atlassian.jira.user.ApplicationUser; import com.atlassian.jira.issue.UpdateIssueRequest; import org.apache.log4j.Category; class UpdateParentPriority extends AbstractIssueEventListener { @Override // Fires on event Issue Updated void workflowEvent(IssueEvent event) { // Create Jira objects MutableIssue mutableIssue = (MutableIssue)event.issue; CustomFieldManager customFieldManager = ComponentAccessor.customFieldManager; CustomField customField = customFieldManager.getCustomFieldObject("customfield_XXXXX"); // Create adminUser user object ApplicationUser adminUserAppUser = ComponentAccessor.getUserManager().getUserByKey("adminUser"); log.info("adminUserAppUser: " + adminUserAppUser); // Incident Priority custom field value def customFieldValue = mutableIssue.getCustomFieldValue(customField); log.info("customFieldValue: " + customFieldValue); // Set default priority based on Incident Priority def majorPriorityID = "3"; def standardPriorityID = "10000"; switch (customFieldValue) { case "Priority 1": mutableIssue.setPriorityId(majorPriorityID); log.info("Priority 1 - set to Major"); break; case "Priority 2": mutableIssue.setPriorityId(standardPriorityID); log.info("Priority 2 - set to Standard"); break; case "Priority 3": mutableIssue.setPriorityId(standardPriorityID); log.info("Priority 3 - set to Standard"); break; case "Priority 4": mutableIssue.setPriorityId(standardPriorityID); log.info("Priority 4 - set to Standard"); break; default: log.info("ERROR - unable to set issue priority"); break; } // Update issue as adminUser user ComponentAccessor.getIssueManager().updateIssue(adminUserAppUser, mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false); log.info("Script finished"); } // end workflowEvent() } // end class UpdateParentPriority
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Would this work using Issue Type value to set Priority? Like if issue type = A, set priority to high. If issue type = B, set priority to medium.
I know Issue Type is not a custom field/
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, I am trying to achieve the similar results and when I use the code above, it works but gives me several warnings:
Line 12: "Public use of this method is deprecated - please use ComponentAccessor instead. Since v 5.2. @ Line 12, Column 37" Line 13: "Get this component ejected in your constructor or use ComponentAccessor for static access instead. Since v 5.2. @ Line 13, Column 41" Line 45: "User UserUtil.getUserByKey(String) or UserUtil.getUserByName(String) instead. Since v 6.0.. @ Line 45, Column 49"
I am worried that we will base our critical workflow on it and it stops working with the next update. Please advise.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It's a bit counter-intuitive, but try putting your post-function before the system post-function Creates the issue originally. That resolves a similar issues with setting the assignee.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Or replace log.info to log.error.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Currently info logs are not logged. You need to set the log level to INFO for your script. Try to add "package your.package" at the top of the script and set log level as described here: https://confluence.atlassian.com/display/JIRAKB/How+to+set+logging+level+for+a+package+in+JIRA
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey Dylan
Have you tried using
setPriority(componentAccessor.getConstantsManager().getPriorityObject("2"))
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You have good logging here. What do you get in logs/console?
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.