Groovy Script For Validator
Validator-Groovy Script:That is, you can add a validator of a custom groovy script to the workflow, and use the groovy script in the groovy editor to implement your custom requirements.
How to add groovy script ?
(1) Click Add Validator
(2) Select the custom groovy script and click Add
(3) Enter name and script to run
(4) Click Add and publish the workflow
(5) When performing this conversion on the issue page, if the value of the specified field is empty, a prompt will be displayed on the page
Code examples
① Field Required
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
/**
* Validator
*/
def customFieldName = "Multiple Users Picker";
def textField = new ArrayList<>(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName(customFieldName));
if (Objects.nonNull(textField) && textField.size() > 0){
def value = issue.getCustomFieldValue(textField.get(0))
if (Objects.isNull(value)){
throw new InvalidInputException(customFieldName + " can't be empty");
}
} |
② Verify that only users in custom fields can be converted
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import com.opensymphony.workflow.InvalidInputException
/**
* Verify that only users in custom fields can be converted
*/
// Issue issue;
def customFieldName = "Multiple Users Picker";
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser as ApplicationUser;
def customFieldList = new ArrayList<CustomField>(ComponentAccessor.customFieldManager.getCustomFieldObjectsByName(customFieldName));
if (customFieldList.size() > 0){
def customField = customFieldList.get(0) as CustomField;
def userList = issue.getCustomFieldValue(customField) as List<ApplicationUser>;
if (Objects.isNull(userList) || !userList.contains(currentUser)){
throw new InvalidInputException(currentUser.username +"is not in the custom field "+customField.name)
}
}
|