4

How can I call a method structured like this:

public with sharing class Calculator {
    private ApexPages.StandardController l;
    public Calculator(ApexPages.StandardController l) {
    ...
}

From another Apex class (for a batch calculation):

public void calculateLeads() {
    List<Lead> leads = [SELECT Id, Lead_Generation_Date__c FROM Lead ORDER BY Lead_Generation_Date__c];
    for(Lead l: leads) {
        Calculator(l.Id); // what do I pass here?!
    }
}
8

You can just create an instance of a StandardController and pass that.

public void calculateLeads() {
    List<Lead> leads = [SELECT Id, Lead_Generation_Date__c FROM Lead ORDER BY Lead_Generation_Date__c];
    for(Lead l: leads) {
        Calculator controller = new Calculator(new ApexPages.StandardController(l));
    }
}

That said, if you're calling a controller from a batch to do (presumably) some calculation, then really for a code architecture point of view you should create a third class that provides a method to do the calculation (ideally taking a list of Lead objects) and reference that from both the batch and the controller that you've already created.

  • Thanks so much. Agreed, too, this is a one-off is all so shortcuts aren't all that bad. – okay56k Jan 13 '16 at 1:13
  • Although your solution does give me a Method does not exist or incorrect signature error. – okay56k Jan 13 '16 at 1:15
  • My bad, you need to create a new instance of the controller. Will update. – Matt Lacey Jan 13 '16 at 1:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.