Update a custom field with the value found in a project role when the project role is updated

Alex Sprague June 10, 2021

I am in the process of trying to come up with a way to create a listener that looks for a change in a specific project role and then goes into the custom field and updates the value to the value in the project role. 

Example is project role is called Project Manager. Someone edits the role and changes it from user a to user b, This triggers a script to take the users account id value for this project role and update a separate custom field called PM with that value for all tickets in that project.  

 

All tickets in a project will now have the PM custom field filled in with the value of user b instead of user a now.

 

Is this something that can be done with the Jira Cloud API? Any input would be wonderful.

1 answer

1 accepted

1 vote
Answer accepted
Max Lim _Adaptavist_
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.
June 11, 2021

This can be achieved with Scripted Fields, instead of Script Listeners. There is no such event for listener to listen on.

To do that, navigate ScriptRunner > Scripted Fields:

1. Make appropriate configurations.

2. Under Field Type, choose "Text field".

3. Under Script to execute, use following snippet:

def projectKey = issue.fields.project.key

def roleDetails = get("/rest/api/3/project/$projectKey/roledetails")
.header('Content-Type', 'application/json')
.asObject(List)
.body as List<Map>

def adminRoleId = roleDetails.find({it.name == "Administrators"}).id //Replace with your role name

def roleActors = get("/rest/api/3/project/$projectKey/role/$adminRoleId")
.header('Content-Type', 'application/json')
.asObject(Map)
.body
.actors as List<Map>

return roleActors*.displayName.join(', ')

4. Once setup, when viewing issue, you might need to click on the ScriptRunner icon (under the issue description) for the scripted field to show for the first time.

Hope this helps!

WW
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.
September 24, 2021

@Max Lim _Adaptavist_,

Are you saying there are no events that fire when a project role gets a user or group added/removed to/from it?

I'm trying to do something based on when a group gets added to a role in a project under Users and roles (not the permission scheme).  If there are no events for this, that would be good to know so that I can try a different approach.

What about these events?  Do you know what they do?

roleevents.png

I can ask in a separate question, but I thought this one was similar.  That is unless Alex is asking about adding a user to a permission scheme under a role.  Then that's different from what I need to do.

WW
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.
September 24, 2021

Never mind. :)

I read in another post just now that the ProjectRoleUpdatedEvent is the one that fires when adding/removing a user or group to/from a project role.

Suggest an answer

Log in or Sign up to answer