jenkinsexceptiongroovyjenkins-pipelinejenkins-shared-libraries

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

  • 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.