0

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);
            }
          }
        }
  • to set up an Opportunity Product, you need at a minimum : quantity, unitPrice, and pricebookentryId; provided that the cloned Oppo has a pricebook2Id - is this what you are talking about? – cropredy Apr 9 '15 at 21:52
  • Yes. the Opportunity that is triggering the trigger would have Opportunity Products associated. I want to clone those products to the newly created Opportunity. – John N. Apr 9 '15 at 22:40
  • if this class is invoked from an Opportunity trigger, does it get called once per Opportunity? Or is it supposed to handle a list of Opportunities? For bulkification reasons, trigger handlers that do DML should be passed a list of work (the list of opportunitie) so they can build a list of objects to supply to a single DML statement. Somehow, this handler is going to need the OLIs from the source opportunities so it can do 2 DML statements - one to insert cloned Oppos and one to insert cloned OLI. – cropredy Apr 9 '15 at 23:22
  • It's meant to clone an Opportunity record 1 at a time as it is edited. I wouldn't have any bulk editing meant to mass create new Opportunities from existing ones. – John N. Apr 10 '15 at 12:26
  • well -- unless you are using a VF controller to intercept all user updates to the Opportunity so the clone is only for a single Oppo; the use of a trigger has to consider doing the work in bulk as triggers will always be invoked with a list of updated records where list size could be 1 (user entry on page) or 200 (Data Loader) or anything in between – cropredy Apr 11 '15 at 19:37
0

I would say try avoid this in trigger work, But you should look into the Sobject method Deepclone here is a link to a page on here about it

Deep Clone (Parent -> Child -> Grand Child)

  • Thanks. I already have a trigger that does a deepclone. The issue is that there are multiple fields that I do not want to clone on the Opportunity and it would be much more efficient to create a new Opportunity form an existing one, and then clone the products. – John N. Apr 10 '15 at 12:25
  • Create a class version of Opportunity, cast the one you have to copy into it, leaving the unwanted fields behind. Then cast that into the new one. Bit of a long way round but should work at least – TimChadwick Apr 13 '15 at 7:29
  • Thanks Tim. Do you happen to have any examples of what that would look like that you can point me to? – John N. Apr 13 '15 at 16:44
  • Do what you are currently doing but take a clone of the Lines and assign them onto the new object. – TimChadwick Apr 14 '15 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.