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

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); } }
Please check once below code :
Hope this helps you.
Thanks
Varaprasad
All Answers
Please check once below code :
Hope this helps you.
Thanks
Varaprasad
Thanks for the help. It was useful for me.