You need to sign in to do that
Don't have an account?
John 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,
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); } } } } }
All Answers
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);
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.