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
Nirmal9114Nirmal9114 

clone account : HELP

i requirement is to clone account with related list.
i am not able to clone related list in this trigger. CAN YOU PLEASE HELP

javascript url :
window.location.href = '/{!Account.Id}/e?clone=1&00N17000000oxkw_ileinner={!Account.Id}';

trigger:

trigger AccountCloning on Account (after insert) {
  
     static Id cloneAccount(Id acctId) 
    { 
       List<Contact> cont = new List<Contact>();
       List<Opportunity> opp = new List<Opportunity>();

       Account acc = [SELECT ID, Name, Organization_Legal_Name__c, FGM_Base__EIN__c, Phone, ShippingAddress, Website, BillingAddress FROM Account WHERE Id = : acctId];
       Account accCopy = acc.clone(false,true);
       accCopy.Name = acc.Name +'-'+system.today();
       insert accCopy;
       
       //cloning Related contact Records
       
       for(Contact c : [SELECT Id, LastName, AccountId FROM Contact WHERE AccountId = : acc.Id]){
         Contact conCopy = c.clone(false,true);
         conCopy.AccountId = accCopy.Id;
         cont.add(conCopy);
        }
       insert cont; 
       return accCopy.Id;
   
   
   }
   }
Vijay NagarathinamVijay Nagarathinam
Hi Tanya,

Refer the below link you get some idea,

https://developer.salesforce.com/forums/?id=906F0000000BTJ8IAO
http://www.infallibletechie.com/2013/06/how-clone-record-with-related-records.html

Thanks,
Vijay
Nirmal9114Nirmal9114
Hi VIjay,

these links are not helping me. i am working on tigger through which i can clone.
Deepak GulianDeepak Gulian
Currently trigger is on after insert event, but if you insert account from account tab, there will be no records in the related list. You need to fix some condition first,like when to initialize the trigger.
Nirmal9114Nirmal9114
DEEPAK - i am not inserting account from account tab i have to clone the existing account having related list.
please help me with the code i was also jus trying to figure out.

thanks
Deepak GulianDeepak Gulian
You can easily do it with apex, but if you really want to make it possible with the trigger only, you should run trigger on Account updat event and create a checkbox field "Cloned" on Account and set it true for clone records.