Forums

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

Scriprtunner: Custom user picker field

Mohamed Darmoul November 22, 2023

Hello Community , 

Can you please help me on creating a script with scriptrunner to set a user pricker field that retrieve the last user who set a status"Traité" in an issue .

Thank you in advance for your help

 

1 answer

2 votes
Bobby Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 22, 2023

Hi @Mohamed Darmoul 

This is the script you need, it can be run in the console: 

import com.atlassian.jira.component.ComponentAccessor

def curIssue = Issues.getByKey('WEB-2')

def changeItems = ComponentAccessor.getChangeHistoryManager().getChangeHistories(curIssue).reverse()

def changeItem = changeItems.find{ changeItem -> changeItem.getChangeItemBeans().find{ changeItemBean -> changeItemBean.field == "status" && changeItemBean.toString == "Done"}}

def author = changeItem.getAuthorObject()

curIssue.update{

    setCustomFieldValue('Custom User Picker Field', author)

}


Just to run through the script, first we get the issue we want to update (this will change if you want to bulk perform this action, or have this script elsewhere).

Next we get the change history of the issue, and reverse that list, as it defaults to earliest first. We want to search from the latest. 

Next we need to find the Change Item we want, so for each Change Item, get get all Change Item Beans. We then search these to see if there is one that changes the status, and that changes the status to the correct status name. Once we get one that matches that criteria, we know we have the correct Change Item. 

Once we have the correct Change Item, we get the Author, and then use HAPI to update the custom field with that user. 

Could you give this a try and let me know how you get on?

Kind regards, 

Bobby

Mohamed Darmoul November 22, 2023

Thank you @Bobby Bailey , I have changed the status name , the ticket and the field name and I got this error 

Scriptrunner.jpg

Bobby Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 22, 2023

Could you let me know what version of ScriptRunner you are using please?

And also, just to confirm, 'Treatment Actor' is a single select user picker field?

Mohamed Darmoul November 22, 2023

The Scriptrunner Version 8.11 and I confirm the "Treatment Actor" isa single select user picker .

Bobby Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 23, 2023

@Mohamed Darmoul , 

This looks like the error checking system in the editor might not be totally in sync with the editor. Could you try just running a simple script that returns “hello world” then paste back in the script and see if the same error is still there?


Kind regards, 

Bobby

Suggest an answer

Log in or Sign up to answer