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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,556,729
Community Members
 
Community Events
184
Community Groups

Update priority field based on value of an existing custom field

I am trying to create a post function that will update the priority field based on the value of a custom field. This is based on an example script from Atlassian Answers, I have included below for reference. needless to say it isn't working - but it doesn't fail which is a start!, so I am trying to add debug statements to see what is happening using "log.info", but I don't see this output anywhere. I have checked the atlassian.jira.log file and see no reference.

The execution information below says no logs.

Time (on server): Tue Apr 04 2017 11:06:37 GMT+0100 (GMT Daylight Time)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
No logs were found for this execution.

 

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_12248");

// 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 = "10000"; // Highest
def standardPriorityID = "5"; // High
switch (customFieldValue)

{ case "Audit (Internal)": mutableIssue.setPriorityId(majorPriorityID); log.info("Set priority to Highest"); break; case "Audit (External)": mutableIssue.setPriorityId(standardPriorityID); log.info("Set priority to Highest"); break; case "IT Problem Management": mutableIssue.setPriorityId(standardPriorityID); log.info("Set priority to High"); break; default: log.info("No need to change the 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

1 answer

2 votes
Daniel Yelamos [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.
Apr 04, 2017 • edited

Hello Roy:

Seems like you took an example that was a bit outdated. I tried this on my machine and it worked fine. Feel free to give it a try and if you come up with any other issues feel free to ask again.

 

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjects(issue)?.find { it.id == "customfield_12248" }

def customFieldVal = issue.getCustomFieldValue(customField)

def majorPriorityID = "10000" // Highest
def standardPriorityID = "5" // High
switch (customFieldVal) {
case "Audit (Internal)":
issue.setPriorityId(majorPriorityID)
log.debug("Set priority to Highest")
break
case "Audit (External)":
issue.setPriorityId(standardPriorityID)
log.debug("Set priority to Standard")
break
case "IT Problem Management":
issue.setPriorityId(standardPriorityID)
log.debug("Set priority to Standard-2")
break
default:
log.debug("No need to change the priority")
break
}

Huge thanks, did the job and a lot cleaner to read.

I found that this didn't work as part of the create transition (I  oved to the last post step, no joy).  So I created a new status after Create, called Set Priority) and set the priority there.  Finally I used a fast track transition so that this transition was executed straight away with no user intervention.  Bit longwinded, but does the trick.  

Thanks again

 

Roy

JamieA
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.
Apr 06, 2017

Hi Roy - this should work on the create transition but it needs to be the first post-function, not the last.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events