Setting Group Picker in Jira post-function

Rupa Jain January 4, 2017

Hi,

I am using the following Custom Script to set a value for single value Group Picker in my post-function:

It is not giving any error, but is not setting the value as well.

import com.atlassian.crowd.embedded.api.Group 
import com.atlassian.jira.ComponentManager 
import com.atlassian.jira.component.ComponentAccessor; 
import com.atlassian.jira.issue.CustomFieldManager; 
import java.util.List; 
 
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager(); 
def customerName = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_12345")); 
ComponentManager componentManager = ComponentManager.getInstance();
Group watcherGroup = componentManager.getUserUtil().getGroup( customerName[0]);
List<Group> groupList = new ArrayList<Group>() groupList.add(watcherGroup); 
 
def cf = customFieldManager.getCustomFieldObjectByName("Test Group Picker"); 
issue.setCustomFieldValue(cf, [groupList]); 
issue.store();

 

Thanks

 

2 answers

1 accepted

3 votes
Answer accepted
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 4, 2017

I suspect it's what you're doing with customfield_12345 - you seem to be treating it as an array (implying a multi-option field of some sort), but then as though the elements are strings, which they won't be.

What type of field is it?  And what do you expect it to be?

 

(And who is Reg?)

Rupa Jain January 4, 2017

customfield_12345 is a 'nFeed' field and holds a string (it is multi-option autocomplete).

 

Rupa Jain January 5, 2017

I have even printed the value of the Group picker custom field at the end of the script and is displaying the correct value but in '[]'like '[jira-users]'

log.warn(issue.getCustomFieldValue(com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Test Group Picker"))*.name)

 

Just the value is not being displayed on the ticket (I have the custom field on the view screen of JIRA ticket). Is it because of the [] brackets or am I missing something else?

Also I have added the custom script after the post-function 'Creates Issue' and before 'Fire Create Issue event' in the workflow.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 5, 2017

A string is not the same as a multi-option.

You need to work out exactly what this nfeed field is returning as a java object - is it an array of strings?  Is it an list of JIRA options?  Is it a collection of nfeed option objects?

From your use of [] I suspect you've done part of this and discovered it is a collection of some form, and then tried to use it as such (because collection[0] will return the first object in the collection) but you need to work out what the objects actually are.

If it were a standard JIRA custom field, I could tell you, but I don't know what nfeed returns when asked for a field value.

Rupa Jain January 5, 2017

nFeed field returns an array of strings.

M concern is that when I display the value of the group picker custom field (copied from nFeed field), it shows the correct value in square brackets. It just does not show on he screen(the custom field is present on the screen).

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 5, 2017

I'm not sure that it is, but there's a really easy way to confirm it.  Could you try

log.warn ( customerName )

log.warn ( customerName[0] )

Rupa Jain January 8, 2017

'log.warn ( customerName )' and log.warn '( customerName[0] )' display the same value.

Rupa Jain January 8, 2017

Nic,

 

Forget the nFeed field. Even if I try to hard code a JIRA group, it does not work

import com.atlassian.crowd.embedded.api.Group
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import java.util.List;
 
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def groupManager = ComponentAccessor.getGroupManager()
 
def watcherGroup = groupManager.getGroup("jira-developers")
def cf = customFieldManager.getCustomFieldObjectByName("Test Group Picker")
 
issue.setCustomFieldValue(cf, watcherGroup)
log.warn(watcherGroup)
log.warn(cf)
issue.store();
log.warn(issue.getCustomFieldValue(com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Test Group Picker"))*.name)

 

Thanks,

Rupa

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 9, 2017

The code I've got for storing a value is different and may be worth trying instead:

IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();

cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), watcherGroup), changeHolder)
Rupa Jain January 9, 2017

Thanks a lot Nic. It worked smile

Application_Group August 19, 2017

Thanks Nic. Your answer helped solve it for me too!!! :)

jeetu September 7, 2017

Hi Rupa,Anupam ,could you please share the modified script.

Application_Group September 7, 2017

Hi Jeetu,

Here is the modified script that I believe should work for you. This is working for me using JIRA 7.4.1

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder

//Get Issue Object
Issue issue  = issue    //The issue object is internally retrieved in the post-function

//Get instance of Custom Field Manager
def customFieldManager = ComponentAccessor.getComponent(CustomFieldManager)
//Initialize variables to setup group picker & automation type custom field value
def groupManager = ComponentAccessor.getGroupManager()
def singleGroupCf = customFieldManager.getCustomFieldObject("customfield_12700")

def group = groupManager.getGroup(<YOUR JIRA USER GROUP NAME>)
List groupList = new ArrayList()
groupList.add(group)
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
singleGroupCf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(singleGroupCf), groupList), changeHolder)
issue.store()

 Hope it helps.

 

Cheers,

Anupam

jeetu September 11, 2017

Thanks Anupam,your answer helped me a lot.

jeetu October 6, 2017

Hi Anupam ,i am using this script for creating permissions based on the group.I have different workflows for both parent ticket and sub ticket.I am facing issues when i am modifying the parent ticket as sub ticket.because its not adding the respective users to group.do you have any solution for this 

1 vote
adammarkham
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.
January 6, 2017

It seems that nFeed claim they do not support programmatic access to their data. Please see: NFEED-652 

That makes sense as it could be backed by any data source and i'm not sure how nFeed reads and stores that data.

You'll want to raise this with plugin vendor of nFeed to see what they say.

Rupa Jain January 8, 2017

Adam,

This is not a nFeed issue. We are using a similar script to set Issue Security Level and it works fine.

Even if I do not read the value from nFeed and just add a hard-coded group name say 'jira-users', the script still does not work.

Thanks,

Rupa

Rupa Jain January 8, 2017

Actual script used without nFeed:

import com.atlassian.crowd.embedded.api.Group
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import java.util.List;

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def groupManager = ComponentAccessor.getGroupManager()

def watcherGroup = groupManager.getGroup("jira-developers")
def cf = customFieldManager.getCustomFieldObjectByName("Test Group Picker")

issue.setCustomFieldValue(cf, watcherGroup)
log.warn(watcherGroup)
log.warn(cf)
issue.store();
log.warn(issue.getCustomFieldValue(com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Test Group Picker"))*.name)

 

The script is being used in the 'Create' transition of the workflow and placed after 'Creates Issue Originally' post-function.

Suggest an answer

Log in or Sign up to answer