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 18SFDC 18 

How to set a flag for the below code so that it doesnot insert renewed opportunities multiple times for the same opportunity

public class RenewOpportunity implements Database.Batchable<sObject>{
    
	private string query;
 //Start Method....   
    public Database.querylocator start(Database.BatchableContext bc){
        Query = 'SELECT id, name, CloseDate ' +
            'FROM Opportunity' +
            'WHERE CloseDate = CloseDate()-30';
        return Database.getQueryLocator(query);
    }
 //Execute Method...   
    public void execute(Database.BatchableContext bc, List<Opportunity> scope){
        List<Opportunity> opportunities = new List<Opportunity>();
        List<Opportunity> oppList = (List<Opportunity>)scope ;

        for (Opportunity opp : oppList ) {
         
            if (opp.CloseDate <= opp.CloseDate - 30) {
                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; 
        }
    }
//Finish Method....    
    public void finish(Database.BatchableContext bc){
         Id job= bc.getJobId();
        System.debug(job);
    }
}

 
Best Answer chosen by SFDC 18
AdikAdik
Create New Field in Oppt_Renewed as Check box default false. Set True for All Oppts Already Renewed, if any.

Here is the Code.

public class RenewOpportunity implements Database.Batchable<sObject>{
    
    private string query;
 //Start Method....   
    public Database.querylocator start(Database.BatchableContext bc){
        Query = 'SELECT id, name, CloseDate ' +
            'FROM Opportunity' +
            'WHERE CloseDate = CloseDate <= LAST_N_DAYS:30 and Oppt_Renewed = false';
        return Database.getQueryLocator(query);
    }
 //Execute Method...   
    public void execute(Database.BatchableContext bc, List<Opportunity> scope){
        List<Opportunity> opportunities = new List<Opportunity>();
        List<Opportunity> oppList = (List<Opportunity>)scope ;

        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;     // To Insert New Oppt
            insert oppList;         // To Updated Oppt_Renewed Flag to True
    }
//Finish Method....    
    public void finish(Database.BatchableContext bc){
         Id job= bc.getJobId();
        System.debug(job);
    }
}

 

All Answers

AdikAdik
Add Custom Checkbox Field like 'Oppt Renewed' and Set that flag in for loop and update the closed Oppt List along with new Oppt Inserts. while Reading List of Clsoed Oppt with SOQL add this Custom Field to Exclude Already Renewed Oppt.
SFDC 18SFDC 18
Hi, Can you help me with the coding part. I'm stuck with the coding part in the for loop after setting the code to flag
AdikAdik
Create New Field in Oppt_Renewed as Check box default false. Set True for All Oppts Already Renewed, if any.

Here is the Code.

public class RenewOpportunity implements Database.Batchable<sObject>{
    
    private string query;
 //Start Method....   
    public Database.querylocator start(Database.BatchableContext bc){
        Query = 'SELECT id, name, CloseDate ' +
            'FROM Opportunity' +
            'WHERE CloseDate = CloseDate <= LAST_N_DAYS:30 and Oppt_Renewed = false';
        return Database.getQueryLocator(query);
    }
 //Execute Method...   
    public void execute(Database.BatchableContext bc, List<Opportunity> scope){
        List<Opportunity> opportunities = new List<Opportunity>();
        List<Opportunity> oppList = (List<Opportunity>)scope ;

        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;     // To Insert New Oppt
            insert oppList;         // To Updated Oppt_Renewed Flag to True
    }
//Finish Method....    
    public void finish(Database.BatchableContext bc){
         Id job= bc.getJobId();
        System.debug(job);
    }
}

 
This was selected as the best answer
SFDC 18SFDC 18
thank you, my query is running fine but the new opportunity was not inserted when the scheduler was run