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
ankur khattriankur khattri 

clone standard object(account) and its field through trigger...want to clone in account only.....can anyone do this by new trigger pattern thanks

Best Answer chosen by ankur khattri
mayurmayur
Trigger Code:-

trigger CloneAccount on account (after insert) {
list<Account> accToClone = new list<Account>();
list<Account> accToSave = new list<Account>();
Set<Id> AccountIds = new Set<Id>();
For(account acc : trigger.new)
{
    AccountIds.add(acc.id);
    accToClone.add(acc);
}

    Map<Id,Account> accountMap = new Map<Id,Account>([
    select  Id, Name, Description,Type, Phone, Fax  from Account where Id IN: AccountIds]);

    for (Account acc :accToClone)
    {   Account Clone = new Account();
        Clone.Name =  accountMap.get(acc.id).Name;
        Clone.Type =  accountMap.get(acc.id).Type;
        Clone.Phone =  accountMap.get(acc.id).Phone;
        Clone.Fax =  accountMap.get(acc.id).Fax;
        Clone.Description =  accountMap.get(acc.id).Description;
        accToSave.add(Clone);
    }
    
    if(CloneAccountRecursive.isTrigger == True){
    CloneAccountRecursive.isTrigger = False;
    insert accToSave;
    }
}


Class Code: - 

public class CloneAccountRecursive{
    public static boolean isTrigger = true;
}


Use this code to clone your account

All Answers

Head In CloudHead In Cloud
Hi Ankur,
You can do this task with the help of trigger and Process Builder.
To get Inf. refer this link:
http://automationchampion.com/2015/02/16/getting-started-with-process-builder-part-3-clone-a-record/
Thanks
mayurmayur
Trigger Code:-

trigger CloneAccount on account (after insert) {
list<Account> accToClone = new list<Account>();
list<Account> accToSave = new list<Account>();
Set<Id> AccountIds = new Set<Id>();
For(account acc : trigger.new)
{
    AccountIds.add(acc.id);
    accToClone.add(acc);
}

    Map<Id,Account> accountMap = new Map<Id,Account>([
    select  Id, Name, Description,Type, Phone, Fax  from Account where Id IN: AccountIds]);

    for (Account acc :accToClone)
    {   Account Clone = new Account();
        Clone.Name =  accountMap.get(acc.id).Name;
        Clone.Type =  accountMap.get(acc.id).Type;
        Clone.Phone =  accountMap.get(acc.id).Phone;
        Clone.Fax =  accountMap.get(acc.id).Fax;
        Clone.Description =  accountMap.get(acc.id).Description;
        accToSave.add(Clone);
    }
    
    if(CloneAccountRecursive.isTrigger == True){
    CloneAccountRecursive.isTrigger = False;
    insert accToSave;
    }
}


Class Code: - 

public class CloneAccountRecursive{
    public static boolean isTrigger = true;
}


Use this code to clone your account
This was selected as the best answer
ankur khattriankur khattri
thanks mayur
ankur khattriankur khattri
can any1 help.i want to clone  the contacts also that are available in the accounts.thanks