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
RelaxItsJustCodeRelaxItsJustCode 

Custom Apex cloning of Opportunity and a subset of Opportunity Line Items.... Please help?

This is the class that I use to create first the opportunity header and then specific line items,  PROBLEM,,,,  For some reason it only takes the first of the line items and adds it to the new renewal opportunity.  Please help I will give major kudos.

 

Heres the code:

public with sharing class RenewalOpportunityCreation {

    public static void CreateRenewalOpportunity(Map<Id,Opportunity> oldMap, Map<Id,Opportunity> newMap)
    {

        List<Opportunity> opptsToInsert = new List<Opportunity>();
        Opportunity newOppt = new Opportunity();

        List<Id> OpptyIds = new List<Id>();

        for (Opportunity o : newMap.Values())
        {   
            OpptyIds.add(o.id);
        }

        for(Opportunity oppt : [Select RecordTypeId, Name, Contact__c, AccountId, id, (Select PriceBookEntry.Name, unitprice, 
                                Quantity, Classification__c, Is_Price_book_Entry_Active__c,Is_Product_Active__c  From OpportunityLineItems 
                                Where (Classification__c= 'Software'  OR Classification__c= 'Bundle (SW w/ HW)' OR Classification__c= 'Annual Maintenance' 
                                OR Classification__c = 'Annual Maintenance - 2nd Year & Beyond'))             
                                From Opportunity where Id in :OpptyIds])
        {
            newOppt.Name =  oppt.Name + ' Renewal Opportunity';
            newOppt.AccountId = oppt.AccountId;
            newOppt.Contact__c = oppt.Contact__c;
            newOppt.RecordTypeId =  ServiceClass.getRecordTypesNameMap('Opportunity').get('Renewal Opportunity').Id;                                                        
            newOppt.CloseDate = Date.Today()+30;
            newOppt.StageName = 'Renewal Opportunity';                  
            newOppt.Type = 'Add-On Business';
            newOppt.NextStep = 'Send a invoice for maintenance renewal';
            newOppt.Payment_Terms__c = 'Net 30';
            newOppt.ForecastCategoryName = 'Omitted';
            newOppt.Owner_Location__c = 'FL';
            newOppt.Original_Opportunity__c = oppt.Id;
            newOppt.OwnerId = '00570000001MRuL';
            
            newOppt.Renewal_Software__c = '';
            
            for (OpportunityLineItem lineItem :oppt.OpportunityLineItems) 
            {
                newOppt.Renewal_Software__c = newOppt.Renewal_Software__c + LineItem.PriceBookEntry.Name + '\n';
            }
            
            if(newOppt.Renewal_Software__c <> Null)
            {
                opptsToInsert.add(newOppt);
            }
          
        }
    
        if(opptsToInsert.size() > 0)
        {
            insert(opptsToInsert);
            
            List<OpportunityLineItem> opptLineItemsToInsert = new List<OpportunityLineItem>();
            
            List<Opportunity> OpptIdToInsert = new List<Opportunity>();
            Opportunity newOpptId = new Opportunity();
  
            for(Opportunity opptInsertedIDs : [Select Id, Description from Opportunity where Id in :opptsToInsert])
            {   
                newOpptId.ID = opptInsertedIDs.id;
 
                OpportunityLineItem newOpptLineItem = new OpportunityLineItem();   
                       
           
            
                for(Opportunity opptInserted : [Select RecordTypeId, Name, Contact__c, AccountId, id, Description, (Select PricebookEntry.Name,PricebookEntryid, 
                                                UnitPrice, Quantity, TotalPrice, Classification__c, Is_Price_book_Entry_Active__c,Is_Product_Active__c  
                                                From OpportunityLineItems Where (Classification__c = 'Software'  OR Classification__c= 'Bundle (SW w/ HW)' 
                                                OR Classification__c= 'Annual Maintenance' OR Classification__c = 'Annual Maintenance - 2nd Year & Beyond')) 
                                                From Opportunity where Id in :OpptyIds])
                {       
                    for (OpportunityLineItem lineItem2 :opptInserted.OpportunityLineItems) {
                    newOpptLineItem.Quantity = lineItem2.Quantity;
                    newOpptLineItem.UnitPrice = lineItem2.UnitPrice;
                    newOpptLineItem.PricebookEntryId = lineitem2.PricebookEntry.id;
                }
              
                newOpptLineItem.OpportunityID = newOpptId.ID;
                
                
                opptLineItemsToInsert.add(newOpptLineItem);                 
                
            }
            if(opptLineItemsToInsert.size() > 0)
            {
                insert(opptLineItemsToInsert);
            }
        }}
    }
}

 Thank you,

 Steve Laycock

