Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Auto Assignee script doesn't work for NOT NULL scenarios.

Aravindi Amarasinghe
Contributor
February 27, 2018

Hi All

I have this script to assign tickets to users based on values selected in 3 different custom fields. Previously "Detected in Release" was a optional field and 1st "if" statement executed when Detected in Release == null. That worked perfectly and assigned issue to users as expected. Now we have requirement to make it mandatory and have N/A as option. We need to execute "if" when Detected in Release == N/A . But this time it doesn't work and keep assigning tickets to u22222 by skipping the 1st "if". I tried this by using other options in the list. But it acts the same.

In short, Detected in Release doesn't pick options in the list but works fine when "null". 

Can somebody help me to find the error in the script? 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.ComponentManager
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 cfd = customFieldManager.getCustomFieldObjectByName("Detected in release")
def cfDetectedRelease = issue.getCustomFieldValue(cfd)

//Validating values selected in Test Phase and Detected in Release custom fields.
if (cfDetectedRelease == "N/A") {
switch(cfTestPhase){
case "Prod":
userName = "u11111"
break
case "Prod Fix":
userName = "u11111"
break
default :
userName = ""
break
}
}
else {
switch(cfTestPhase){
default :
userName = "u22222"
break
}
}

//Validating the value selected in Project name.
switch(cfProjectName){
case "Project1" :
userName = "p11111"
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().getUser(userName))
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
}

2 answers

1 accepted

3 votes
Answer accepted
Mahesh S
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.
February 28, 2018

You need to use the condition as follows,

String cfDetectedRelease = issue.getCustomFieldValue(cfd)

if (cfDetectedRelease.equals("N/A")) {
}

Please refer to this.

https://www.geeksforgeeks.org/difference-equals-method-java/

Aravindi Amarasinghe
Contributor
February 28, 2018

Hi @Mahesh S

Thanks a lot it works beautifully! :)

Mahesh S
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.
February 28, 2018

Sweet!! :)

0 votes
Vasiliy Zverev
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.
February 28, 2018

Here is refactored code;

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.UpdateIssueRequest
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.event.type.EventDispatchOption

//Defining custom fields
String userName;
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField cf = customFieldManager.getCustomFieldObjectByName("Test Phase")
def cfTestPhase = issue.getCustomFieldValue(cf)

CustomField cfd = customFieldManager.getCustomFieldObjectByName("Detected in release")
def cfDetectedRelease = issue.getCustomFieldValue(cfd)

//Validating values selected in Test Phase and Detected in Release custom fields.
if (cfDetectedRelease == null) {
switch(cfTestPhase){
case "Prod":
userName = "u11111"
break
case "Prod Fix":
userName = "u11111"
break
default :
userName = ""
break
}
}
else {
switch(cfTestPhase){
default :
userName = "u22222"
break
}
}

//Validating the value selected in Project name.
CustomField cfp = customFieldManager.getCustomFieldObjectByName("Project Name")
switch(issue.getCustomFieldValue(cfp)){
case "Project1" :
userName = "p11111"
break
}

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

//Assigning the issue to the value picked by userName
if (! userName.isEmpty() ) {
issue.setAssigneeId (userName)
ComponentAccessor.getIssueManager().updateIssue(
ComponentAccessor.getJiraAuthenticationContext().getUser(),
issue,
UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build()) //for compatability with JIRA7
}
Aravindi Amarasinghe
Contributor
March 4, 2018

Hello, 

Thanks for the answer. 

But my scenario is when NOT Null. And the 1st answer worked with it. 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events