Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagegroovy
themeEclipse
firstline1
titledeleteRows usage example
linenumberstrue
collapsetrue
// This script deletes all rows assigned to a current user in status "Closed" in
// a Grid custom field with default configuration named "API Sample" in issue TEST-1

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.plugin.PluginAccessor
import com.atlassian.jira.user.ApplicationUser

// Get issue id
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class)
Issue issue = issueManager.getIssueObject("TEST-1")
Long issueId = issue.getId()

// Get TGNG custom field id
CustomFieldManager customFieldManager = ComponentAccessor.getOSGiComponentInstanceOfType(CustomFieldManager.class)
CustomField tgngCustomField = customFieldManager.getCustomFieldObjectsByName("API Sample").get(0)
Long tgngCustomFieldId = tgngCustomField.getIdAsLong()

ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// Get GridService
PluginAccessor pluginAccessor = ComponentAccessor.getPluginAccessor()
Class apiServiceClass = pluginAccessor.getClassLoader().findClass("com.idalko.tgng.jira.server.api.GridService")
def gridService = ComponentAccessor.getOSGiComponentInstanceOfType(apiServiceClass)

// Read field data
def fieldData = gridService.readFieldData(issueId, tgngCustomFieldId, user, null)
def gridRows = fieldData.getRows()

// Ids of rows to delete
def toDelete = new ArrayList()
for (row in gridRows) {
    def columns = row.getColumns()
    def assignee = columns.get("jassignee")
    if (assignee != null && assignee.get("key") == user.getKey() && columns.get("jstatus") == "Closed") {
        toDelete.add(row.getRowId())
    }
}

gridService.deleteRows(issueId, tgngCustomFieldId, user, toDelete)

The example script checks whether a sample Grid custom field contains values on transition and throws an error otherwise.

Code Block
languagejava
titlereadFieldDataInEditMode usage example
// This script checks that Grid custom field named "API Sample" contains at least one row.
// Can be used only as scripted validator of issue transition. Cannot be run from the Script Console.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.opensymphony.workflow.InvalidInputException
@WithPlugin("tge.cloud")
import com.idalko.tgng.jira.server.api.GridFieldData
import com.idalko.tgng.jira.server.api.GridService

// Get TGNG custom field id
String tgngCustomFieldName = "API Sample"
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField tgngCustomField = customFieldManager.getCustomFieldObjectsByName(tgngCustomFieldName).iterator().next()
Long tgngCustomFieldId = tgngCustomField.getIdAsLong()

// Get user
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// Get GridService
GridService gridService = ComponentAccessor.getOSGiComponentInstanceOfType(GridService)
 
// Read field data
GridFieldData fieldData = gridService.readFieldDataInEditMode(issue, tgngCustomFieldId, user)
 
// Check if the grid is empty
if (fieldData.rows.size() == 0) {
    // Throw an exception with the field id and an error message for TGNG field
    // it will prevent transition with the noted error, user will stay on the transition screen.
    throw new InvalidInputException(tgngCustomField.id, "Add at least one record to grid to proceed")
}


Extract Information from the Grid using the Java API

...