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?!
    }
}

1 Answer 1

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.

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

You must log in to answer this question.

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