ScriptRunner Custom Field

Carmine Fabrizio August 20, 2019

Hi all,

I am using a groovy script in a REST endpoint, and I'd like to use the custom field to get value.

I have this block of values 

def value = [
"a9d43495-9bb2-cdb0-99ea": value1,
"09fb242c-d6f2-a4a0-fea3": value2,
"60dd5447-a7c0-68fd-fb68": value3
]

 and I have a custom field with id customfield_11601, any idea about how can I define it?

I tried this:

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_11601")
def cFieldValue = issue.getCustomFieldValue(cField)

def value = [
"a9d43495-9bb2-cdb0-99ea": value1,
"09fb242c-d6f2-a4a0-fea3": value2,
"60dd5447-a7c0-68fd-fb68": cFieldValue
]

 

but it did not work. Any help will be much appreciated.

Thanks

2 answers

1 accepted

0 votes
Answer accepted
Elifcan Cakmak
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 20, 2019

Hello,

The script is correct. Where are you using this script, on Script Console? Because if that's the case, you are not defining the issue to get the custom field. Also what errors are you getting? 

When I test this script like this, it works:

import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueObject("DSP-2")
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_11802")
def cFieldValue = issue.getCustomFieldValue(cField)

def value = [
"a9d43495-9bb2-cdb0-99ea": "value1",
"09fb242c-d6f2-a4a0-fea3": "value2",
"60dd5447-a7c0-68fd-fb68": cFieldValue
]

return value

Regards.

Carmine Fabrizio August 20, 2019

Hi,

thanks for the reply.

I have created a Jira template with a few custom fields, and with ScriptRunner I have imported a groovy script as REST endpoints

Capture1.PNGCapture2.PNG

 

And I have my key button 

 

Capture3.PNG

You are correct, I am not defining any issue, but which one should I define? 

Thanks!

Carmine Fabrizio August 20, 2019

Ok, got it, if I do 

def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueObject("INF-30")
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_11601")
def cFieldValue = issue.getCustomFieldValue(cField)

It works, but def issue value "INF-30" is static, how can I make it progressive for the new issue?

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 20, 2019

You'll need to pass the issue key in your web item link then parse that out in your rest api.

In web item link, try /rest/scriptrunner/latest/custom/DEPLOY?key=$issue.key

Then in your DEPLOY rest API

def issueKey = queryParams.getFirst('key')
def issue = issueManager.getIssueObject(issueKey)

 Maybe add a bit of error trapping:

if(!issueKey){
return Response.serverError().entiy([error: "You must provide a key parameter"]).build()
}
if(!issue){
return Response.serverError().entiy([error: "The key provided [$issueKey] is not a valid Jira issue."]).build()
}
Carmine Fabrizio September 23, 2019

managed with the post function. Thanks for the help 

1 vote
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 20, 2019

Repeating as a separate answer ... in case you'd like to mark this as the correct answer:

 

You'll need to pass the issue key in your web item link then parse that out in your rest api.

In web item link, try /rest/scriptrunner/latest/custom/DEPLOY?key=$issue.key

Then in your DEPLOY rest API

def issueKey = queryParams.getFirst('key')
def issue = issueManager.getIssueObject(issueKey)

 Maybe add a bit of error trapping:

if(!issueKey){
return Response.serverError().entiy([error: "You must provide a key parameter"]).build()
}
if(!issue){
return Response.serverError().entiy([error: "The key provided [$issueKey] is not a valid Jira issue."]).build()
}
Carmine Fabrizio August 21, 2019

Hi Peter, thanks for the reply

I've configured 

web item link, try /rest/scriptrunner/latest/custom/DEPLOY?key=$issue.key

 and

def issueKey = queryParams.getFirst('key')
def issue = issueManager.getIssueObject(issueKey)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_11601")
def cFieldValue = issue.getCustomFieldValue(cField)

 

but no luck, getting the same null value, do you know how to debug it?

Thanks

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 21, 2019

Make sure your endpoint definition includes the queryParams in the call

DEPLOY(
httpMethod: "POST",
groups: ["jira-administrators","jira-users"]
) {MultivaluedMap queryParams, String body ->
def issueKey = queryParams.getFirst('key')
def issue = issueManager.getIssueObject(issueKey)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_11601")
def cFieldValue = issue.getCustomFieldValue(cField)
/.../
}

And then dump some values to the log ...


/.../
log.info "queryParams = $queryParams"
def issueKey = queryParams.getFirst('key')

log.issueKey "issueKey = $issueKey"
def issue = issueManager.getIssueObject(issueKey)

log.issueKey "issue = $issue"
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def cField = customFieldManager.getCustomFieldObject("customfield_11601")
def cFieldValue = issue.getCustomFieldValue(cField)

 Which of the log outputs are visible in [jira-home]/log/atlassian-jira.log should give you some hints as to what's happening.

Carmine Fabrizio August 22, 2019

Sorry I did not get you. This is what I have, I mean part of the code

 

def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueObject("INF-39")
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cFieldStackName = customFieldManager.getCustomFieldObject("customfield_11601")
def cFieldValueStackName = issue.getCustomFieldValue(cFieldStackName)
def cFieldVpcRange = customFieldManager.getCustomFieldObject("customfield_11602")
def cFieldValueVpcRange = issue.getCustomFieldValue(cFieldVpcRange)
def cFieldSubRange = customFieldManager.getCustomFieldObject("customfield_11603")
def cFieldValueSubRange = issue.getCustomFieldValue(cFieldSubRange)


def value = [
"a9d43495-9bb2-cdb0-99ea-066c0cc54c34": cFieldValueVpcRange,
"09fb242c-d6f2-a4a0-fea3-64b5641f8023": cFieldValueSubRange,
"60dd5447-a7c0-68fd-fb68-65aeb4025d37": cFieldValueStackName
]

def jsonBody = [:]
jsonBody.put("FormValues", value)
jsonBody.put("ProjectID", ProjectName )
jsonBody.put("EnvironmentId", "Environments-1")
jsonBody.put("ReleaseID", newReleaseId)

def bodyjson = JsonOutput.prettyPrint(JsonOutput.toJson(jsonBody))

def newDeployPath = "deployments"
def newDeploy = new HTTPBuilder(URL)
newDeploy.request(POST) { req ->
uri.path = newDeployPath
requestContentType = ContentType.JSON
headers.'X-Octopus-ApiKey' = 'API-xxx'
body = bodyjson

response.success = { resp, JSON ->

return JSON

}

response.failure = { resp ->
return "Request failed with status ${resp.status}"

}
}
Carmine Fabrizio August 22, 2019
2019-08-22 08:27:51,329 https-jsse-nio-443-exec-21 WARN carmine.fabrizio 507x7841x1 1qaetpz 10.100.3.55 /rest/scriptrunner/latest/custom/customadmin/com.onresolve.scriptrunner.canned.common.rest.CustomRestEndpoint [c.o.scriptrunner.runner.RestEndpointManagerImpl] Execution of REST endpoint script failed
groovy.lang.MissingPropertyException: No such property: queryParams for class: Script25
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 22, 2019

What does your rest endpoint declaration line look like?

Seems like you've cut that out of the code you shared. 

Suggest an answer

Log in or Sign up to answer