reachit
reachit

Reputation: 63

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

} 

Upvotes: 0

Views: 22047

Answers (1)

Maicon Mauricio
Maicon Mauricio

Reputation: 3021

MissingPropertyException means the variable wasn't declared.

There were some errors in your code:

  1. You used echo, which doesn't exist in Groovy. Use one of the print functions instead. On the code below I used println

  2. The datePart variable 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

See Groovy's documentation.

Upvotes: 2

Related Questions