Best Answer chosen by Admin (Salesforce Developers) 
Sfd developerSfd developer

Hi,

 

Try this,

 

public with sharing class RenewalOpportunityCreation {

    public static void CreateRenewalOpportunity(Map<Id,Opportunity> oldMap, Map<Id,Opportunity> newMap)
    {

        List<Opportunity> opptsToInsert = new List<Opportunity>();
        Opportunity newOppt = new Opportunity();

        List<Id> OpptyIds = new List<Id>();

        for (Opportunity o : newMap.Values())
        {   
            OpptyIds.add(o.id);
        }

        for(Opportunity oppt : [Select RecordTypeId, Name, Contact__c, AccountId, id, (Select PriceBookEntry.Name, unitprice,
                                Quantity, Classification__c, Is_Price_book_Entry_Active__c,Is_Product_Active__c  From OpportunityLineItems
                                Where (Classification__c= 'Software'  OR Classification__c= 'Bundle (SW w/ HW)' OR Classification__c= 'Annual Maintenance'
                                OR Classification__c = 'Annual Maintenance - 2nd Year & Beyond'))             
                                From Opportunity where Id in :OpptyIds])
        {
            newOppt.Name =  oppt.Name + ' Renewal Opportunity';
            newOppt.AccountId = oppt.AccountId;
            newOppt.Contact__c = oppt.Contact__c;
            newOppt.RecordTypeId =  ServiceClass.getRecordTypesNameMap('Opportunity').get('Renewal Opportunity').Id;                                                        
            newOppt.CloseDate = Date.Today()+30;
            newOppt.StageName = 'Renewal Opportunity';                  
            newOppt.Type = 'Add-On Business';
            newOppt.NextStep = 'Send a invoice for maintenance renewal';
            newOppt.Payment_Terms__c = 'Net 30';
            newOppt.ForecastCategoryName = 'Omitted';
            newOppt.Owner_Location__c = 'FL';
            newOppt.Original_Opportunity__c = oppt.Id;
            newOppt.OwnerId = '00570000001MRuL';
            
            newOppt.Renewal_Software__c = '';
            
            for (OpportunityLineItem lineItem :oppt.OpportunityLineItems)
            {
                newOppt.Renewal_Software__c = newOppt.Renewal_Software__c + LineItem.PriceBookEntry.Name + '\n';
            }
            
            if(newOppt.Renewal_Software__c <> Null)
            {
                opptsToInsert.add(newOppt);
            }
          
        }
    
        if(opptsToInsert.size() > 0)
        {
            insert(opptsToInsert);
            
            List<OpportunityLineItem> opptLineItemsToInsert = new List<OpportunityLineItem>();
            
            List<Opportunity> OpptIdToInsert = new List<Opportunity>();

 
            for(Opportunity opptInsertedIDs : [Select Id, Description from Opportunity where Id in :opptsToInsert])
            {   
                newOpptId.ID = opptInsertedIDs.id;
 
         
            
                for(Opportunity opptInserted : [Select RecordTypeId, Name, Contact__c, AccountId, id, Description, (Select PricebookEntry.Name,PricebookEntryid,
                                                UnitPrice, Quantity, TotalPrice, Classification__c, Is_Price_book_Entry_Active__c,Is_Product_Active__c  
                                                From OpportunityLineItems Where (Classification__c = 'Software'  OR Classification__c= 'Bundle (SW w/ HW)'
                                                OR Classification__c= 'Annual Maintenance' OR Classification__c = 'Annual Maintenance - 2nd Year & Beyond'))
                                                From Opportunity where Id in :OpptyIds])
                {   
                    for (OpportunityLineItem lineItem2 :opptInserted.OpportunityLineItems) {
                    OpportunityLineItem newOpptLineItem = new OpportunityLineItem();  
                    newOpptLineItem.Quantity = lineItem2.Quantity;
                    newOpptLineItem.UnitPrice = lineItem2.UnitPrice;
                    newOpptLineItem.PricebookEntryId = lineitem2.PricebookEntry.id;
                    newOpptLineItem.OpportunityID = newOpptId.ID;
                    opptLineItemsToInsert.add(newOpptLineItem);    
                }             
            }
            if(opptLineItemsToInsert.size() > 0)
            {
                insert(opptLineItemsToInsert);
            }
        }}
    }
}

