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
poojaapoojaa 

How do i add a flag to my below code so that when i execute the code for second time already renewed old opportunities are not renewed again and they should be updated to is renewed true to prevent them from inserting a new renewed opportunity again?

global class RenewOpportunity implements Database.Batchable<sObject>,Database.stateful{
    string count = '';
    private string query;
     //Start Method....   
    global Database.querylocator start(Database.BatchableContext bc){
        Query = 'SELECT id, name,CloseDate,AccountId,ownerId,Is_Renewed__c'+
            ' FROM Opportunity ' +
            'WHERE CloseDate = NEXT_N_DAYS:30 AND Is_Renewed__c = false';
        return Database.getQueryLocator(query);
    }
    
 //Execute Method...   
    global void execute(Database.BatchableContext bc, List<Opportunity> scope){
    try{
        List<Opportunity> opportunities = new List<Opportunity>();
        List<Opportunity> oppList = (List<Opportunity>)scope ;
        List<Opportunity> opptoUpdate = new List<Opportunity>();
        //count=oppList.size();
        for (Opportunity opp : oppList ) {
            

                 Opportunity renewal = new Opportunity();
                renewal.AccountId   = opp.AccountId;
                renewal.Name        = opp.Name + 'Renewal';
                renewal.CloseDate   = opp.CloseDate + 365;
                renewal.StageName   = 'Open';
                renewal.OwnerId     = opp.OwnerId;
                opportunities.add(renewal);
                
                           
        }
        insert opportunities;
        update opportunities;
        }catch(Exception e){
        count=e.getMessage();
        }
        
    }
//Finish Method....    
    public void finish(Database.BatchableContext bc){
         Id job= bc.getJobId();
        System.debug('Count:'+count);
    }
}

 
Best Answer chosen by poojaa
v varaprasadv varaprasad
Hi Poojitha,

Please check once below code : 
global class RenewOpportunity implements Database.Batchable<sObject>,Database.stateful{
    string count = '';
    private string query;
     //Start Method....   
    global Database.querylocator start(Database.BatchableContext bc){
        Query = 'SELECT id, name,CloseDate,AccountId,ownerId,Is_Renewed__c'+
            ' FROM Opportunity ' +
            'WHERE CloseDate = NEXT_N_DAYS:30 AND Is_Renewed__c = false';
        return Database.getQueryLocator(query);
    }
    
 //Execute Method...   
    global void execute(Database.BatchableContext bc, List<Opportunity> scope){
    try{
        List<Opportunity> opportunities = new List<Opportunity>();
        List<Opportunity> oppList = (List<Opportunity>)scope ;
        List<Opportunity> opptoUpdate = new List<Opportunity>();
        //count=oppList.size();
        for (Opportunity opp : oppList ) {
            

                Opportunity renewal = new Opportunity();
                renewal.AccountId   = opp.AccountId;
                renewal.Name        = opp.Name + 'Renewal';
                renewal.CloseDate   = opp.CloseDate + 365;
                renewal.StageName   = 'Open';
                renewal.OwnerId     = opp.OwnerId;
                opportunities.add(renewal);
				
				//Update flag//
				
				Opportunity updOppflag = new Opportunity();
				updOppflag.id = opp.id;
				updOppflag.Is_Renewed__c = True;
				opptoUpdate.add(updOppflag);
				
				
                
                           
        }
        insert opportunities;
        update opportunities;
		update opptoUpdate;
        }catch(Exception e){
        count=e.getMessage();
        }
        
    }
//Finish Method....    
    public void finish(Database.BatchableContext bc){
         Id job= bc.getJobId();
        System.debug('Count:'+count);
    }
}

Hope this helps you.

Thanks
Varaprasad


 

All Answers

v varaprasadv varaprasad
Hi Poojitha,

Please check once below code : 
global class RenewOpportunity implements Database.Batchable<sObject>,Database.stateful{
    string count = '';
    private string query;
     //Start Method....   
    global Database.querylocator start(Database.BatchableContext bc){
        Query = 'SELECT id, name,CloseDate,AccountId,ownerId,Is_Renewed__c'+
            ' FROM Opportunity ' +
            'WHERE CloseDate = NEXT_N_DAYS:30 AND Is_Renewed__c = false';
        return Database.getQueryLocator(query);
    }
    
 //Execute Method...   
    global void execute(Database.BatchableContext bc, List<Opportunity> scope){
    try{
        List<Opportunity> opportunities = new List<Opportunity>();
        List<Opportunity> oppList = (List<Opportunity>)scope ;
        List<Opportunity> opptoUpdate = new List<Opportunity>();
        //count=oppList.size();
        for (Opportunity opp : oppList ) {
            

                Opportunity renewal = new Opportunity();
                renewal.AccountId   = opp.AccountId;
                renewal.Name        = opp.Name + 'Renewal';
                renewal.CloseDate   = opp.CloseDate + 365;
                renewal.StageName   = 'Open';
                renewal.OwnerId     = opp.OwnerId;
                opportunities.add(renewal);
				
				//Update flag//
				
				Opportunity updOppflag = new Opportunity();
				updOppflag.id = opp.id;
				updOppflag.Is_Renewed__c = True;
				opptoUpdate.add(updOppflag);
				
				
                
                           
        }
        insert opportunities;
        update opportunities;
		update opptoUpdate;
        }catch(Exception e){
        count=e.getMessage();
        }
        
    }
//Finish Method....    
    public void finish(Database.BatchableContext bc){
         Id job= bc.getJobId();
        System.debug('Count:'+count);
    }
}

Hope this helps you.

Thanks
Varaprasad


 
This was selected as the best answer
poojaapoojaa
Hi Varaprasad,

Thanks for the help. It was useful for me.