+ Start a Discussion
John NeilanJohn Neilan 

Clone sObject Method not Cloning

Hello,

My trigger below is designed to clone an Opportunity related to a custom object record (Account_Manager_Handoff__c).  An Opportunity record is being created whe I specify field values, however, fields from the related Opportunity that it is meant to clone are not coming through unless the manadatory values are included in the trigger code.  My expectation was that Line 29 of my trigger would clone all the values in the related Opportunity, except any that I specified otherwise.  Can anyone help as to why this is ot happeing?  Thanks,

/public class ClassAccountManagerHandoffNotes {


    public void autoCreateRenewal1(Account_Manager_Handoff__c[] newAMH){

        Map<String,String> oppMap = new Map<String,String>();
        Set<Id> parentOpp = new Set<Id>();
    

        FOR (Account_Manager_Handoff__c amh1 : newAMH) {
            parentOpp.add(amh1.Opportunity__c);

        Map<Id,Opportunity> oppMap2 = new Map<Id,Opportunity> ([SELECT Id
                                                                FROM Opportunity
                                                                WHERE Id IN :parentOpp]);
                                                                

        set<Id> amhIds = trigger.newMap.keyset();
        List<Account_Manager_Handoff__c> amhOppData = [SELECT Opportunity__r.Managed_Transfer__c, Opportunity__c,Opportunity__r.Primary_Contact__c,Assigned_Account_Manager__c,Opportunity__r.Acct_Name__c,Opportunity__r.Renewal_Date_Year__c,Assigned_Account_Manager__r.Id,Opportunity__r.Renewal_Date_Next__c,Opportunity__r.Amount,Opportunity__r.Id,Opportunity__r.Business_Goals__c
                                                        FROM Account_Manager_Handoff__c
                                                        WHERE Id IN :amhIds] ;
       FOR(Account_Manager_Handoff__c amh2 : amhOppData){ 
        

        system.debug('********AMH'+amh2.Assigned_Account_Manager__c+' - Acct Manager '+amh2.Opportunity__r.Managed_Transfer__c);
        IF(oppMap2.containskey(amh2.Opportunity__c) && amh2.Assigned_Account_Manager__c != NULL && amh2.Opportunity__r.Managed_Transfer__c == TRUE){
        system.debug('********AMH  Opportunity: '+amh2.Opportunity__c+' Assigned AM: '+amh2.Assigned_Account_Manager__c+' Mgd Transfer: '+amh2.Opportunity__r.Managed_Transfer__c);
  
            Opportunity renewalOpp = amh2.Opportunity__r.clone(false,true);

 //               Opportunity clonedObj = renewalOpp.clone(false,true);
                          
/*                renewalOpp.Name = amh2.Opportunity__r.Acct_Name__c + ' - Renewal (' +amh2.Opportunity__r.Renewal_Date_Year__c + ')';
                renewalOpp.OwnerId = amh2.Assigned_Account_Manager__r.Id;
                renewalOpp.StageName   = 'Active Discussions';
                renewalOpp.CloseDate   = amh2.Opportunity__r.Renewal_Date_Next__c;
                renewalOpp.Amount = amh2.Opportunity__r.Amount;
                renewalOpp.Effective_Date__c  = amh2.Opportunity__r.Renewal_Date_Next__c;
                renewalOpp.Renewal__c = 'Yes';
                renewalOpp.Business_Goals__c = amh2.Opportunity__r.Business_Goals__c;
                renewalOpp.Primary_Contact__c = amh2.Opportunity__r.Primary_Contact__c;
                renewalOpp.Renewed_Opportunity__c = amh2.Opportunity__r.Id;
                renewalOpp.Probability = 5;*/
    
    insert renewalOpp;
    system.debug('@@@@@@@@' + renewalOpp);
    }
    }
    }
    }
  

}


Best Answer chosen by John Neilan
John NeilanJohn Neilan
Thanks.  After doing more research on the clone method, it seems as though it does not work as I expect cloning a record would, which would be to clone whatever object I am specifying.  Instead, it seems that you have to specify in the SOQL which fields are to be cloned.  There is no SELECT * in SOQL, so I am investigating a work around for it, as listing each individual field on the Opportunity record would be unmanageable as new fields are added.  Thanks for you help though!

All Answers

Pavan DavePavan Dave
The issue might be with line:
Opportunity renewalOpp = amh2.Opportunity__r.clone(false,true);

Can you try this:
Opportunity oppTest = [select id, name, other fields from Opportunity where id = 'amh2.Opportunity__r.id'];
Opportunity renewalOpp = oppTest.clone(false,true);
John NeilanJohn Neilan
Thanks, but I'm still getting the same validation errors.  It seems that I need to specify every mandatory field in my Select statement in order for them to be cloned, which somewhat defeats the purpose of cloning as I can just write statements to assign their values to the cloned Opportunity. 
Pavan DavePavan Dave
Correct, if this is a validation error, you have to pull all of the mandatory fields in the query.

No this is not the problem with the clone method. This is because of the relationship. You are trying to get related opportunity in Trigger.
John NeilanJohn Neilan
Thanks.  After doing more research on the clone method, it seems as though it does not work as I expect cloning a record would, which would be to clone whatever object I am specifying.  Instead, it seems that you have to specify in the SOQL which fields are to be cloned.  There is no SELECT * in SOQL, so I am investigating a work around for it, as listing each individual field on the Opportunity record would be unmanageable as new fields are added.  Thanks for you help though!
This was selected as the best answer