+ Start a Discussion
Chitral ChaddaChitral Chadda 

trigger to clone account

trigger partneracClone on account (after update) {

// 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);
}
if (accToClone.size() > 0)
{
// 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);
    }

    insert accToSave;
}}


i am able to save the trigger but after saving there is no cloned account, only the onw which i create is there
Alex TennantAlex Tennant
Your trigger currently only runs 'after update', when you are performing your tests are your inserting/creating new records, or are you updating/editing existing ones? Currently your trigger will run everytime an Account is edited, it will not run when an Account is created.
Chitral ChaddaChitral Chadda
o  yes .. my requirement is that when i insert new acount it should be cloned
Alex TennantAlex Tennant
In that case change this line:
trigger partneracClone on account (after update) {
To:
trigger partneracClone on account (after insert) {
Anoop yadavAnoop yadav
Hi,

Try the below code.
trigger partneracClone on account (after update) {

// 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);
}
if (accToClone.size() > 0)
{
// 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 :accountMap.values())
    {   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);
    }

    insert accToSave;
}}

logontokartiklogontokartik
I am not sure why you have written such complex code for cloning. Actually there is a clone function which you can use if you want to clone something. Otherwise still, the code can be much simplified. Please see below

trigger AccountClone on Account (after update) {

    List<Account> newAccts = new List<account>();	
    
    for(Account acc : Trigger.new){
        Account cloneAcc = new Account();
        
        // Also you can use clone function instead,
        // cloneAcc = acc.clone(false,false);
        // newAccts.add(cloneAcc);
        
        cloneAcc.Name = acc.Name;
        cloneAcc.Type = acc.Type;
        cloneAcc.Phone = acc.Phone;
        cloneacc.Fax = acc.Fax;
        cloneAcc.Description = acc.Description;
        newAccts.add(cloneAcc);
    }
    
    insert newAccts;
    
}


Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
Hi Chitral,

You can use Clone method of sObject class to clone account.
You can refer http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_sobject.htm#apex_System_SObject_clone for more information.

But you might have to take care of recurrsive clone calls as you are creating new Account record on After Insert trigger which might go in recurssive flow.
you might want to refer to http://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US (http://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US) for avoid recussions caused by triggers.

Thanks,
N.J
Chitral ChaddaChitral Chadda
@logontokartik
not working , when i save the new account  there is no  cloned account
logontokartiklogontokartik
when you say not working how are you confirming that account is not created? are you getting any errors? did you check your debug logs for inserts? If you are doing global search, it might be sometime before they show up. 

Alex TennantAlex Tennant
Did you try changing to 'after insert' like I suggested?
Chitral ChaddaChitral Chadda
@logontokartik
when i save the account , its not showing the new cloned account with the same name in the account list.
only the one which i hv created is in the list
Chitral ChaddaChitral Chadda
@alex: yup not working

Chitral ChaddaChitral Chadda
@@logontokartik
my requiremnt is for trigger to run when new account is inserted , it should clone  that account