Skip to main content
Skip table of contents

How to add a new row to Lookup Manager via ScriptRunner

Scenario

The user can use ScriptRunner to add a row into the Lookup Manager via groovy script.

How-to

Step 1:Go to ScriptRunner Console

Step 2: Prepare the new record data

CODE
import groovy.json.JsonOutput;
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import groovyx.net.http.ContentType
import static groovyx.net.http.Method.*
import groovy.json.JsonOutput
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.akelesconsulting.jira.plugins.rest.LookupService
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.json.JsonSlurper

@WithPlugin("com.akelesconsulting.jira.plugins.LookupManager")

@PluginModule
LookupService lookupService

def log = Logger.getLogger("LookupManager")
log.setLevel(Level.DEBUG)

//Step 1: Set table name. e.g. App Mapping
def tableName = "YOUR_LOOKUP_TABLE_NAME";

log.debug("***** Get table information ****")
def tableListText = lookupService.getTableList()
def tableListArray = new JsonSlurper().parseText( tableListText )
def table = tableListArray.find {table -> table.name.equals(tableName)}
//log.debug("Table information: " + table)
log.debug("Table ID: " + table.id)

log.debug("***** Get column information*****")
def columns = table.columns
//Sample: [[id:4, name:Application], [id:5, name:App Owner]]
log.debug("Column information: " + columns)

//Step 2: Use the column id to build the new record data, E.g. {"4":"Jira","5":"admin"}
def dataString = '{"COLUMN_ID_1":"VALUE_1","COLUMN_ID_2":"VALUE_2"}'

Step 3: Send request to add new row in Lookup Manager

CODE
//Step 3: Set base URL, E.g. http://localhost:2990/jira
def baseUrl = "YOUR_JIRA_BASE_URL";

//Step 4: Set username and password
def userName = "YOUR_USERNAME"
def password = "YOUR_PASSWORD"

def http = new HTTPBuilder(baseUrl + "/rest/lookuprestresource/1.0/lookup" + "/table/" + tableId + "/entries")
http.request(POST) {
            
    headers.'Authorization' = "Basic " + ((userName + ":" + password).bytes.encodeBase64().toString())
    headers.'X-Atlassian-Token' = 'no-check';
    requestContentType = URLENC
    body = "data=" + dataString
    
    response.success = { resp, data ->
        log.warn("SUCCESS: " + resp.status)
        log.warn(data)
    }

    response.failure = { resp, data ->
        log.warn("ERROR: " + resp.status)
        log.warn(data)
    }
}

Demo

Learn More

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.