Add Rows



Add Rows
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; // 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); //Script Runner can show validation error for this line. Just ignore them. 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; //Script Runner can show validation error for the next two lines. Just ignore them. 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 add some rows Map<String, Object> row = new HashMap<String, Object>(); row.put("isummary", "Some summary"); row.put("iassignee", "admin"); row.put("istatus", "Done"); row.put("idue", new Date().getTime()); //Script Runner can show validation errors here as well. Ignore them. try { List<Long> rowIds = tgeGridDataManager.addRows(issue.getId(), tgeCustomFieldId, Arrays.asList(row), user); result.append("Grid ID=" + tgeCustomFieldId + " data was successfully added. IDs of added rows: " + rowIds + "\n"); } catch (Exception e) { result.append("Grid ID=" + tgeCustomFieldId + " data cannot be added: " + e.getMessage() + "\n"); } // check if data was really added //Ignore the Script Runner validation errors 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();