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
Chitral ChaddaChitral Chadda 

clone account trigger

trigger partneracClone on account (after insert) {

// accounts ids to query
Account[] accToClone = new Account[]{};
Account [] accToSave = new Account[]{};

Set<Id> AccountIds = new Set<Id>();
For(account acc : trigger.new)
{
     AccountIds.add(acc.id);
accToClone.add(acc);
}

 // query accounts and store by there name to lookup it up quickly
    Map<Id,Account> accountMap = new Map<Id,Account>([
        select  Id  , Name,Description,Phone,Fax  from Account where Id IN: AccountIds]);

    // clone 

    for (Account acc :accToClone)
    {   Account theClone = new Account();


        
        theClone.Name =  accountMap.get(acc.id).Name;
        theClone.Type =  accountMap.get(acc.id).Type;
        theClone.Phone =  accountMap.get(acc.id).Phone;

        theClone.Fax =  accountMap.get(acc.id).Fax;
        theClone.Description =  accountMap.get(acc.id).Description;

        accToSave.add(theClone);
    }
  system.debug('******'+accToSave);
    insert accToSave;
}

error:
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger partneracClone caused an unexpected exception, contact your administrator: partneracClone: execution of AfterInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account.Type: Trigger.partneracClone: line 26, column 1


requirement wen i click save button i want a new account to be created with same name and other fields also same..but i get this error
Amarjeet ChawlaAmarjeet Chawla
You need to include the Type to include it in the Clone.....  theClone.Type =  accountMap.get(acc.id).Type;
May be something like:
  select  Id  , Name,Description,Phone,Fax, Type  from Account where Id IN: AccountIds])
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