Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Automatically Set Approvers based on Requestor's Property using Scriptrunner

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

1 vote
Answer accepted

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

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

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

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

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.

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)

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

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

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

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!

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

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

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

Hi @Alvin Parreno, 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.

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

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)

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

@Alvin Parreno, 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.

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

@Alvin Parreno 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. 

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

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

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

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

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

@Alvin ParrenoI 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. 

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!

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Statuspage

New feature: Slack notifications for Statuspage

We're excited to announce the release of a long-requested feature on Statuspage. Now visitors to your status page can subscribe to get notified in Slack when you report an incident or maintenance. Th...

81 views 0 9
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you