You need to sign in to do that
Don't have an account?

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); } }
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
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);
}
}