Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,556,301
Community Members
 
Community Events
184
Community Groups

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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
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!!!

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