Skip to main content
global class Account_Add_Opportunities implements Database.Batchable<sObject>{

    

    List<Account> conlist = new List<Account>();

        

    List<Opportunity> updateList = new List<Opportunity>();

    

    

    global Account_Add_Opportunities (){

        // Batch Constructor

    }

    // Start Methode

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

        

        return Database.getQueryLocator([SELECT id,Name,CloseDate,Type from Opportunity WHERE Type = 'New Business' AND StageName = 'New Opportunity' ]); 

        

    }

    // Execute Logic

    global void execute(Database.BatchableContext BC, List<Opportunity>scope){

            conlist  = [SELECT Id,Name,BillingAddress from Account where Name = 'Closed Lost Opportunities'];

 

             System.debug('aaaaaaaaaaaaa'+conlist);    

        for(Opportunity opp :scope )

        {   

            opp.StageName = 'Closed Lost' ;

               for (Account acct :conlist)

               { 

                            opp.AccountID = acct.Id;

            

            }

            

            updateList.add(opp);

            

        }

        

                update updateList ;

 

        }

    

    global void finish(Database.BatchableContext BC){

        // Logic to  be Executed at finish

    }

}
3 个回答
  1. 2020年7月20日 13:00
    Hi Srikanth,

    You can schedule the class by calling the batch class I hope this below help article about that information could help you:

    Below is the sample that was mentioned in the article:

    global with sharing class SchedularForBatchApex implements Schedulable

    {

    global void execute(SchedulableContext sc)

    {

    ID BatchId = Database.executeBatch(new RunBatchApex (), 200);

    }

    Public static void SchedulerMethod()

    {

    string con_exp= ‘0 0 1 * * ?’;System.schedule(‘RunBatchApexTest’, con_exp, new SchedularForBatchApex());

    }

    }

    >> https://www.minddigital.com/how-to-call-batch-apex-by-scheduler-class-within-salesforce/

    Looking forward for your response.

    Do let me know if this helps or incase if there are any questions do let me know.

    Regards,

    Anutej
0/9000