+ Start a Discussion
Glen.ax1034Glen.ax1034 

batch apex / i just want it to "update" all opportunities once a week

I have apex code that populates fields, and I want to run the "before update" on all opportunities once a week to make sure that these fields that are populated are current.

I've created the scheduled batch apex in classes and have set it to schedule and run, and it's running but it isn't running the "before update" on all of my opportunities.

 

here's my scheduler:

global class batchitupson_schedule implements Schedulable {
global void execute(SchedulableContext scMain) {
BatchUpdateForecast clsBatchItUpSon = new BatchUpdateForecast('Select Id, Name FROM Opportunity','Opportunity','Source_Name__c','Gwins');
ID idBatch = Database.executeBatch(clsBatchItUpSon, 100);
}
}

 

and here is my Scheduled class that is batchable, as you can tell, all i want it to do is open all the opportunities and update them, much like if you were to click through them manually and click "Edit" then click "Save" which then runs my "before update" code for the opportunity

 

global class BatchUpdateForecast implements Database.Batchable<sObject>{

   global final String Query;
   global final String Entity;
   global final String Field;
   global final String Value;

   global BatchUpdateForecast (String q, String e, String f, String v){

      Query=q; Entity=e; Field=f;Value=v;
   }

   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope){
     //for(sobject s : scope){
     //s.put(Field,Value); 
     //}
     update scope;
    }

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

 

h8r41dh8r41d

Hi,

 

I'm a little bit fuzzy on exactly what you're trying to accomplish.

 

Do you also have a trigger on your opportunity object? I'm assuming this is what you mean by "before update". And it looks like you're trying to save your object records so the trigger will run, correct?

 

Can you post the trigger?

 

Gabriel Alack
Contact Us - We Can Help!
Salesforce Superheroes
------------------------------
help@salesforcesuperheroes.com
www.salesforcesuperheroes.com
1-888-407-9578 x122

 

Glen.ax1034Glen.ax1034

yes, i have a trigger:

 

trigger forecastMonths on Opportunity (before update, before insert) {

//// a bunch of code that i want to run every week on all the opportunities

}

 

yeah the idea is to get this trigger to run nightly, and i thought that the best way to do this was to batch through the opportunity's using a scheduled batch class and update all of them.