Confluence has been updated to version 6.15.9

Page tree
Skip to end of metadata
Go to start of metadata


Edit Grid
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.plugin.PluginAccessor;
import com.atlassian.jira.user.ApplicationUser;
  
//this script edits data in a grid named 'TGE_TEST' with the default configuration, in issue 'TEST-1'
  
// get an issue
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class);
Issue issue = issueManager.getIssueObject("TEST-1");
  
// get TGE custom field
CustomFieldManager customFieldManager = ComponentAccessor.getOSGiComponentInstanceOfType(CustomFieldManager.class);
CustomField tgeCustomField = customFieldManager.getCustomFieldObjectsByName("TGE_TEST").get(0);
Long tgeCustomFieldId = tgeCustomField.getIdAsLong();
  
// get current user
JiraAuthenticationContext jiraAuthenticationContext = ComponentAccessor.getOSGiComponentInstanceOfType(JiraAuthenticationContext.class);
Object userObject = jiraAuthenticationContext.getLoggedInUser();
User user = userObject instanceof ApplicationUser ? ((ApplicationUser) userObject).getDirectoryUser() : (User) userObject;
  
// read grid data
PluginAccessor pluginAccessor = ComponentAccessor.getPluginAccessor();
Class dataManagerClass = pluginAccessor.getClassLoader().findClass("com.idalko.jira.plugins.igrid.api.data.TGEGridTableDataManager");
def tgeGridDataManager = ComponentAccessor.getOSGiComponentInstanceOfType(dataManagerClass);
  
StringBuilder result = new StringBuilder();
def callResult = null;
try {
    callResult = tgeGridDataManager.readGridData(issue.getId(), tgeCustomFieldId, null, null, 0, 10, user);
    result.append("Grid ID=" + tgeCustomFieldId + " content before modification: " + callResult.getValues() + "\n");
} catch (Exception e) {
    result.append("Grid ID=" + tgeCustomFieldId + " data cannot be retrieved: " + e.getMessage() + "\n");
}
  
// let's edit the data
Class changeSetClass = pluginAccessor.getClassLoader().findClass("com.idalko.jira.plugins.igrid.api.data.dto.TGEChangeSet");
List changes = new ArrayList();
for (Map<String, Object> row : callResult.getValues()) {
    Long rowId = (Long) row.get("id");
    String summary = row.get("isummary");
  
    Map<String, Object> newRowValues = new HashMap<String, Object>();
    newRowValues.put("isummary", summary + "_changed");
  
    def changeSet = changeSetClass.newInstance();
    changeSet.setRowId(rowId);
    changeSet.setValues(newRowValues);
    changes.add(changeSet);
}
  
try {
    tgeGridDataManager.applyChanges(issue.getId(), tgeCustomFieldId, changes, user);
    result.append("Grid ID=" + tgeCustomFieldId + " was successfully updated!\n");
} catch (Exception e) {
    result.append("Grid ID=" + tgeCustomFieldId + " data cannot be updated: " + e.getMessage() + "\n");
}
  
// check if data was changed
try {
    callResult = tgeGridDataManager.readGridData(issue.getId(), tgeCustomFieldId, null, null, 0, 10, user);
    result.append("Grid ID=" + tgeCustomFieldId + " content after modification: " + callResult.getValues() + "\n");
} catch (Exception e) {
    result.append("Grid ID=" + tgeCustomFieldId + " data cannot be retrieved: " + e.getMessage() + "\n");
}
  
println(result.toString());
return result.toString();
  • No labels