Reputation: 6612
I'm pretty new in groovy and I'm trying to make my first jira script. The following code is giving me that the variable "finalMessage" is undeclared..it seems to be related with it being global. What am I doing wrong?
finalMessage = ""
def mainMethod() {
logMessage "hello groovy"
return finalMessage
}
def logMessage(message){
finalMessage += message
}
Upvotes: 0
Views: 486
Reputation: 196
I'm not exactly sure why you would want to do this, however, I think I may have interpreted this correctly. Instead, maybe you could use an array & then join it at the end of the script to get what you want. For example
// This now becomes an array
finalMessage = []
// this function just calls the log message function? Not sure why you have this extra function
def mainMethod() {
logMessage "hello groovy"
}
def logMessage(message){
// instead of trying to change a global value, now that it's an array, you can push to the array
finalMessage.push(message)
}
// here we call the above "mainMethod" in order to execute the logMessage function with the message "hello groovy"
mainMethod()
// this is the final message array now joined with spaces
def joinedFinalMessage = finalMessage.join(' ')
// Print the final message
joinedFinalMessage
Upvotes: 1