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

0 votes
Accepted answer

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 votes
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

Can anyone help?

Very late but this is more accurate -- 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.component.ComponentAccessor

String fieldName = 'Ticket Cost'

/** trying to cast toString() to double because I'm not sure that OP is using a number field or not **/
Double value
try {
value = Double.valueOf( cfValues[fieldName]?.toString() )
} catch ( e ) {
return false /** not a number, apparently **/
}

if ( value >= 200 ) {
return ComponentAccessor.getGroupManager().isUserInGroup( currentUser, 'WHCM' )
} else {
return true
}

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted 2 hours ago in Statuspage

How do your teams prepare for really high (planned) traffic days like Cyber Monday?

Hi there! Shannon from Statuspage here.  👋  With Cyber Monday quickly approaching, we're looking to hear from Atlassian customers – specifically from teams who touch incident response li...

10 views 0 2
Join discussion

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