0

I have some code like this

def lookupTickets() {
    User currentUser = webAuthService.currentUser()
    def http = new HTTPBuilder(zdURL)
    http.auth.basic("${zdUser}/token", zdApiKey)
    http.get(path: "/api/v2/users/search.json", 
             query: [query: currentUser.emailAddress], 
             requestContentType: ContentType.JSON, { resp, json ->
              println "Response status: ${resp.statusLine}"
                  def zenDeskUserId = json?.users[0]?.id
    })
    return MYRESULT
}

The line def zenDeskUserId = json?.users[0]?.id gives me the result I am looking to return to the browser.

How can I return this value in the outer method when it is only in scope from within the inner closure?

Alidad
  • 5,463
  • 1
  • 24
  • 47
benstpierre
  • 32,833
  • 51
  • 177
  • 288

1 Answers1

2

Do you think this will not work?

def lookupTickets() {
    def zenDeskUserId

    User currentUser = webAuthService.currentUser()
    def http = new HTTPBuilder(zdURL)
    http.auth.basic("${zdUser}/token", zdApiKey)
    http.get(path: "/api/v2/users/search.json", 
             query: [query: currentUser.emailAddress], 
             requestContentType: ContentType.JSON, { resp, json ->

                 println "Response status: ${resp.statusLine}"
                 zenDeskUserId = json?.users[0]?.id
    })
    return zenDeskUserId
}
dmahapatro
  • 49,365
  • 7
  • 88
  • 117