Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Current »

This script can update:

  • table’s existing rows

  • update table with a new row

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.util.ErrorCollection
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper

// Get the necessary Jira components
def issueService = ComponentAccessor.getIssueService();
def userManager = ComponentAccessor.getUserManager();
def issueManager = ComponentAccessor.getIssueManager();
def customFieldManager = ComponentAccessor.getCustomFieldManager();

// Specify the user to perform the update
def user = userManager.getUserByName("admin");

// Specify the issue to update
def issue = issueManager.getIssueObject("BD-6");

// Specify the table field to update (replace 10347l with the actual custom field ID)
def customField = customFieldManager.getCustomFieldObject(10347l);

def issueInputParameters = issueService.newIssueInputParameters();

// Update rows in the table field

// Retrieve the current value of the table field for the given issue
def oldValue = customField.getCustomFieldType().getActualValue(customField, issue) as String;

// Parse the current value as JSON
def jsonSlurper = new JsonSlurper();
def object = jsonSlurper.parseText(oldValue);

// Assert that the parsed object is a map and its values are a list
assert object instanceof Map
assert object.values instanceof List

// Iterate over each row in the table
object.values.each { row ->
    // Assert that the row is a map
    assert row instanceof Map
    
    // Check if the value of "field-219" in the row is "text"
    if (row.get("field-219") == "text") {
        // Update the value of "field-219" to "change text"
        row.put("field-219", "change text")
    }
    
    // Log the updated row
    log.warn(row);
}

// Build the JSON with the updated rows
JsonBuilder builder = new JsonBuilder(object);

//OR

// Define new column values for the table in JSON format
def newValue = ["values":[
    [
        "field-191": [104],
        "field-219": "text",
        "field-192":"BD-7",
        "field-190":["admin"],
        "field-247":8.5,
        "field-189":false,
        "id":28632 // add existing rowId in the table for this issue for the update
    ],
    [
        "field-191": [105],
        "field-219": "text2",
        "field-192":"BD-7",
        "field-190":["admin"],
        "field-247":"45.5",
        "field-189":false,
        "id":-1 // to add new row
    ]
]]

// Convert the new value to JSON format
JsonBuilder builder = new JsonBuilder(newValue);

// Set the table value using the new JSON value
issueInputParameters.addCustomFieldValue(customField.getId(), builder.toString());

// Skip the check screen
issueInputParameters.setSkipScreenCheck(true);

// Retain existing values when the parameter is not provided
issueInputParameters.setRetainExistingValuesWhenParameterNotProvided(true, true);

// Perform the update
log.warn("start")

// Validate the update parameters
IssueService.UpdateValidationResult validationResult = issueService.validateUpdate(user, issue.getId(), issueInputParameters);

// Check if the validation is successful
if (validationResult.isValid()) {

    // Update the issue with the new values
    IssueService.IssueResult issueResult = issueService.update(user, validationResult);

    // Check if the issue update is successful
    if (!issueResult.isValid()) {
        log.error(issueResult.getErrorCollection().toString());
    }
} else {
    log.error(validationResult.getErrorCollection().toString());
}
log.warn("end")
  • No labels