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
John NeilanJohn Neilan 

Add Products to a New Opportunity

Hello,

I have a trigger that is meant to fire when certain conditions are met on an edited Opportunity.  The trigger is designed to create a new Opportunity using some values from the previous Opportunity.  I also want to pull through the OpportunityLineItems associated with the old Opportunity.  I tried cloning, but the issue was that there are a large number of fields I don't want to clone and I need to set them to blank individually.  Essentially, I am just trying to clone an Opps products, nothing else.  I have the code below to create the Opp, but I'm stuck on how to clone the products.  Can anyone help?  Thanks,
 
public class ClassAccountManagerHandoffNotes {
    
    public void addNotes(List<Account_Manager_Handoff__c> accountManagerHandoffs){
        
        Set<Id> accountsInTrigger = new Set<Id>();
        Map<Id,String> accountMap = new Map<Id,String>();
        Map<Id,Id>accountMap2 = new Map<Id,Id>();
        List<Account> accountsToUpdate = new List<Account>();

        List<Opportunity> createNewOpp = new List<Opportunity>;
        
        FOR(Account_Manager_Handoff__c amh : accountManagerHandoffs){
            IF(amh.AccountLookup__c != NULL){
            
            accountsInTrigger.add(amh.AccountLookup__c);
            String notes = amh.Platform_Experience__c + '\n\n' + amh.Issues_Encountered__c + '\n\n' + amh.Known_Goals__c + '\n\n' + amh.Additional_Notes__c;
            String OppId = amh.Opportunity__c;
            Id owner = amh.Assigned_Account_Manager__c;
            accountMap.put(amh.AccountLookup__c,notes);
            accountMap2.put(amh.AccountLookup__c,owner);


//Clone the Opportunity that is associated with the handoff and all createable fields 
            IF(amh.Handoff_Official__c == TRUE){

              Opportunity opp New Opportunity();

                  opp.accountId = amh.AccountLookup__r.Id;
                  opp.CloseDate = amh.Opportunity__r.Next_Renewal_date__c;
                  opp.Probability = 10;
                  opp.OwnerId = amh.Assigned_Account_Manager__c;
                  opp.Term__c = amh.Opportunity__r.Term__c;
                  opp.Renewal_Base__c = amh.Opportunity__r.Final_Annual_Amount_for_Trigger__c;
                  opp.Effective_Date__c = amh.Opportunity__r.Next_Renewal_Date__c;
                  opp.Renewed_Opportunity__c = amh.Opportunity__r.Id;
                  opp.RP_Contract_Start__c = amh.Opportunity__r.Effective_Date__c;
                  opp.StageName = 'Phase 1: Planning & Training';

              createNewOpp.add(opp);
            }
          }
        }

 
YuchenYuchen
I think you can loop through the fields for Opportunity and OpportunityLineItem to build the query, and when you build the query string, just remove the fields that you do not want. Or you can just include the fields that you want in the query string. The next step will be clone the Opportunity and its OpportunityLineItem separately.

You can refer to the following link:
http://christopheralunlewis.blogspot.com/2012/04/clone-plus-clone-salesforce-objects.html