/
Update table field via Scriptrunner (Java API)
Update table field via Scriptrunner (Java API)
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();
// Retrieve the current value of the table field for the given issue
def oldValue = customField.getCustomFieldType().getActualValue(customField, issue) as String;
/* New */
// Check if value is NULL
if (oldValue == null) {
oldValue = '{"values":[]}';
}
// 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);
}
/* New */
// Add new row to existing rows
object.values.push([
"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
]);
// Build the JSON with the updated rows
JsonBuilder builder = new JsonBuilder(object);
// 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")
Related content
Scriptrunner Behaviours
Scriptrunner Behaviours
Read with this
Copy table values via post-function
Copy table values via post-function
More like this
Column Types
Column Types
Read with this
How to copy a table cell value into a custom field (via Scriptrunner)
How to copy a table cell value into a custom field (via Scriptrunner)
More like this
Copy table from parent to child issue on transition with Scriptrunner
Copy table from parent to child issue on transition with Scriptrunner
More like this
Automation
More like this
© 2023, STAGIL