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)

//please provide these values //get your tables API token following the docu and provide it here after the "Basic " def tablesAuth = "Basic <yourEncodedApiToken>" //for manual runs, provide issue key here. When using this in an issue context, e.g. in a postfunction, remove this part def issue = [:] issue.key = "<yourIssueKey>" //get the table custom field ID from the tables list def cfId = "<yurTableFieldId>" //get the column ID from your table's configuration def columnId = "<yourRowId>" //provide the ID of your target custom field here (make sure it's of the correct field type!) def tarCf = "customfield_<yourTargetCustomfieldId>" //read table def tableData = get("https://apps.stagil.com/stjc/rest/stj/1.0/api/table/customField/${cfId}/issue/${issue.key}") .header("Content-Type", "application/json") .header("Accept", "application/json") .header("Authorization", tablesAuth) .asObject(Map) .body.data //determining the target row in the table; Provide an integer (starting at 0 for first entry) or leave as is to always read the latest row def targetRow = tableData.data.size()-1 //identify target value boolean getName = false //for select lists and user pickers, the regular field will return the account ID and the option ID, respectively. set this to true to receive the user's name or the options actual names def fieldId if(!getName) { fieldId = "field-${columnId}" } else { fieldId = "field-${columnId}-name" } def targetValue = tableData[targetRow]["${fieldId}"] logger.info("target value: ${targetValue}") //update issue def fieldMapping = [fields:[:]] fieldMapping.fields << ["${tarCf}":targetValue] def update = put('/rest/api/3/issue/' + issue.key) .header('Content-Type', 'application/json') .header("Accept", "application/json") .queryString("notifyUsers", false) .queryString("overrideScreenSecurity", true) .body(fieldMapping) .asObject(Map) logger.info("RESPONSE ${update.status}: ${update.body}")