All Answers

Sfd developerSfd developer

Hi,

 

Try this,

 

public with sharing class RenewalOpportunityCreation {

    public static void CreateRenewalOpportunity(Map<Id,Opportunity> oldMap, Map<Id,Opportunity> newMap)
    {

        List<Opportunity> opptsToInsert = new List<Opportunity>();
        Opportunity newOppt = new Opportunity();

        List<Id> OpptyIds = new List<Id>();

        for (Opportunity o : newMap.Values())
        {   
            OpptyIds.add(o.id);
        }

        for(Opportunity oppt : [Select RecordTypeId, Name, Contact__c, AccountId, id, (Select PriceBookEntry.Name, unitprice,
                                Quantity, Classification__c, Is_Price_book_Entry_Active__c,Is_Product_Active__c  From OpportunityLineItems
                                Where (Classification__c= 'Software'  OR Classification__c= 'Bundle (SW w/ HW)' OR Classification__c= 'Annual Maintenance'
                                OR Classification__c = 'Annual Maintenance - 2nd Year & Beyond'))             
                                From Opportunity where Id in :OpptyIds])
        {
            newOppt.Name =  oppt.Name + ' Renewal Opportunity';
            newOppt.AccountId = oppt.AccountId;
            newOppt.Contact__c = oppt.Contact__c;
            newOppt.RecordTypeId =  ServiceClass.getRecordTypesNameMap('Opportunity').get('Renewal Opportunity').Id;                                                        
            newOppt.CloseDate = Date.Today()+30;
            newOppt.StageName = 'Renewal Opportunity';                  
            newOppt.Type = 'Add-On Business';
            newOppt.NextStep = 'Send a invoice for maintenance renewal';
            newOppt.Payment_Terms__c = 'Net 30';
            newOppt.ForecastCategoryName = 'Omitted';
            newOppt.Owner_Location__c = 'FL';
            newOppt.Original_Opportunity__c = oppt.Id;
            newOppt.OwnerId = '00570000001MRuL';
            
            newOppt.Renewal_Software__c = '';
            
            for (OpportunityLineItem lineItem :oppt.OpportunityLineItems)
            {
                newOppt.Renewal_Software__c = newOppt.Renewal_Software__c + LineItem.PriceBookEntry.Name + '\n';
            }
            
            if(newOppt.Renewal_Software__c <> Null)
            {
                opptsToInsert.add(newOppt);
            }
          
        }
    
        if(opptsToInsert.size() > 0)
        {
            insert(opptsToInsert);
            
            List<OpportunityLineItem> opptLineItemsToInsert = new List<OpportunityLineItem>();
            
            List<Opportunity> OpptIdToInsert = new List<Opportunity>();

 
            for(Opportunity opptInsertedIDs : [Select Id, Description from Opportunity where Id in :opptsToInsert])
            {   
                newOpptId.ID = opptInsertedIDs.id;
 
         
            
                for(Opportunity opptInserted : [Select RecordTypeId, Name, Contact__c, AccountId, id, Description, (Select PricebookEntry.Name,PricebookEntryid,
                                                UnitPrice, Quantity, TotalPrice, Classification__c, Is_Price_book_Entry_Active__c,Is_Product_Active__c  
                                                From OpportunityLineItems Where (Classification__c = 'Software'  OR Classification__c= 'Bundle (SW w/ HW)'
                                                OR Classification__c= 'Annual Maintenance' OR Classification__c = 'Annual Maintenance - 2nd Year & Beyond'))
                                                From Opportunity where Id in :OpptyIds])
                {   
                    for (OpportunityLineItem lineItem2 :opptInserted.OpportunityLineItems) {
                    OpportunityLineItem newOpptLineItem = new OpportunityLineItem();  
                    newOpptLineItem.Quantity = lineItem2.Quantity;
                    newOpptLineItem.UnitPrice = lineItem2.UnitPrice;
                    newOpptLineItem.PricebookEntryId = lineitem2.PricebookEntry.id;
                    newOpptLineItem.OpportunityID = newOpptId.ID;
                    opptLineItemsToInsert.add(newOpptLineItem);    
                }             
            }
            if(opptLineItemsToInsert.size() > 0)
            {
                insert(opptLineItemsToInsert);
            }
        }}
    }
}

This was selected as the best answer
JPClark3JPClark3

You shouldn't place queries inside for loops. You can easily run into query limits.

