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
SFDCIronManSFDCIronMan 

Help solve my problem this Program cloning opportunity but not its OpportunityLineItem

public class Opportunity_CloneHandler2 
{
    
    public static void OpportunityCloneData(List<Opportunity> records)
    {
        //Holds list Of Opportunity 
        List<Opportunity> ListOpp = new List<Opportunity>(); 
        //holds list of OpportunityLineItem 
        List<OpportunityLineItem> ListOppItem  = new List<OpportunityLineItem>(); 
        //final list for insertion
        SObject[] sobjList = new List<SObject>(); 
        
        Set<id> parentOppIds = new set<Id>();
        
        Set<Id> childOppIds = new Set<Id>();
        
        Map<Id,List<OpportunityLineItem>> oplitemsMap = new  Map<Id,List<OpportunityLineItem>>();
        
        //hold OpportunityId and ExternalId in Map
        Map<String,String> OpportunityOpportunityLineItemMap = new Map<String,String>();
        
        //For Preventing Recursion
        if(OpportunityRecursive_Trigger_Handler.isFirstTime)
        {
            //Resursion Handler Class
            OpportunityRecursive_Trigger_Handler.isFirstTime = false;
            
            //Cloning Opportunity 
            for(Opportunity parentopp : records)
            { 
                //If Paramters
                if(parentopp.Create_Child_Opportunities_Invoice__c && parentopp.StageName == 'Closed Won' && parentopp.Product_Type__c == 'Subscription' && parentopp.Payment_Frequency__c != Null && parentopp.Interval__c != Null && parentopp.Interval__c != Null)
                {
                    Integer Interval = Integer.valueOf(parentopp.Interval__c);
                    For(Integer Count=1;Count<=Interval;Count++)
                    {
                        parentoppIds.add(parentopp.Id);
                        
                        Opportunity newopp = new Opportunity();
                        
                        newopp.External_Id__c = String.valueOf(Integer.valueOf(math.rint(math.random()*100000)));
                        newopp.Name = parentopp.Name;
                        newopp.AccountId = parentopp.AccountId;
                        newopp.Type = 'Invoice';
                        newopp.StageName = 'Closed Won';
                        newopp.Product_Type__c = parentopp.Product_Type__c;
                        If(parentopp.Billing_Start_Date__c != Null)
                        {
                            If(parentopp.Payment_Frequency__c == 'Monthly')
                            {
                                newopp.CloseDate = parentopp.Billing_Start_Date__c.addMonths(1);
                            }
                            else 
                                If(parentopp.Payment_Frequency__c == 'Quarterly')
                            {
                                newopp.CloseDate = parentopp.Billing_Start_Date__c.addMonths(3);
                            }
                            else 
                                If(parentopp.Payment_Frequency__c == 'Half Yearly')
                            {
                                newopp.CloseDate = parentopp.Billing_Start_Date__c.addMonths(6);
                            }
                        }
                        else
                        {    
                            newopp.CloseDate = parentopp.CloseDate;
                        }    
                        newopp.Payment_Frequency__c = parentopp.Payment_Frequency__c;
                        newopp.Interval__c = parentopp.Interval__c;
                        newopp.Billing_Start_Date__c = newopp.CloseDate;
                        newopp.Parent_Opportunity__c = parentopp.Id;
                        newopp.Cancelation_Date__c = parentopp.Cancelation_Date__c;
                        newopp.Service_End_Date__c = parentopp.Service_End_Date__c;
                        OpportunityOpportunityLineItemMap.put(newopp.Name,newopp.External_Id__c);
                        
                        childOppIds.add(newopp.Id);
                        //Final List
                        ListOpp.add(newopp);
                        If(parentopp.Cancelation_Date__c != Null && newopp.CloseDate > newopp.Cancelation_Date__c || parentopp.Service_End_Date__c != Null && newopp.CloseDate > newopp.Cancelation_Date__c)
                        {
                            for(Opportunity delOpp : [Select Id, Name, AccountId, Type, StageName, Product_Type__C, CloseDate, Billing_Start_Date__c From Opportunity Where Type = 'Invoice' And StageName = 'Closed Won' And Id IN : childOppIds] )
                            {
                                ListOpp.add(delOpp);
                            }
                            delete ListOpp;
                        }
                        
                    }//End of if Checkbox
                    
                }//End of for
                
            }
            
            //Fetching OpportunityLineitems For Opportunity to update related OpportunityLineItem if Present
            for(OpportunityLineitem opl : [SELECT Id, Product2Id, Quantity, UnitPrice,pricebookentryid, OpportunityId FROM OpportunityLineItem Where OpportunityId IN : parentOppIds])
            {
                //Mapping Inserted or Old opportunityIds to Clone the related OpportunityLineItem
                if(!oplitemsMap.containsKey(opl.OpportunityId))
                {
                    oplitemsMap.put(opl.OpportunityId, new List<OpportunityLineitem>{opl});
                }//End of IF Old IDs
                
                else
                {
                    oplitemsMap.get(opl.OpportunityId).add(opl);
                }//end of else
                
            }//End Of for iteration oppoopportunityLineItem
            
            for(Opportunity op : ListOpp)
            {
                //Iterating OpportunityLineitem From Parent Opportunity For Cloning Its OpportunityLineItem
                for(OpportunityLineitem ol : oplitemsMap.get(op.Parent_Opportunity__c))
                {
                    OpportunityLineItem oli = new OpportunityLineitem();
                    oli.OpportunityId = op.Id;
                    oli.Product2Id = ol.Product2Id;
                    oli.Quantity = ol.Quantity;
                    oli.UnitPrice = ol.UnitPrice;
                    oli.pricebookentryid=ol.pricebookentryid;
                    
                    if(OpportunityOpportunityLineItemMap.containsKey(op.Name))
                    {
                        Opportunity lineItemRef = new Opportunity(External_Id__c = OpportunityOpportunityLineItemMap.get(op.Name));
                        oli.Opportunity = lineItemRef;
                    }
                    
                    ListOppItem.add(oli);
                }//End of for
            }
            
            //add Opportunity And OpportunityLineItem List  
            sobjList.addAll(ListOpp);
            sObjList.addAll(ListOppItem);
            
            Database.SaveResult[] results = Database.insert(sobjList);
        }//End of If for Cloning
        
    }//End Of Preventing Recursion
}
AbhinavAbhinav (Salesforce Developers) 
Hi

You can get refernce from here 

https://developer.salesforce.com/forums/?id=906F00000008oEUIAY

Thanks!
SFDCIronManSFDCIronMan
Hi,
thank you for you effort,
yeah ive already created this Program And its working fine but i wanted to insert Opportunity and its OpportunityLineItem in one go please help me
thanks!