set Group picker field using groovy script in post function

Srilatha Pokala June 5, 2015

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 votes
Srilatha Pokala June 10, 2015

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)
}
Srilatha Pokala June 10, 2015

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

0 votes
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.
June 8, 2015

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.

0 votes
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.
June 8, 2015

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

0 votes
Matheus Fernandes
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 5, 2015

done

0 votes
Srilatha Pokala June 5, 2015

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

0 votes
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.
June 5, 2015

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

Suggest an answer

Log in or Sign up to answer