It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

ScriptRunner Custom Field

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 Community Leader Aug 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.

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!

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?

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()
}

managed with the post function. Thanks for the help 

1 vote

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()
}

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

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.

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}"

}
}
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

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
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

314 views 1 3
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you