Add users to multi-user picker field using post-function custom scriptrunner script

Ashvin August 3, 2023

We are using Scriptrunner and Jira Server, 8.20.11.

As part of our change management process and workflow, a certain individual is the approver based on the type of change.  In one case, there are 2 users for a certain change type.

Have a custom script using ScriptRunner post-function to accomplish this need.  

I am able to retrieve the value of the single select field that represents the "type of change", however, i'm not able to set the users to the multi-user picker custom field.

My code:

 

def cfvalue = issue.getCustomFieldValue('Type of Change')

log.warn('Type of Change value = ' + cfvalue.get(null))

if (cfvalue.get(null) ==~ 'Infrastructure')
{
    issue.setCustomFieldValue('Approvers','User1');
    issue.setCustomFieldValue('Approvers','User2');
}
if (cfvalue.get(null) ==~ 'Operational')
{
    issue.setCustomFieldValue('Approvers', 'User3');
}
if (cfvalue.get(null) ==~ 'Software')
{
    issue.setCustomFieldValue('Approvers','User4);
}

 

From the errors, it appears the "user name" has to be an object, but i'm unclear on how to create an object for the users to be used in the setting of the custom field.

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 4, 2023

Hi @Ashvin

Could you please specify what type of field you are using for the Type of Change? Is it a Single Select List?

I am requesting this so I can provide a sample code.

I am looking forward to your feedback.

Thank you and Kind regards,

Ram

Ashvin August 4, 2023

@Ram Kumar Aravindakshan _Adaptavist_  That field is a Cascading Select.  I only need the value from the 1st option/select field

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 10, 2023

Hi @Ashvin

For your requirement, it would be best that you use ScriptRunner HAPI feature.

With HAPI, all you will need to do is:-

def cascadeFieldValue = issue.getCustomFieldValue('Sample Cascade') as String

issue.set {
if (cascadeFieldValue == '[null:Story]') {
setCustomFieldValue('Sample User Picker', 'ram')
} else if (cascadeFieldValue == '[null:Bug]') {
setCustomFieldValue('Sample User Picker', 'admin')
}
}

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

To install HAPI, please update your ScriptRunner plugin to the latest release, i.e. 8.9.0 or at least to version 7.11.0.

Below is a screenshot of the Post-Function configuration:-

post_function_config1.png

post_function_config2.png

 

Below are a couple of test screenshots for your reference:-

1. I create a new ticket and set the parent value in the Cascade Select List to Strory.

 

test1.png

2. Once the issue has been created, I transition the issue to In Progress. As expected, the Sample User Picker is updated to Ram Kumar

test2.png

 

3. Next, I transition the issue back to To Do and update the parent value of the Cascade Select List to Bugtest3.png

4. I transitioned the issue once again to In Progress, and as expected, the Sample User Picker is updated to Admintest4.png

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events