It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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

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?

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

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

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'? 

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;
}
}

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

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira Service Desk

Tell us how you've implemented Change Management

Hello Community 👋, I'm a product manager at Atlassian, looking at improving change management capabilities across our products. In particular, we're looking at bridging the gap between Dev & ...

315 views 0 6
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you