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)
}
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/
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sweet!! :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello,
Thanks for the answer.
But my scenario is when NOT Null. And the 1st answer worked with it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.