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

Using ScriptRunner to Set Assignee Based on Custom Field of Type User Picker (Single Select)

Hi Everyone,

I have tried customizing many of the scripts that I have seen in these community responses, but I just cannot find one that works exactly and all of my attempts at making it work on my own have failed.  Please help if you can.  I really appreciate your time!

Lesley

 

I am currently using this script to try and update the assignee of a newly created issue to the value of a custom field (user picker, single select).  I am copying the custom field from its linked issue, but it may be that the issue isn't getting fully created before it tries to run this script and doesn't know the value of the custom field yet.

If you have a better idea, here is my use case:  an issue is auto-created as a linked issue (that's working), and I want to set the assignee of this new issue to the value of a custom field, which was filled-in on the original issue.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager

def CustomFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_15741")
ApplicationUser user = issue.getCustomFieldValue(cField) as ApplicationUser
UserManager userManager = ComponentAccessor.getUserManager();
issue.setAssignee((user))

2 answers

1 accepted

0 votes
Answer accepted
Leo Community Leader Mar 09, 2020 • edited

Hi @Lesley_Groh,

can you try below code for assigning ticket after fetching value from custom field

def issueService = ComponentAccessor.getIssueService()

def validateAssignResult = issueService.validateAssign(user, issue.id, issue.reporterId)
issueService.assign(user, validateAssignResult)

 Also if you are using this script in post-function, verify the order you placed 

 

BR,
Leo

Thank you, Leo!  I updated it to this.  I will give it a go.  Yes, I am using a post-function on the "Create" Issue step.  The order in which I am using this script is the last step of the create issue (after "Fire a Issue Created event. . .").

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager

def issueService = ComponentAccessor.getIssueService()
def CustomFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_15741")
ApplicationUser user = issue.getCustomFieldValue(cField) as ApplicationUser
def validateAssignResult = issueService.validateAssign(user, issue.id, issue.reporterId)
issueService.assign(user, validateAssignResult)
UserManager userManager = ComponentAccessor.getUserManager();
issue.setAssignee((user))

Leo Community Leader Mar 09, 2020

Hi, I don't think last 2 lines are required as IssueService,assign will assign the ticket

and if it's not working. try placing this function on top/before Re-index an issue

No, it failed with this error, but did create the ticket and assigned to the project admin (me).

groovy.lang.MissingPropertyException: No such property: customFieldManager for class: Script95
at Script95.run(Script95.groovy:8)

I will try the re-order and removing those last two lines.  Thanks!

Leo Community Leader Mar 09, 2020

I suppose you'll have to modify the custom field line like below 

def cField = customFieldManager.getCustomFieldObjectById("customfield_15741")

I'm sorry for the delay - I had some meetings.

I was performing your suggestions one at a time and trying them.

"I don't think last 2 lines are required as IssueService,assign will assign the ticket" - 

I tried removing the two lines that you suggested, and then, the issue was not assigned to the project admin, instead, the re-assignment to my custom field value failed and the issue remained assigned to the reporter of the linked issue.

Then, I was going to try this:  "and if it's not working. try placing this function on top/before Re-index an issue," but there isn't a Re-index issue step in my create issue.  So, I didn't do that one.

Then, I tried this: "I suppose you'll have to modify the custom field line like below," but I receive two different errors at the script writing level, before I can update the changes:

The first error is:

"Cannot find matching method com.atlassian.jira.issue.CustomFieldManager#getCustomFieldObjectByID(java.lang.S

Please check if the declared type is correct and if the method exists."

The second error is:

""Cannot find matching method com.atlassian.jira.issue.MutableIssue#getCustomFieldValue(java.lang.Object).

Please check if the declared type is correct and if the method exists."

Thanks!

Leo Community Leader Mar 11, 2020

Hi @Lesley_Groh,

I didn't get time to look into it. finally I got it worked like below 

 1. Script I used 

import com.atlassian.jira.component.ComponentAccessor 
import com.atlassian.jira.user.ApplicationUser
def cfUpdate = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Field Name")
def assignee = issue.getCustomFieldValue(cfUpdate) as ApplicationUser issue.setAssignee(assignee)

2. I placed this post-function on top, even before create issue originally 

Post-function-order.png

BR,

Leo

image.png

 

Hi Leo, Unfortunately, I am seeing this error message when I copy the script into the Post Function.  I tried to change it to what it wants, but then, I receive an error below.  Thanks for your continued efforts!  Lesley

Second error after updating line 3:image.png

Leo Community Leader Mar 11, 2020

Hi @Lesley_Groh,

You can ignore the first warning. try to proceed with warning don't update line 3 with suggestions

mostly it should work even if there is a warning. it is working script in my system

BR,

Leo 

YAY!!!  If I could do a back flip, I would!  THANK YOU!!!  It is working.  I appreciate your quick responses and help with getting this to work!!!

0 votes

This worked for me too for a very similar scenario, thanks Leo! (and Lesley for making the post in the first place) :)

Suggest an answer

Log in or Sign up to answer
TAGS

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