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

Scriptrunner Validator

Stephen Marsh January 17, 2018

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

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Joshua Yamdogo @ 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.
January 19, 2018

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
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.
January 17, 2018

Hello,

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

Stephen Marsh January 17, 2018

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
Stephen Marsh January 19, 2018

Can anyone help?

Steven Behnke
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.
June 13, 2018

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
}
TAGS
AUG Leaders

Atlassian Community Events