RelaxItsJustCodeRelaxItsJustCode

Thank you very much, I had to make a two small changes after you gave your input and now it works perfectly.  Will give kudos.

 

Thank you,

Steve Laycock

 

public with sharing class RenewalOpportunityCreation {

    public static void CreateRenewalOpportunity(Map<Id,Opportunity> oldMap, Map<Id,Opportunity> newMap)
    {

        List<Opportunity> opptsToInsert = new List<Opportunity>();
        Opportunity newOppt = new Opportunity();

        List<Id> OpptyIds = new List<Id>();

        for (Opportunity o : newMap.Values())
        {   
            OpptyIds.add(o.id);
        }

        for(Opportunity oppt : [Select RecordTypeId, Name, Contact__c, AccountId, id, (Select PriceBookEntry.Name, unitprice,
                                Quantity, Classification__c, Is_Price_book_Entry_Active__c,Is_Product_Active__c  From OpportunityLineItems
                                Where (Classification__c= 'Software'  OR Classification__c= 'Bundle (SW w/ HW)' OR Classification__c= 'Annual Maintenance'
                                OR Classification__c = 'Annual Maintenance - 2nd Year & Beyond'))             
                                From Opportunity where Id in :OpptyIds])
        {
            newOppt.Name =  oppt.Name + ' Renewal Opportunity';
            newOppt.AccountId = oppt.AccountId;
            newOppt.Contact__c = oppt.Contact__c;
            newOppt.RecordTypeId =  ServiceClass.getRecordTypesNameMap('Opportunity').get('Renewal Opportunity').Id;                                                        
            newOppt.CloseDate = Date.Today()+30;
            newOppt.StageName = 'Renewal Opportunity';                  
            newOppt.Type = 'Add-On Business';
            newOppt.NextStep = 'Send a invoice for maintenance renewal';
            newOppt.Payment_Terms__c = 'Net 30';
            newOppt.ForecastCategoryName = 'Omitted';
            newOppt.Owner_Location__c = 'FL';
            newOppt.Original_Opportunity__c = oppt.Id;
            newOppt.OwnerId = '00570000001MRuL';
            
            newOppt.Renewal_Software__c = '';
            
            for (OpportunityLineItem lineItem :oppt.OpportunityLineItems)
            {
                newOppt.Renewal_Software__c = newOppt.Renewal_Software__c + LineItem.PriceBookEntry.Name + '\n';
            }
            
            if(newOppt.Renewal_Software__c <> Null)
            {
                opptsToInsert.add(newOppt);
            }
          
        }
    
        if(opptsToInsert.size() > 0)
        {
            insert(opptsToInsert);
            
            List<OpportunityLineItem> opptLineItemsToInsert = new List<OpportunityLineItem>();
            
            List<Opportunity> OpptIdToInsert = new List<Opportunity>();

 
            for(Opportunity opptInsertedIDs : [Select Id, Description from Opportunity where Id in :opptsToInsert])
            {   

                // Had to change newOpptId.id to newOppt.id
                newOppt.ID = opptInsertedIDs.id;    
            
                for(Opportunity opptInserted : [Select RecordTypeId, Name, Contact__c, AccountId, id, Description, (Select PricebookEntry.Name,PricebookEntryid,
                                                UnitPrice, Quantity, TotalPrice, Classification__c, Is_Price_book_Entry_Active__c,Is_Product_Active__c  
                                                From OpportunityLineItems Where (Classification__c = 'Software'  OR Classification__c= 'Bundle (SW w/ HW)'
                                                OR Classification__c= 'Annual Maintenance' OR Classification__c = 'Annual Maintenance - 2nd Year & Beyond'))
                                                From Opportunity where Id in :OpptyIds])
                {   
                    for (OpportunityLineItem lineItem2 :opptInserted.OpportunityLineItems) {
                    OpportunityLineItem newOpptLineItem = new OpportunityLineItem();  
                    newOpptLineItem.Quantity = lineItem2.Quantity;
                    newOpptLineItem.UnitPrice = lineItem2.UnitPrice;
                    newOpptLineItem.PricebookEntryId = lineitem2.PricebookEntry.id;

                     // Had to change newOpptId.id to newOppt.id
                    newOpptLineItem.OpportunityID = newOppt.id;
                    opptLineItemsToInsert.add(newOpptLineItem);    
                }             
            }
            if(opptLineItemsToInsert.size() > 0)
            {
                insert(opptLineItemsToInsert);
            }
        }}
    }
}