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

How to validate user on create issue when custom field value selected

Gavin Minnis July 17, 2018

I've created a change request JSD project. It is used to terminate existing employees/contractors. When terminating an employee, I need to ensure that only HR staff can create such a request. Anyone can terminate a contractor.

I have a custom field called "Category" that has options a) Employee b) and Contractor. So if "Employee" is selected, I need some validator that ensures the issue create is a member of the HR Termination project role. I found a script online that I've modified, but it isn't quite working.

The following script throws the error no matter what option is selected, Employee or Contractor. So somehow this code isn't specifically checking to see what value was selected in my Category custom field.

Any help would be appreciated.

 

import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.security.roles.ProjectRoleActors
import com.atlassian.jira.security.roles.ProjectRole
import com.opensymphony.workflow.InvalidInputException
import org.apache.log4j.Category

def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction");
ComponentManager componentManager = ComponentManager.getInstance();
ProjectRoleManager projectRoleManager = ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class) as ProjectRoleManager;

def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10201");

def cfCategory = (String)issue.getCustomFieldValue(customField);

ApplicationUser currentUser = ComponentAccessor.JiraAuthenticationContext.getLoggedInUser();
ProjectRole hrRole = projectRoleManager.getProjectRole("HR Termination");
ProjectRoleActors hr = projectRoleManager.getProjectRoleActors(hrRole, issue.getProjectObject());
Boolean isHR = hr.contains(currentUser);

if (!isHR) {
if (cfCategory == "Employee")
return false
}

1 answer

0 votes
Neta Elyakim
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.
July 18, 2018

Try this:

import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.security.roles.ProjectRoleManager

def projectManager = ComponentAccessor.projectManager
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)

ApplicationUser currentUser = ComponentAccessor.JiraAuthenticationContext.getLoggedInUser();
String customFieldValue = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_10201"));

if(customFieldValue == "Employee"){
def projectRole = projectRoleManager.getProjectRole("HR Termination")
def project = projectManager.getProjectByCurrentKey('IT')// enter your project key

def isExistinProjectRole = projectRoleManager.isUserInProjectRole(currentUser, projectRole, project)

if (!isExistinProjectRole) {
invalidInputException = new InvalidInputException("You don't have permission to open this ticket");
return null;
}
}

 

Notice that you need to change the project key according to your relevant project

Gavin Minnis July 18, 2018

Hi Neta,

Thanks for the response, but the solution is not working. As before, I get a validation error, but it is happening no matter what selection is made. It should only occur when "Employee" is selected.

Any ideas?

Gavin Minnis July 18, 2018

Also to be clear, I'm using a Simple Scripted Validator.

Neta Elyakim
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.
July 18, 2018

The script should run as script validator -> Custom script validator

Gavin Minnis July 18, 2018

OK, I made the change and got the following error:

2018-07-18 15:54:52,874 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-07-18 15:54:52,874 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
groovy.lang.MissingPropertyException: No such property: JiraAuthenticationContext for class: com.atlassian.jira.component.ComponentAccessor
Possible solutions: jiraAuthenticationContext
 at Script4026.run(Script4026.groovy:10)

The custom field "Category" that I am checking against is a single select dropdown field. Does that change the way that we are checking its value for 'Employee'? 

Neta Elyakim
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.
July 18, 2018

No, the script should be fine because we take the field value and the field is a single select so there should be one string that the script should return.

 

 

It seems like the script can take the current user.

Let run the script on the reporter field (how should be automation the current user)

Try this:

import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.security.roles.ProjectRoleManager

def projectManager = ComponentAccessor.projectManager
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)

ApplicationUser reporter = issue.getReporter()
String customFieldValue = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_10201"));

if(customFieldValue == "Employee"){
def projectRole = projectRoleManager.getProjectRole("HR Termination")
def project = projectManager.getProjectByCurrentKey('IT')// enter your project key

def isExistinProjectRole = projectRoleManager.isUserInProjectRole(reporter, projectRole, project)

if (!isExistinProjectRole) {
invalidInputException = new InvalidInputException("You don't have permission to open this ticket");
return null;
}
}
Gavin Minnis July 18, 2018

That seems to have fixed the issue. I'll check a bit more. But I think you've got it. Thanks!

Neta Elyakim
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.
July 18, 2018

Great! if it's work for you, accept the answer :)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events