The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

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

IF/Else statement in post function does not work

Filzah Aziz
February 17, 2021

I'm trying to set my assignee based on components and a select custom field value. For some reason, it is not picking up the correct user. When I choose component "Hardware" and Location "Rotterdam", it would be username1. When I choose component "Hardware" and Location "Utrecht", it would also be username1. Same goes with the last option. I'm not too sure why the if-else statement is behaving this way. Any help? Any ideas?

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.customfields.manager.OptionsManager

final String user1 = 'username1'
final String user2 = 'username2'
final String user3 = 'username3'

def one = ComponentAccessor.getUserManager().getUserByName(user1)
def two = ComponentAccessor.getUserManager().getUserByName(user2)
def three= ComponentAccessor.getUserManager().getUserByName(user3)

def cf = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Location'}
def cfConfig = cf.getRelevantConfig(issue)

// select custom field values

def utrecht = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find { it.toString() == 'Utrecht' }
def rotterdam = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find { it.toString() == 'Rotterdam' }
def copenhagen = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find { it.toString() == 'Copenhagen' }

//Component

def isHardwareSelected = issue.components*.name.contains('Hardware')

if(rotterdam && isHardwareSelected){
issue.setAssignee(one )

}else if(utrecht && isHardwareSelected){
issue.setAssignee(two)


}else if(copenhagen&& isHardwareSelected){
issue.setAssignee(three)
}

 

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

1 vote
Answer accepted
Leo
Community Champion
February 17, 2021

Hi @Filzah Aziz 

I don't find any issue with your script except the method you are trying to fetch selected custom field(Location) option.

If I'm not wrong your script choose the 1st one always when hardware is selected irrespective of location, if so that is expected

def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Location")[0]
String val = issue.getCustomFieldValue(cfSelect) as String

if((val == "Utrecht")&& isHardwareSelected){
issue.setAssignee(one )

}else if((val == "Rotterdam") && isHardwareSelected){
issue.setAssignee(two)


}else if((val== "Copenhagen")&& isHardwareSelected){
issue.setAssignee(three)
}

 

Best Regards,

Leo 

Filzah Aziz
February 18, 2021

Yes indeed! Thanks for that- i've added your bit to my script :)