Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
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

Groovy script to assign issue based on three custom fields

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

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
Community showcase
Published in Jira Software

👋 Looking for 15-20 volunteers to test Atlassian training content

Hi everyone! Are you interested in beta testing Atlassian University’s newest (unreleased!) training course? We’re looking for 15-20 volunteers to test our newest training course, Basic reporting...

832 views 25 28
Read article

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