[SalesForce] getting “Static method cannot be referenced from a non static context: String String.valueOf(Object)”

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, '', '');
}

Best Answer

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()

Related Topic