'Getting groovy.lang.MissingPropertyException: No such property: datepart for class: groovy.lang.Binding
I am newbie to jenkins pipeline scripting and i am just trying to concatenate date to string getting below No Such Property exception. Dont know where am doing wrong. Could some one please help me to resolve this
def generateRandomText(){
def temp = ""
try{
Date date = new Date()
String datePart = date.format("ddHHmmssSSS")
temp = "abcde" + datepart
echo "printing ... $temp"
return temp
}
catch(theError){
echo "Error getting while generating random text: {$theError}"
}
return temp
}
Solution 1:[1]
MissingPropertyException means the variable wasn't declared.
There were some errors in your code:
You used
echo, which doesn't exist in Groovy. Use one of theprintfunctions instead. On the code below I usedprintlnThe
datePartvariable was mispelled
Here's your code fixed:
def generateRandomText(){
def temp = ""
try{
Date date = new Date()
String datePart = date.format("ddHHmmssSSS")
temp = "abcde" + datePart
println "printing ... $temp"
return temp
}
catch(theError){
println "Error getting while generating random text: {$theError}"
}
return temp
}
generateRandomText()
Output on groovyConsole:
printing ... abcde21195603124
Result: abcde21195603124
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
