Forums

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

Set assignee based on field value

Callum Carlile _Automation Consultants_
Community Champion
January 30, 2019

I need to insert a post function into a workflow that says if a custom field value is <200, set the assignee to Person A, and if the value is greater than or equal to 200, set the assignee to Person B.

I have scriptrunner, please may someone help me with this script!

1 answer

Callum Carlile _Automation Consultants_
Community Champion
January 30, 2019

Hi @Fazila Ashraf, thanks for your reply.

In that code;

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

String userName;

switch(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("fieldname").toString()){
    case "1": userName = "user1";break;
    case "2": userName = "user2";break;
    case "3": userName = "user3";break;    
}

issue.setAssignee(ComponentAccessor.getUserManager().getUserByName(userName))

I am not sure where/how to define the different cases. I think i should be using the statements 

if (number >= 200)

and

if (number < 200)

somewhere in the code, but I am unsure how to incorporate this. I'm afraid I am very new to ScriptRunner and groovy, any help would be hugely appreciated! 

Fazila Ashraf
Community Champion
January 30, 2019

Yes, you should be using an 'If .. else" statement for your use case

Callum Carlile _Automation Consultants_
Community Champion
January 30, 2019

I have got the following code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption

def number = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10202").getValue(issue)
if ((int) number >= 200){
issue.setAssignee(ComponentAccessor.getUserManager().getUserByName("francismiers"))
}
else{
issue.setAssignee(ComponentAccessor.getUserManager().getUserByName("charlotteflowerdew"))
}

log.error(number)

The error from the log says: "Cannot invoke method getValue() on null object".

I'm sure this is a minor detail I have got wrong, but like i said i am new to groovy so any advice would be apprieciated. 

Callum Carlile _Automation Consultants_
Community Champion
January 31, 2019

For anyone else interested, the code below worked for me:

import com.atlassian.jira.component.ComponentAccessor

log.info(issue)

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjectByName("Value (£)")

def number = customField.getValue(issue)

if ((int) number >= 200){
issue.setAssignee(ComponentAccessor.getUserManager().getUserByName("Username A"))
}
else{
issue.setAssignee(ComponentAccessor.getUserManager().getUserByName("Username B"))
}

Suggest an answer

Log in or Sign up to answer