+ Start a Discussion
Rajat MahajanRajat Mahajan 

Urgent Requirement!!!! - Need to create opprotunity products from a class

Hi All,


I have a custom object program consisting of products (child object)


All products have different expiry dates. Even a program has a expiry date.


When a program expiry date comes for a product then that product converts into an opportunity also,

the products associated with it get converted into that opportunity (products).


IF product have different end dates than program dates, then they should be created as a different opportunity, else they would remain in the same opportunity.


Please help out.





Plenty of help here for your needs:



Here's one way you could set this up. 


Create a Batch Apex class which is a class that implements the Batchable interface.  Then, schedule your batch apex class to run nightly.  If you don't have experience with Batch Apex, I'd suggest reading up on it here http://ow.ly/9i82x or here http://ow.ly/9i83b

In the start() method of your batch apex class, query your Programs something like this [ select Id, field1, field2,..., ( select Id, field1, field2,... from Products__r ) from Program__c where ExpirationDate__c > System.today() AND isFlagged__c = false ].  In this case, isFlagged__c is just a flag to denote that the Program has already had Opportunities created for it, so that you're not pulling in the same Programs each night.


In your execute() method, loop through each Program and create an opportunity.  Then, loop through each child record, check to see if the expiration dates match and either create the OpportunityLineItem or create a new Opportunity.  For example:


global void execute( Database.BatchableContext BC, List<Program__c> scope ) {

           List<Opportunity>               oppsToInsert = new List<Opportunity>();

           Map<Id, Opportunity>         oppMap        = new Map<Id, Id>();

           List<OpportunityLineItem> itemsToInsert = new List<OpportunityLineItem>();


           // loop through your query results and create a new Opportunity for each Program.  Create a RelatedProgram field used to make matching easier.

           for( Program__c program : scope ) {

                 Opportunity o = new Opportunity( RelatedProgram__c = program.Id, set your other field values here );

                 oppsToInsert.add( o );

                 program.isFlagged__c = true;


           insert oppsToInsert;


           // create a map of Program Id => Opportunity Id

           for( Opportunity o : oppsToInsert ) {

                 oppMap.put( o.RelatedProgram__c, o.Id );



          // loop through each Product that is a child of one of the Programs in your query.  For each Product with an expiration date that matches the Program expiration date, create a new OpportunityLineItem

          // using the map to match the right Opportunity. 

           for( Product__c product : [ select ExpirationDate__c, Program__r.Id, Program__r.ExpirationDate__c from Product__c where Program__c IN ( select Id from Program__c where Id IN :scope ) ] ) {

                 if( product.ExpirationDate__c == product.Program__r.ExpirationDate__c ) {

                    OpportunityLineItem item = new OpportunityLineItem( OpportunityId = oppMap.get( product.Program__r.Id ), set other fields here );

                    lineItemsToInsert.add( item );

                 } else {

                    // insert your logic to create a new Opportunity for the Products whose expiration dates don't match.



           insert lineItemsToInsert;



Hope that gets you going in the right direction.


~ Clint