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
an2014an2014 

Create Opportunity for every account

Hello

I want to create an opportunity for every quarter for every account at the beginning of every quarter. can someone suggest a solution. Since soql allows only 1000 records and there could be more than a 1000opptys  being created, can someone suggest solution please
Best Answer chosen by an2014
Amit Chaudhary 8Amit Chaudhary 8
Hi An2014,

Please create one APEX batch job and schedule that according to quarter,
Please try below code :-
global class SearchAndReplace implements Database.Batchable<sObject>{


   global Database.QueryLocator start(Database.BatchableContext BC)
   {
	  String query ='Select id,Name from Account limit 10';	
      return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<Account> scope)
   {
		   List<Opportunity> OpportunityList = new List<Opportunity>();
		   for(Account a : scope)
		   { 
				Opportunity opp = new Opportunity();
				opp.name = a.Name;
				opp.CloseDate=date.Today();
				opp.StageName='Quoted';
				opp.Accountid = a.id;
				
			    OpportunityList.add(a); 
		   } 
		   if(OpportunityList.size() > 0)
			insert OpportunityList; 
   }

   global void finish(Database.BatchableContext BC)
   {
	
   }
}




Thanks,
Amit Chaudhary

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Hi An2014,

Please create one APEX batch job and schedule that according to quarter,
Please try below code :-
global class SearchAndReplace implements Database.Batchable<sObject>{


   global Database.QueryLocator start(Database.BatchableContext BC)
   {
	  String query ='Select id,Name from Account limit 10';	
      return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<Account> scope)
   {
		   List<Opportunity> OpportunityList = new List<Opportunity>();
		   for(Account a : scope)
		   { 
				Opportunity opp = new Opportunity();
				opp.name = a.Name;
				opp.CloseDate=date.Today();
				opp.StageName='Quoted';
				opp.Accountid = a.id;
				
			    OpportunityList.add(a); 
		   } 
		   if(OpportunityList.size() > 0)
			insert OpportunityList; 
   }

   global void finish(Database.BatchableContext BC)
   {
	
   }
}




Thanks,
Amit Chaudhary
This was selected as the best answer
CRMScienceKirkCRMScienceKirk
Check into creating a batch class that can be scheduled to be executed at the start of each quarter.  The batch will allow you higher limits.
an2014an2014
Thank you soo very much for your response.