Automatically Set Approvers based on Requestor's Property using Scriptrunner

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 2, 2018

Hello , I am new to JIRA, can someone help me with 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. 

(i.e. if Requestor 1 creates a ticket, then the post function will read the property of requestor 1 and then finds the Manager and automatically set it to Approvers Field)

Any comments/suggestions are really appreciated. If you can also provide a sample post-function code for this one, that would be great. 

Thanks!

5 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Mauricio Karas
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 5, 2018

Hey, Alvin.

I was able to set the approver based on a property with the following script running as a post-function in the "create issue" transition:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.UserPropertyManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue

//Gets the user property value based on the key.
def propertyKey = 'Manager'; //Property name
UserPropertyManager userPropertyManager = ComponentAccessor.getUserPropertyManager();
def propertyValue = userPropertyManager.getPropertySet(issue.getReporter())?.getString('jira.meta.'+propertyKey);

//Gets the approver based on the property.
ApplicationUser approver = ComponentAccessor.getUserManager().getUserByKey(propertyValue);
List<ApplicationUser> approvers = new ArrayList<>();
approvers.add(approver);

//Updates the field in the issue with the approver.
def groupManager = ComponentAccessor.groupManager;
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def approverField = customFieldManager.getCustomFieldObject("customfield_10307"); //Custom field name
def changeHolder = new DefaultIssueChangeHolder();
approverField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(approverField), approvers), changeHolder);

 You need to change the "propertyKey" value to match the one you've set in your users, I've set mine as "Manager". You also need to make sure that the value of the property in Jira has the correct username of the approver since I'm getting the user with the "getUserByKey" function.

And finally, you need to change the custom field's name to match yours, click on the cog icon(Jira Administration), then Issue > Custom Fields, find the "Approvers" field and click on Edit. In the edit page if you look at the URL you'll see the field id in the last part, "id=10307", then the value you need to put in the script will be "customfield_"+id.

Kind regards,
Maurício Karas

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 5, 2018

Hello @Mauricio Karas , thank you for the detailed explanation. will try your solution later. I will give feedback then.. So much appreciated, +vote . Thanks!

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 5, 2018

Hi Mauricio, how can I check the propertyKey? so I can replace it. Thanks!

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 5, 2018

Hi @Mauricio Karas, already saw it at User Management. It works perfectly . Thank you! How can I give you 5 stars?

Daniel Haws December 21, 2018

Hi @Mauricio Karas, I have been trying to accomplish this in a simple case first, with no success. I stumbled across this thread and it looks to be the most promising, but there must be something fundamental that I am missing and just can't get this to work. The simplified version of your solution is provided here where I just want to explicitly set the approver to a known user, instead of deriving it from a property.

 

I have this script running as the first post function in the "Create Issue" transition, as according to the documentation from Adaptivist:

OnCreate.png

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.UserPropertyManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue

//Gets the user property value based on the key.
////def propertyKey = 'Manager'; //Property name
////UserPropertyManager userPropertyManager = ComponentAccessor.getUserPropertyManager();
////def propertyValue = userPropertyManager.getPropertySet(issue.getReporter())?.getString('jira.meta.'+propertyKey);

//Gets the approver based on explicit user key.
ApplicationUser approver = ComponentAccessor.getUserManager().getUserByKey('someuser');
List<ApplicationUser> approvers = new ArrayList<>();
approvers.add(approver);

//Updates the field in the issue with the approver.
def groupManager = ComponentAccessor.groupManager;
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def approverField = customFieldManager.getCustomFieldObject("customfield_10300"); //Custom field name
def changeHolder = new DefaultIssueChangeHolder();
approverField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(approverField), approvers), changeHolder);

Also, I'm not sure if the groupManager object is necessary, but it doesn't seem to be used.

Any help or guidance is appreciated.

flaimo July 8, 2019

I adopted the script to our custom field number of the approvers field, still I get the following error under 7.13:

 

2019-07-08 15:48:35,563 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2019-07-08 15:48:35,569 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
java.lang.NullPointerException
 at com.atlassian.jira.issue.customfields.impl.MultiUserCFType.getChangelogValue(MultiUserCFType.java:123)
 at com.atlassian.jira.issue.customfields.impl.MultiUserCFType.getChangelogValue(MultiUserCFType.java:80)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.getChangelogValue(ImmutableCustomField.java:376)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:411)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)
 at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source)
 at Script10.run(Script10.groovy:22)
0 votes
Mark_Vincent_T__Paril June 4, 2020

