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

Deleted user April 23, 2014

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

1 vote
Tsol
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.
April 23, 2014

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)

}

Tsol
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.
April 23, 2014

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

1 vote
Boris Georgiev _Appfire_
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.
April 23, 2014

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 ?

JamieA
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.
April 23, 2014

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...

0 votes
Deleted user April 24, 2014

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.

Sven Winkelmann August 20, 2014

do you've a working example?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events