set Group picker field using groovy script in post function

set Group picker field using groovy in post function is not working

My code is working if i test script from script fields and also updating value in database, when i use same code from create post function values are not saving to database

did i miss anything?

Here is the code:

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;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem;
import com.atlassian.jira.issue.util.IssueChangeHolder;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.crowd.embedded.api.Group;
import com.atlassian.jira.user.util.UserUtil;
try{
     customFieldManager = ComponentManager.getInstance().getCustomFieldManager()
     cfCustomerId = customFieldManager.getCustomFieldObjectByName("Customer ID")
     cfCustomerIdValue = issue.getCustomFieldValue(cfCustomerId)
     userUtil = ComponentManager.getInstance().getUserUtil()
     
     cfTestGroupPicker = customFieldManager.getCustomFieldObjectByName("Test Group Picker")
     cfTestGroupPickerValue = issue.getCustomFieldValue(cfTestGroupPicker)
                 
     Map<String, ModifiedValue> modifiedFields = issue.getModifiedFields();
     FieldLayoutItem fieldLayoutItem = ComponentManager.getInstance().getFieldLayoutManager().getFieldLayout(issue).getFieldLayoutItem(
            cfTestGroupPicker);
     DefaultIssueChangeHolder issueChangeHolder = new DefaultIssueChangeHolder();    
     Group groupValue = userUtil.getGroupObject("customer_"+cfCustomerIdValue);
          List<Group> groupList = new ArrayList<Group>()
            groupList.add(groupValue)
    cfTestGroupPicker.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cfTestGroupPicker), groupList), issueChangeHolder);
    cfTestGroupPickerValue = issue.getCustomFieldValue(cfTestGroupPicker)
    log.debug("cfTestGroupPickerValue...."+cfTestGroupPickerValue)
  }
catch(Exception all)
{
  log.error("Exception While updating group custom field : "+all)
}

6 answers

This widget could not be displayed.

Please use the {code} macro to format this properly. What position is the post-function?

This widget could not be displayed.

could you please explain what is {code} macro? i have never used it before. groovy script call at 3rd position

This widget could not be displayed.

done

This widget could not be displayed.

thanks Matheus... now I can actually read it.

This widget could not be displayed.

I would use:

issue.setCustomField ( cfTestGroupPicker  )

 

This should work providing this function comes before the one that is "saves the issue and reindexes", or similar.

This widget could not be displayed.

I do not see method called issue.setCustomField ( cfTestGroupPicker ), assuming you are referring to setCustomFieldValue():

I have added following to my script, but when I create an issue I got the following error on UI, no error in the logs

Error creating issue: Error occurred while creating issue. This could be due to a plugin being incompatible with this version of JIRA. For more details please consult the logs, and see: http://confluence.atlassian.com/x/3McB

Any idea? Same script works fine from Script Fields

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;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem;
import com.atlassian.jira.issue.util.IssueChangeHolder;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.crowd.embedded.api.Group;
import com.atlassian.jira.user.util.UserUtil;
 
try{ 
     customFieldManager = ComponentManager.getInstance().getCustomFieldManager()
     cfCustomerId = customFieldManager.getCustomFieldObjectByName("Customer ID")
     cfCustomerIdValue = issue.getCustomFieldValue(cfCustomerId)
     userUtil = ComponentManager.getInstance().getUserUtil()
 
     cfTestGroupPicker = customFieldManager.getCustomFieldObjectByName("Test Group Picker")
     cfTestGroupPickerValue = issue.getCustomFieldValue(cfTestGroupPicker)
     log.debug("Before cfTestGroupPickerValue...."+cfTestGroupPickerValue)
     Object o = "customer_"+cfCustomerIdValue
     issue.setCustomFieldValue(cfTestGroupPicker, o)                
     Map<String, ModifiedValue> modifiedFields = issue.getModifiedFields();
     FieldLayoutItem fieldLayoutItem = ComponentManager.getInstance().getFieldLayoutManager().getFieldLayout(issue).getFieldLayoutItem(
            cfTestGroupPicker);
     DefaultIssueChangeHolder issueChangeHolder = new DefaultIssueChangeHolder();
     Group groupValue = userUtil.getGroupObject("customer_"+cfCustomerIdValue);
     log.debug("groupValue..."+groupValue);
       List<Group> groupList = new ArrayList<Group>()
         groupList.add(groupValue)
    cfTestGroupPicker.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cfTestGroupPicker), groupList), issueChangeHolder);
    cfTestGroupPickerValue = issue.getCustomFieldValue(cfTestGroupPicker)
    log.debug("After cfTestGroupPickerValue...."+cfTestGroupPickerValue)
   }
catch(Exception all)
{
  log.error("Exception While updating group custom field : "+all)
}

Above script works fine with Script Fields by passing in Issue key in "Preview Issue Key". Using the same script in create Post function throws following error: Error creating issue: Error occurred while creating issue. This could be due to a plugin being incompatible with this version of JIRA. For more details please consult the logs, and see: http://confluence.atlassian.com/x/3McB Please suggest, did i miss any step while creation Note: my post function is at 3rd position in create Post functions

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

269 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