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
Ramana123Ramana123 

I want to run this batch class at every weekend . an anyone help me in this..?

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
    }
}
ANUTEJANUTEJ (Salesforce Developers) 
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
Ramana123Ramana123
Hii Anutej, thanks for the help ,but i am getting the error like this . pic below
ANUTEJANUTEJ (Salesforce Developers) 
Sorry Srikanth, I was unable to find the image.