It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Groovy Script 'Set Priority value' not working

I've set up a listener than runs whenever an update occurs - This part is working, the script is being run but it doesn't update the priority field. There are no errors shown in the code editor... Any ideas?

 

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("rating");

// Incident Priority custom field value
def customFieldValue = mutableIssue.getCustomFieldValue(customField);
log.info("customFieldValue: " + customFieldValue);

def customfieldManager = ComponentAccessor.getCustomFieldManager()
def rating = customfieldManager.getCustomFieldObjectByName("rating");

if (rating >12) {
mutableIssue.setPriorityId("10000");

} else
if (rating>8) {
mutableIssue.setPriorityId("2");

} else
if (rating>3) {
mutableIssue.setPriorityId("3");

} else
if (rating>0) {
mutableIssue.setPriorityId("4");

}
}
}

 

5 answers

1 accepted

0 votes
Answer accepted

FYI this was the final code (working):

 

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

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
MutableIssue mutableIssue = (MutableIssue)event.issue
CustomFieldManager customFieldManager = ComponentAccessor.customFieldManager
CustomField customField = customFieldManager.getCustomFieldObjectsByName("Rating")[0]

def Rating = mutableIssue.getCustomFieldValue(customField)
if (!Rating) {return}

if (Rating>12) {
mutableIssue.setPriorityId("10000")
} else if (Rating>8) {
mutableIssue.setPriorityId("2")
} else if (Rating>3) {
mutableIssue.setPriorityId("3")
} else if (Rating>0) {
mutableIssue.setPriorityId("4")
}
ComponentAccessor.issueManager.updateIssue(currentUser, mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false)

looks like you've made a typo

you've already defined custom field object as customField earlier, so now rating should be a value held in issue, not same custom field object as before

try rewriting like this:

 

def rating = mutableIssue.getCustomFieldValue(customField)

i've updated to this:

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("rating");

// Incident Priority custom field value
def customFieldValue = mutableIssue.getCustomFieldValue(customField);
log.info("customFieldValue: " + customFieldValue);

def customfieldManager = ComponentAccessor.getCustomFieldManager()
def rating = mutableIssue.getCustomFieldValue(customField);

if (rating > 12) {
mutableIssue.setPriorityId("10000");

} else
if (rating > 8) {
mutableIssue.setPriorityId("2");

} else
if (rating > 3) {
mutableIssue.setPriorityId("3");

} else
if (rating > 0) {
mutableIssue.setPriorityId("4");

}
}
}

 

Now im getting an error when i call rating in the If statements: 'cannot find matching method' please check if declared type is correct and the method exists

I think when issue doesnt have the value in the field, it tries to compare null to a number and doesnt like it, so i'd add check like this before your ifs:

if (!rating) {return}

also, are is rating field a number type? or is it something else?

to add, using 

customFieldManager.getCustomFieldObject("rating")

wont work, use 

customFieldManager.getCustomFieldObjectsByName("rating")[0]

or

customFieldManager.getCustomFieldObject("customfield_12345") //using proper id

 because you are not even getting the field object

Thanks for your help!!

I feel like this might work

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

MutableIssue mutableIssue = (MutableIssue)event.issue
CustomFieldManager customFieldManager = ComponentAccessor.customFieldManager
CustomField customField = customFieldManager.getCustomFieldObjectsByName("rating")[0]

def rating = mutableIssue.getCustomFieldValue(customField)
if (!rating) {return}

if (rating >12) {
mutableIssue.setPriorityId("10000")
} else if (rating>8) {
mutableIssue.setPriorityId("2")
} else if (rating>3) {
mutableIssue.setPriorityId("3")
} else if (rating>0) {
mutableIssue.setPriorityId("4")
}

e: fixed couple of typos 

Screenshot 2019-06-25 at 13.14.50.png

Unfortunately I'm still getting a few errors :-(

dont mind static type checking. if your "rating" field returns Double as you said before it should be fine.

It appears to still be failing using this script

The rating field is set using another script runner script here (I made a scripted field and this works):

 

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.user.util.DefaultUserManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.issue.customfields.option.Option


Option optionA = (Option)ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Impact").getValue(issue)
Option optionB = (Option)ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Probability").getValue(issue)

Double product = Double.parseDouble(optionA.toString()) * Double.parseDouble(optionB.toString());

return product

Updated and refactored, now like this:

 

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;

// Create Jira objects
MutableIssue mutableIssue = (MutableIssue)event.issue;
CustomFieldManager customFieldManager = ComponentAccessor.customFieldManager;
CustomField customField = customFieldManager.getCustomFieldObject("rating");

// Incident Priority custom field value
def customFieldValue = mutableIssue.getCustomFieldValue(customField);
def customfieldManager = ComponentAccessor.getCustomFieldManager()
def rating = mutableIssue.getCustomFieldValue(customField);

if (!rating) {return}
else

if (rating > 12) {
mutableIssue.setPriorityId("10000");

} else
if (rating > 8) {
mutableIssue.setPriorityId("2");

} else
if (rating > 3) {
mutableIssue.setPriorityId("3");

} else
if (rating > 0) {
mutableIssue.setPriorityId("4");

}

>It appears to still be failing using this script

logs or any kind of error would always be nice, but I guess we forgot to store the change we've made:

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

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
MutableIssue mutableIssue = (MutableIssue)event.issue
CustomFieldManager customFieldManager = ComponentAccessor.customFieldManager
CustomField customField = customFieldManager.getCustomFieldObjectsByName("rating")[0]

def rating = mutableIssue.getCustomFieldValue(customField)
if (!rating) {return}

if (rating >12) {
mutableIssue.setPriorityId("10000")
} else if (rating>8) {
mutableIssue.setPriorityId("2")
} else if (rating>3) {
mutableIssue.setPriorityId("3")
} else if (rating>0) {
mutableIssue.setPriorityId("4")
}
ComponentAccessor.issueManager.updateIssue(currentUser, mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false)

Time (on server): Tue Jun 25 2019 13:59:40 GMT+0100 (British Summer Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2019-06-25 12:59:40,017 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2019-06-25 12:59:40,017 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
java.lang.NullPointerException
 at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
 at com.atlassian.jira.issue.Issue$getCustomFieldValue$4.call(Unknown Source)
 at Script332.run(Script332.groovy:12)

what kind of events are you listening to with it? because NullPointerException on the 12th row means it's not getting issue from the event

are you sure yor field called "rating"? not "Rating" or something else

it's casesensitive

I changed the rating to Rating and now its failing on the If statement:

 

Time (on server): Tue Jun 25 2019 14:33:00 GMT+0100 (British Summer Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2019-06-25 13:33:00,051 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2019-06-25 13:33:00,052 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
 at Script336.run(Script336.groovy:15)

what's the search template for that "Rating" scripted field? If it's "Free text" then it might explain things, so in this case I'd suggest to switch it to "Number searcher"

It works! Thanks so much for your help Ilya!!

that was intense

Like Peter Brown likes this

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Calling all Jira Cloud users! Give us feedback on our exploration of a new navigation.

Hi everyone! My name’s Matt and I’m a product manager at Atlassian. I work in the navigation & findability space for all our Jira Cloud products. We’ve been working on trying to improve the exp...

1,086 views 17 12
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you