Set custom field in post-function, depending on user group membership

Hi,


We're using Jira service desk for multiple clients (in one project) and have a custom field called "Client". I would like to set this field automatically in a post-function depending on which user group the user is a member of, e.g.

if (user.inGroup("Client A")) { 

	Client = "Client A" }

elseif if (user.inGroup("Client B")) { 

	Client = "Client B" }

...

I've tried using user properties on the user and copying them over as a post-function, but this is going to be tedious and error prone to maintain. I've tried a conditional script runner ("Set Field Value to constant or Groovy expression) but I would need to write one per client, and besides, I can't get the conditional logic for group membership to work.

Other than writing a bespoke groovy script (which is beyond my skills at the moment, and would also cause a maintenance problem) can anyone think of a good way to do this?

thanks,
Mark.

3 answers

I still think the easiest way is using groovy :). If you have a common naming patter for the clinet user groups then it will be possible to make this work and not to require maintanance. You'll get help with the script - I'm sure a couple of people will be able to provide you with samples.

So is there a common naming pattern for the groups ?

yeah, good point. It could be more or less maintenance free, if you just set the group according to whatever group the user is in, excluding jira-users. Or make all your client groups begin with Client...

I think a custom post function should do the work for you

Try the following, with the appropriate changes for your case. I have not tested so possibly there bugs in it.

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue; 


GroupManager groupManager = ComponentAccessor.getGroupManager();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

//custom field client id = xxxxx

CustomField customField_client = customFieldManager.getCustomFieldObject( xxxxx );


if (groupManager.isUserInGroup(issue.getCustomFieldValue( customField_client ), String groupname)) {

issue.setCustomFieldValue(customField_client, value)

}

Related documentation for groovy script runner plugin is here and is very well written and updated.

Thanks Kostas, that's fantastic. I'm still waiting to get access to the test server so I can store script files on there and try this out, but that gives me loads to go on. Thanks.

do you've a working example?

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

177 views 0 4
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you