Is there a way to automatically set approver based on custom field values of request?

Parham November 22, 2017

we have a custom field which is the list of our teams. is there a way to set approver of the issue based on the chosen item of the custom field.

 

P.S

i tried automation rules, but couldn't find SET APPROVER on action list items.

5 answers

11 votes
Johnson Wang November 30, 2017

Hi @Parham,

Like Paulo said, one solution is to use an add-on like Power Scripts. With a few lines of code, you can easily and quickly set approver based on custom field values of request.

We've put together a short 4 min video showing how to set this up and execute. We've also included the sample script we used, so you can just copy and paste to test it yourself.

Hope this helps!

Johnson

Parham December 17, 2017

thank you Johnson, it was very helpful.

Dermot Tobin May 23, 2018

Hi,

I have used this script in the change workflow at the create stage but the approval email is not triggered. Have tried all possible variations but no luck. It successfully sets the approver but the mail is not triggered. When I set the approver manually it triggers the mail. If you could offer any advice it would be much appreciated.

Thanks

Dermot

Susan Hauth _Jira Queen_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 23, 2018

Hi Dermot,

Mine works when I use a script.  It's just important that I put the script not as the last item in the post-script events.  Where did you place yours?

S

Dermot Tobin May 23, 2018

Hi Susan,

I got it going using the workflow toolbox (you suggested this in another post from me btw thanks!). I tried the script in every possible sequence in the post function to no avail. It's all working as expected now.

Thanks!

Dermot

Deleted user July 10, 2019

I am having the same issue.

afelts October 18, 2021

@Susan Hauth _Jira Queen_Do you still have this script? The link above does not work.

1 vote
Susan Hauth _Jira Queen_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 18, 2021

Hi Everyone,

Sharing a script out that sets the field "Approvers" to a Jira group based on the custom field "office".  This goes in Scriptrunner (Server/DC).

Hope it helps:

 

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue

/**
* Script that Set Approvers depending on the custom field : Office
*/

def customFieldname = "Office" //Name of the custom field to get the value from
def userPickerApprovers = "Approvers" //User Picker Field that will be populated with all users from the selected group in the groupAppvoers field
Map groupsApprovers = ["Boston":"NA Client Services Managers","Toronto":"NA Client Services Managers","Pittsburgh":"NA Client Services Managers","Montreal":"NA Client Services Managers","London":"UK Client Service Managers",
"London Gresham":"UK Client Service Managers","Ho Chi Minh City":"UK Client Service Managers","Sydney":"AU CS and Account Managers","Cape Town":"Cape Town Client Services Managers","Frankfurt":"Frankfurt CS and Account Managers",
"Milan":"Milan CS and Account Managers","Paris":"Paris Client Services Managers"] //Map of the [City:Approvers Group for that City]

MutableIssue issue = issue as MutableIssue
def cfManager = ComponentAccessor.getCustomFieldManager()
def myFieldCf = cfManager.getCustomFieldObjectsByName(customFieldname)?.first()
def fieldValue = issue.getCustomFieldValue(myFieldCf)
if(groupsApprovers.keySet().contains(fieldValue.toString())) {
def groupManager = ComponentAccessor.groupManager
def customFieldManager = ComponentAccessor.customFieldManager

def groupApprovers = groupsApprovers[fieldValue.toString()]
def group = groupManager.getGroup(groupApprovers)
if(group) {
def users = groupManager.getDirectUsersInGroup(group)
users = users - issue.reporter
def approversField = customFieldManager.getCustomFieldObjectsByName(userPickerApprovers)?.first()
def changeHolder = new DefaultIssueChangeHolder()
approversField.updateValue(null,issue,new ModifiedValue(null,users), changeHolder)
}
else{
log.warn("Group ${groupApprovers} not found for issue ${issue.key}")
}
}

afelts October 24, 2021

Thanks!

1 vote
Ivan Simon Padros January 17, 2019

Hi Parham, I do it with the built in Jira Service Desk Automation. I select the actions and "Do" modify issue, select the field Approvers and then choose the approvers for that scenario. The bad thing is that you have to add every approver for each scenario, it cannot be a group.

Noah Hofmann June 29, 2021

Thank you! This solved an issue for me!

Like Binesh Swamy likes this
0 votes
Susan Hauth _Jira Queen_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 1, 2017

Hi Parham,

I do it with a post-function script (using Scriptrunner). 

Susan

Alvin
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 3, 2018

Hi Susan, can you please help me on my problem .

I want to create a simple post function on JIRA Service Desk that automatically sets the Approvers Field based on the requestor's manager. could you provide me some suggestions, or even sample code? thanks

Susan Hauth _Jira Queen_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 11, 2018

Hi alvin,

One simple way would be to keep the requestor's manager in a user property, then just copy it to a field.  I've done that in the past.

Susan

Alvin
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 11, 2018

Thanks Susan, I already achieve it.

Kyle Li December 29, 2019

Hello Susan

Could you please kindly share your script out ?

We also encounter this requirement from our users.

0 votes
Paulo Hennig
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 23, 2017

Hi, Parham.

Unfortunately, this is not possible to achieve without a customization or a specific 3rd party plugin that can proceed with that. But notice that it's possible to have a pre-selected list of your approvals, so if this custom field that you've mentioned is a "User Picker" type, you can navigate through your project configuration and on "Request Types", you can add this field there as hidden and an option will appear for you to set a list of users there.

I'm not sure how your workflow works exactly, but I believe you can customize it according to my statement ahead and maybe have more than 1 approval field. 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events