Groovy script to assign issue based on three custom fields

aravindi.amarasinghe December 14, 2021

Hi All, 

We have this perfectly working script in our workflow and I want to modify it to add one more field (Cleint) to validate to assign the issue to different users.  I cannot get this Client field add there right. Can someone help me to understand what's wrong with this script? 

We are in JIRA server version 8.13.10

My Requirement is , 

When Global Release is empty,  Test phase is Prod or ProdFix and Client is one of CFL/SBA/CYBG , the assignee should goes to one person , the rest should goes in "business prod support" 

When global release is not empty it checks test phase again for Doc support option , if not it goes in au.global.relelase 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.event.type.EventDispatchOption

//Defining custom fields
String userName;
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Test Phase")
def cfTestPhase = issue.getCustomFieldValue(cf)
def cfp = customFieldManager.getCustomFieldObjectByName("Project Name")
def cfProjectName = issue.getCustomFieldValue(cfp)
def cfg = customFieldManager.getCustomFieldObjectByName("Global Release")
def cfGlobalRelease = issue.getCustomFieldValue(cfg)
def cfc = customFieldManager.getCustomFieldObjectByName("Client")
def cfClient = issue.getCustomFieldValue(cfc)

//Validating values selected in Test Phase and Global Release custom fields.
if (cfGlobalRelease == null) {
switch(cfTestPhase){
case "Prod":
switch(cfClient){
case "CFL":
userName = "e0119168"
break
case "SBA":
userName = "e0119168"
break
case "CYBG":
userName = "e0119168"
break
default :
userName = "business prod support" /* issue.getReporter().getName() */
}
break
}

switch(cfTestPhase){
case "Prod Fix":
switch(cfClient){
case "CFL":
userName = "e0119168"
break;
case "SBA":
userName = "e0119168"
break;
case "CYBG":
userName = "e0119168"
break;
default :
userName = "business prod support" 
}
break;


}


}

 

else {
switch(cfTestPhase){
case "Doc/Helptext":
userName = "e0119138"
break
default :
userName = "au.global.release"
break
}
}



log.error("Username chosen: " + userName)

//Assigning the issue to the value picked by userName
if (! userName.isEmpty() ) {
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
issue.setAssignee(ComponentAccessor.getUserManager().getUserByName(userName))
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
}

 

1 answer

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 15, 2021

Is the script editor showing you any warnings or errors?

Do you get what you expect in the logs from the log.error line?

Could you change the last bits of the "set assignee" code at the bottom, changing the "set assignee" line so it goes into a variable and we can echo that out in the log to check it actually has got a good value:

I would change

issue.setAssignee(ComponentAccessor.getUserManager().getUserByName(userName))
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

to

def newassignee = ComponentAccessor.getUserManager().getUserByName(userName)
log.error ("User object found: " + newassignee
issue.setAssignee(newassignee)
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.13.10
TAGS
AUG Leaders

Atlassian Community Events