1

I was about to use a batch class and now I'm stuck cause It wont execute. I don't know why.

Here's the code from my controller class:

public ConstructorName(){
   dispForecastSummary();
}

public void dispForecastSummary(){
    SummarizeForecastDetailBatchable s = new SummarizeForecastDetailBatchable();
    Database.executeBatch(s);
}

...

Here's the code from my batch class:

global class SummarizeForecastDetailBatchable implements Database.Batchable<sObject>{

global Database.QueryLocator start(Database.BatchableContext BC)
{
    String query = 'SELECT Id,Name FROM Opportunity';
    return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<Opportunity> scope)
{
     System.debug('executed');
     for(Opportunity a : scope)
     {

     }
}   
global void finish(Database.BatchableContext BC)
{
}
}

I checked the APEX Jobs and debug logs, it's not there either. Why it's not executing in the Controller class? I executed my batch class using anonymous window in dev console, and it's running perfectly. Am I doing something wrong?

Please help me.

3
  • can you share your VF code thats invoking this ? Commented Jul 12, 2014 at 19:04
  • Nothing special on my vf page. I just want to run my method dispForecastSummary() onload so i place it in the constructor.
    – mdechavez
    Commented Jul 12, 2014 at 19:48
  • 1
    Add debug and monitor logs and that should help to figure whether logs are captired Commented Jul 12, 2014 at 19:57

2 Answers 2

5

I don't believe you can execute a batch from a constructor, just like you can't execute DML operations. I would try calling your method in the page action, or possibly via javascript remoting onload.

4
  • 1
    Yes, it would be the same calling a method that called the batch method. I would suggest javascript remoting instead of the actionfunction. Here is a link: salesforce.com/us/developer/docs/pages/Content/… . Then add something like this to call the remote: var previousOnload = window.onload; window.onload = function() { if (previousOnload) { previousOnload(); } alert('Execute Remote Here.'); }
    – JimRae
    Commented Jul 14, 2014 at 1:02
  • 1
    If you wanted to avoid JavaScript, you could use the 'action' attribute on apex:page and move the code into a Action Method on the controller. Note that both this option and the one in the answer do open the page up to CSRF attacks, it is better to have a apex:commandButton the user has to press as Visualforce injects the appropriate CSRF checking here, but not currently on page load. Commented Jul 14, 2014 at 10:48
  • 2
    Andrew, I agree, but was trying to answer the question asked. My concern for @mdechavez is more about the overall execution flow. The page will likely render prior to the batch job getting completed, so the data may not all be present. any attempt to refresh the page would result in the batch job firing again. I might suggest that, depending on the volume to be processed in the batch, that this be switched to a scheduled batch job, possibly with a custom object that monitors the last execution, and the page just include the last batch refresh (from custom object) with the most currect data.
    – JimRae
    Commented Jul 14, 2014 at 13:47
  • Yep totally agree, in fact i just answered a question with that very advice! ;-) Commented Jul 14, 2014 at 16:51
1

Yes, it would be the same calling a method that called the batch method. I would suggest javascript remoting instead of the actionfunction. Here is a link: https://www.salesforce.com/us/developer/docs/pages/Content/pages_js_remoting.htm . Then add something like this to call the remote: var previousOnload = window.onload; window.onload = function() { if (previousOnload) { previousOnload(); } alert('Execute Remote Here.'); }

You must log in to answer this question.

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