Hello all,
We are trying to ensure that one of several specific users is selected within a User Picker (single user) customfield; and that the specific user selected is within the department selected within a previous Select List (Single Choice) customfield present to the user.
For example:
We have a Select List (Single Choice) customfield called Dept which contains all departments within our group. This field asks the submitter to select their corresponding department.
After the user selects the department they are within, a User Picker (single user) customfield named Internal Approver shows up and allows the submitter to select a single user. We only want them to select from one of the following users:
1) Bob Smith (Username = bobSm)
2) Michael M (Username = micmal)
3) Test_user (Username = TestUser1)
I've been trying to create a script that handles all of this, but I can't seem to get it to work. So far I have the following:
cfValues['Dept'] == 'Dept1' && cfValues['Internal Approver'] == 'Bob Smith' || cfValues['Internal Approver'] == 'Michael M' || cfValues['Internal Approver'] == 'Test_User'
Can someone help me here?
Thanks,
Mike
Conclusion:
TLDR for those who don't want to read the back and forth. I wanted to have a validator within my create screen which allowed me to use a Select List (Single Choice) customfield to allow only specific "approvers" per value selectable within the afore mentioned Select List (Single Choice) customfield.
The code below is what we came up with:
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
Map<String, List<String>> departmentApproverMap = [
Dept1: ['bobSm', 'micmal', 'TestUser1'],
Dept2: ['jwillians', 'tessk'],
//etc
]
def deptCf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName('Dept')[0]
def approverCf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName('Internal Approver')[0]
def approverName = issue.getCustomFieldValue(approverCf)?.name as String
def deptValue = issue.getCustomFieldValue(deptCf)?.value as String
//add some code here is you need to do something if either dept or approver fields are empty
//For example
if (!deptValue) {
throw new InvalidInputException(deptCf.id, 'You must select the Department')
}
if (!approverName) {
throw new InvalidInputException(approverCf.id, 'You must select the approver')
}
def validApprovers = departmentApproverMap[deptValue] as List<String>
if(!validApprovers){
log.warn "The script does not contain approval mapping for deparment: $deptValue"
return
}
if (!validApprovers.contains(approverName)) {
def approverUserFullNames = validApprovers.findResults {userName->
def user = ComponentAccessor.userManager.getUserByName(userName)
if(!user){
log.warn "The script contains an invalid username: $userName"
return null
}
return user.displayName
}
throw new InvalidInputException(deptCf.id, "You selected a user that is not in the approved list: ${approverUserFullNames.join(', ')}")
}