Hi I've been having trouble with automating as well, only finding out that we need to use scripts. May I know if this is a plugin that we need to purchase? and what is its name?

 

 

Thank you!

0 votes
Joerg Strehl October 4, 2018

Sure, here is my code. But i have to say I´m completly new in Jira / Groovy.

@BaseScript FieldBehaviours fieldBehaviours
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.user.UserPropertyManager
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.user.ApplicationUser
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import com.atlassian.jira.bc.user.search.UserSearchService

def desc = getFieldById("description")
def userPropertyManager = ComponentAccessor.getUserPropertyManager()
def userManager = ComponentAccessor.getUserManager()

def approversField = getFieldByName("Approvers BL")

def repfield = getFieldById("reporter")
def reporter = repfield.getFormValue() as String
def rep = userManager.getUserByName(reporter)

String propertyKey2 = "Vorgesetzter"
String propertyKey = "Bereichsleiter"
String propertyValue = null

//Gets the approver based on the user property.

if (rep){
propertyValue = userPropertyManager.getPropertySet(rep)?.getString('jira.meta.'+propertyKey2)

if (propertyValue == null){
approversField.setFormValue("")
}

else{
if (propertyValue){
approversField.setFormValue(propertyValue)

}
}
}
// Showing the Value of the approversField for testing
desc.setFormValue("Value from Field "Approver BL": " +propertyValue)
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.
October 4, 2018

@Joerg Strehl , this is for setting default approver on user picker field on customer portal upon creation right?

Joerg Strehl October 4, 2018

@Alvin, yes I want to set an default approver on an user picker field based on the reporter´s user proprties. The script is running on the "Reporter" field.

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.
October 4, 2018

HI @Joerg Strehl What do you mean you have changed the username?

Joerg Strehl October 4, 2018

@Alvin The usernames of all Jira user were first like "joerg.strehl", but we changed the login to google single sign on and set the email adress as username. The new users are created automatic on first login, the old ones were changed manually via the user management. 

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.
October 4, 2018

Hi @Joerg Strehl So the problem occurs on old ones?

Joerg Strehl October 4, 2018

@Alvinyes, as far as I could test, only the old user was affected.

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.
October 4, 2018

Hi @Joerg Strehl Okay , I assumed you don't need help anymore. Cheers!

Joerg Strehl October 4, 2018

Hi @Alvin thanks for your help. So my script is ok? Or is there an other way to set a value on a user picker field?

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.
October 4, 2018

@Joerg Strehl, have you tried setting it without the property? I am trying to set value on user picker field using the current user

Joerg Strehl October 4, 2018

@AlvinI tried to set the approver manually to one specific old user, if i use the new username the username is showed in the "approver" field, but when i click on create it doesn´t work. If i use the old username nothing is showing in the "approver" field. 

0 votes
Joerg Strehl October 3, 2018

Hi, we have a similar requirement in our company. Would it also be possible to apply this solution to the create screen in the customer portal? We would like to have pre-filled the approver field.
Many Thanks

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.
October 3, 2018

Hi @Joerg Strehl , if you are talking about customer portal upon creation, you must use Behavior

Joerg Strehl October 4, 2018

Hi @Alvin, thanks for the quick reply. I try it already with behaviors, but now I have encountered a problem. In the field "approver" the user and also the avatar are displayed but with some users I get the error message "please provide a valid value". We have changed the user name and I suspect that it is related with it.

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.
October 4, 2018

can you show me the code? did you mapped it?

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.
October 4, 2018
0 votes
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 6, 2018

Hi @Mauricio Karas , I encountered a problem, customer account can't capture the Manager. only agents and admin can have an approver, how can I solve this? Thanks

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 6, 2018

I mean, having two values on Manager can't capture them both on Approver

Mauricio Karas
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 6, 2018

Hey, Alvin.

I believe you can use the properties even in users that are only customers.

And as for having two managers, I don't think it's possible to have two values for one property and you cannot have two properties with the same key. I'm not sure what is the issue, can you explain it a little more?

Kind regards,
Maurício Karas

Like Avanish Pathak likes this
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 6, 2018

Hi @Mauricio Karas, Thanks for a very quick assistance. Yep you're right, I can use the properties even in users that are only customers. I would like to have a group of approver (Approver : Team Atlassian) . Is it possilbe? Can I assign approver to a group so that all people on that group can approve request? Thanks!

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 6, 2018

Hi @Mauricio Karas , do you have any ideas on how could I achieve multiple approver based on the requestor property? Thank you

TAGS
AUG Leaders

Atlassian Community Events