Scriptrunner Validator

Hi all,

I want to restrict a transition so that only users of a certain group can make the transition, based on the value of a custom field.

For example, if the value of my custom field is greater than 200, then i only want users who are a member of the 'WHCM' group to be able to make this transition.

Below is the code I have so far but I am not able to make the transition even when the custom field value is less than 200 or I am a member of the WHCM group.

If anyone could advise where I am going wrong I would be very greatful :-)

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category;
import com.atlassian.jira.ComponentManager;
import com.opensymphony.workflow.InvalidInputException;
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.opensymphony.util.TextUtils
import com.atlassian.jira.component.ComponentAccessor;

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

CustomField customField_1 = customFieldManager.getCustomFieldObject("13700");
String string_1 = issue.getCustomFieldValue(customField_1);

def groupManager = ComponentAccessor.getGroupManager()
if (issue.getCustomFieldValue("customField_1") > "200") {
return groupManager.isUserInGroup(currentUser, 'WHCM')
}
return true

2 answers

1 accepted

Stephen,

I think there could be a couple issues in your script.

CustomField customField_1 = customFieldManager.getCustomFieldObject("13700");

Is this really the id of your custom field? Could you try using the field name instead of the ID, like this: customFieldManager.getCustomFieldObject("yourFieldNameHere");

It also doesn't seem correct that you're making the value of the custom field a String, if you want to check if it's greater than 200. Should this not be a number instead?

Finally, you should be passing the custom field variable into getCustomFieldValue without quotes. That looks like it's causing the error with getCustomFieldValue.

Revised script:

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category;
import com.atlassian.jira.ComponentManager;
import com.opensymphony.workflow.InvalidInputException;
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.opensymphony.util.TextUtils

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

CustomField customField_1 = customFieldManager.getCustomFieldObjectByName("yourFieldNameHere")
def number = issue.getCustomFieldValue( customField_1 ) as Integer

def groupManager = ComponentAccessor.getGroupManager()
if (number > 200) {
return groupManager.isUserInGroup(currentUser, 'WHCM')
}
0 vote
Alexey Matveev Community Champion Jan 17, 2018

Hello,

Did you try to see the logs? Do you have any error there?

Hi, yes the logs tell me that the condition has failed on my issue. I think the issue is somewhere in my script syntax. But I am not sure where...

2018-01-17 10:45:07,142 http-nio-8080-exec-3 ERROR N455069 645x4375x2 1oumajj 10.44.103.65 /secure/CommentAssignIssue.jspa [c.o.s.c.jira.utils.ConditionUtils] *************************************************************************************
2018-01-17 10:45:07,143 http-nio-8080-exec-3 ERROR N455069 645x4375x2 1oumajj 10.44.103.65 /secure/CommentAssignIssue.jspa [c.o.s.c.jira.utils.ConditionUtils] Condition failed on issue: WHCM-700, built-in script:com.onresolve.scriptrunner.canned.jira.workflow.validators.SimpleScriptedValidator
java.lang.NullPointerException
 at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
 at com.atlassian.jira.issue.Issue$getCustomFieldValue$5.call(Unknown Source)
 at Script50.run(Script50.groovy:18)
2018-01-17 10:45:07,143 http-nio-8080-exec-3 ERROR N455069 645x4375x2 1oumajj 10.44.103.65 /secure/CommentAssignIssue.jspa [c.o.s.c.jira.utils.ConditionUtils] Script follows:
    import com.atlassian.jira.component.ComponentAccessor
    import org.apache.log4j.Category;
    import com.atlassian.jira.ComponentManager;
    import com.opensymphony.workflow.InvalidInputException;
    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.opensymphony.util.TextUtils
    import com.atlassian.jira.component.ComponentAccessor;
    
    CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
    
    CustomField customField_1 = customFieldManager.getCustomFieldObject("13700");
    String string_1 = issue.getCustomFieldValue( customField_1 );
    
    def groupManager = ComponentAccessor.getGroupManager()
    if (issue.getCustomFieldValue("customField_1") > "200") {
    return groupManager.isUserInGroup(currentUser, 'WHCM')
    }
    return true

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Fadoua M. Boualem
Published Monday in Trello

Using Trello to manage events

As a Jira power user, I was at first doubtful that Trello could benefit my workflow. Jira already uses boards (ones you can customize!), so why would I even need to use Trello?! In this post you will...

522 views 8 9
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you