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");
}
}
}
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)
>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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
are you sure yor field called "rating"? not "Rating" or something else
it's casesensitive
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
that was intense
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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");
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
dont mind static type checking. if your "rating" field returns Double as you said before it should be fine.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.