function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC-DMGSFDC-DMG 

Batch Job - Apex Code for Opportunity field update

Hello All,

 

I am trying to create a batch job (that I have scheduled to run daily) that copies the value of one fied to another field.

 

I thought I had it up and running, but when I ran a report this morning, it only updated the field for a small handful of opportunities and it should be running on all of them.This is my first batch job, and I have been trying to discern how to write it correctly based on other posted examples in different places. I was wondering if anyone would be able to help?

 

Here's what needs to happen:

  • Essentially, on a daily basis, I want the batch job to run a trigger on all Opportunities to take the value of the "Stage" field and copy the text into a custom field called "Start of Day Stage" (which sounds simple enough...)


Here's my trigger apex code for the update:

 

global class DailyStageUpdateBatch implements Database.batchable<sObject>, Database.Stateful{

global Database.QueryLocator start(Database.BatchableContext bc){

    return database.getQuerylocator('select id,name,stagename from opportunity');
}
    
   global void execute(Database.batchableContext info, List<Opportunity> scope){ 
       List<Opportunity> oppsToUpdate = new List<Opportunity>(); 
       for(Opportunity o : scope){ 
           o.start_of_day_stage__c = o.stagename;
           oppsToUpdate.add(o);
       }
       update oppsToUpdate; 
   }     
   global void finish(Database.batchableContext info){     
   } 
}

 

And here's the code that implements it (that I then have scheduled to run every morning):

 

// APEX to schedule the batch run to update Opportunities to capture the stage at the start of the day.  
//
global class DailyStageCaptureSchedule implements Schedulable{
   global void execute(SchedulableContext sc) {
        DailyStageUpdatebatch batch = new DailyStageUpdatebatch();
      ID batchprocessid = Database.executeBatch(batch); 
   }
}

 

Any thoughts/ideas please and thank you!!

David Lee(China Developer)David Lee(China Developer)

Hi, Here is my idea.

I guess there are so many opportunities in your org, So you want to update them by a batch job.

The reason it just updated some of records is that someone are updating these opportunities when the batch job is processing. For example, the original value of an opportunity is "status 1", when the batch job is working, someone edited the value to "Status 2", So it will just updated to "status 1" not "status 2".

 

So i suggest you run this batch job at mid-night and nobody will modify these opportunities.

 

 

 

SFDC-DMGSFDC-DMG

Hi,

 

Thank you very much for your response and your suggestion! I'm actually looking to capture it at the start of the day so that we can evaluate how it changed to the end of the day, so that's why I would prefer to run it on all opportunities and and that's why I would like to run it in the morning instead of at midnight.

 

Ideally, this is how the process will go:

1. One scheduled batch job that runs in the morning that captures the current stage on the opportunity and record the value in the "start of day stage" field

2. A second batch job (that would essentially be similar code except that it will record the current stage of the opportunity in the "end of day stage" field

3. There will be an Analytic Snapshot that runs at the end of the day after the second batch job has run, allowing us then look at the opportunities whose stage value changed during the day.

 

I understand that I can use Opportunity Stage history to get at this information, but that does not work for us in this case because we do not want to see all of the changes throughout one day... in one single report we want to see which opportunities had stage changes within a day and we want to view ONLY where it started and where it ended for the day (versus everything in between, which is why the other approach does not work).

 

Because of the times it is running, it is unlikely that someone will be editing the opportunity at the same time, so that should not have been a reason for only a small amount of opportunities being updated the way it's currently set up. I am more concerned about the code not working properly, but that is a very good suggestion to follow to run it at times when no one will be modifying the opps.

 

Does anyone know if and what edits need to be made to my code to have it update all of the opportunities?

 

Thanks!!