Question

I have a scheduled class set up that i would like to work such that it calls a visualforce page that does things to the record. Currently the user has to click the button on the individual record which calls the visualforce page. I would like for the scheduled apex to run and execute this automatically. Here is what i have so far. I put the name of the visualforce page commented out as a place holder for now. Any guidance would be greatly apprecaited!

 global class LastLoginUpdate implements Schedulable{
    global void execute(SchedulableContext SC) {

    List<Case> Cases = [SELECT ID, status, Queue_for_Traccinvoice__c FROM Case WHERE          status = 'to be billed'];
for(case c : Cases){
    if(c.status == 'to be billed'){
        //Call CreateTraccInvoice;
    }
}

update cases;

} }

Was it helpful?

Solution

Instead of 'calling' a page, you should execute the action method in the Controller class that is responsible for that page. If you look at the page you mentioned, at the top you should see something like <apex:page controller="myController" ... where myController is the name of the class that contains logic for that page. Unless the button you were talking about is a standard action (like Save or Delete), there is a custom action defined in that Controller class and it is linked to the button you mentioned via action="..." attribute on the <apex:commandButton> tag.

Now that you know the Controller action that is responsible for handling a button click, you just have to create an instance of that controller in your schedulable class, initialise it with Case ID and call that action programatically.

P.S. to make the solution more future-proof and avoid hitting governor limits, instead of reusing existing action and potentially executing DML statement for each individual case that you loop through, consider modifying replicating the logic of action function in your scheduled class but modifying it so that it can handle a list of cases as an input.

OTHER TIPS

-- Don't know what is purpose of calling VF page. If the VF page in turn calling apex method to do some action/function, you can very well instantiate the Apex class here and call the method.

  • Also you don't need to check the "if(c.status == 'to be billed')" again in the for loop. You already fetching the records based onthe status 'to be billed only.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top