Delete Rows
Delete 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.fields.CustomField;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
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");
// find TGE custom fields
PluginAccessor pluginAccessor = ComponentAccessor.getPluginAccessor();
Class tgeConfigManagerClass = pluginAccessor.getClassLoader().findClass("com.idalko.jira.plugins.igrid.api.config.grid.TGEGridConfigManager");
def tgeConfigManager = ComponentAccessor.getOSGiComponentInstanceOfType(tgeConfigManagerClass);
List<Long> tgeCustomFieldIds = tgeConfigManager.getGridCustomFieldIds();
// get current user
JiraAuthenticationContext jiraAuthenticationContext = ComponentAccessor.getOSGiComponentInstanceOfType(JiraAuthenticationContext.class);
Object userObject = jiraAuthenticationContext.getLoggedInUser();
User user = userObject instanceof ApplicationUser ? ((ApplicationUser) userObject).getDirectoryUser() : (User) userObject;
// get TGE custom field
CustomFieldManager customFieldManager = ComponentAccessor.getOSGiComponentInstanceOfType(CustomFieldManager.class);
CustomField tgeCustomField = customFieldManager.getCustomFieldObjectsByName("TGE_TEST").get(0);
Long tgeCustomFieldId = tgeCustomField.getIdAsLong();
// read the data from the first grid
Class dataManagerClass = pluginAccessor.getClassLoader().findClass("com.idalko.jira.plugins.igrid.api.data.TGEGridTableDataManager");
def tgeGridDataManager = ComponentAccessor.getOSGiComponentInstanceOfType(dataManagerClass);
def rows = tgeGridDataManager.readGridData(issue.getId(), tgeCustomFieldId, null, null, 0, 100, user);
def values = rows.getValues()
StringBuilder returnResult = new StringBuilder();
returnResult.append("Grid rows: " + values + "\n");
if (!values.isEmpty()) {
Map<String, Object> lastRow = values.get(values.size() - 1);
Set<Long> rowIdsToDelete = new HashSet<Long>();
def lastRowId = lastRow.get("id")
rowIdsToDelete.add(lastRowId);
tgeGridDataManager.deleteRows(issue.getId(), tgeCustomFieldId, rowIdsToDelete, user);
returnResult.append("Deleted row # " + lastRowId + "\n")
rows = tgeGridDataManager.readGridData(issue.getId(), tgeCustomFieldId, null, null, 0, 100, user);
values = rows.getValues()
returnResult.append("Grid rows after deletion: " + values + "\n");
} else {
returnResult.append("There are no rows to delete in this grid\n");
}
println(returnResult.toString());
return returnResult.toString();