1

I have this static class called from my lightning component, but am getting the error

"Static method cannot be referenced from a non static context: String String.valueOf(Object)"

on the line where I try and calculate a start date from the string passed. What do I need to do to fix this?

@AuraEnabled
public static void generatePDF(myRec__c rec, string selquarter){
    string selqenddate = selquarter.substringBetween('(', ')');
    date startdate = (selqenddate.valueOf(selqenddate)).addMonths(-3).startofMonth;
    myPDF(rec.id, '', '');
}

2 Answers 2

5

The string class's valueOf() method is a static method.

Static methods need to be called like this: Class.staticMethodName() i.e. String.valueOf()

What you're currently doing is using an instance of a string to try to call a static method, which (as the error indicates) is not allowed.

bad:

selqenddate.valueOf(selqenddate)

good:

String.valueOf(selqenddate)

Of course, you don't need to use String.valueOf() at all here because selquarter is a string, and substringBetween() also returns a string.

Instead, you need to be using a method that takes a String as input, and gives you a Date as output such as Date.parse()

1

As the error suggests, you are trying to use a static method valueOffrom String class on an instance of a String named selqenddate, which is not allowed.

You are most likely are trying to construct a date from a string value, and that you will need to utilize the Date.valueOf()instead. Your code should look like something as below:

Date startdate = 
    (Date.valueOf(selqenddate))
        .addMonths(-3)
            .toStartOfMonth();

Note, there’s no property startOfMonth on Date class.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .