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

0 vote

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

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

0 vote

done

0 vote

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

0 vote

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.

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

347 views 1 18
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