Setting Group Picker in Jira post-function

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

This widget could not be displayed.

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?)

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

 

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.

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.

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

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] )

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

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

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)

Thanks a lot Nic. It worked smile

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

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

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

Thanks Anupam,your answer helped me a lot.

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 

This widget could not be displayed.

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.

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

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
Community showcase
Posted Tuesday in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

220 views 1 3
Join discussion

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