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 

trgr clone accnt

if  i save a new account , then cloned account shud also b created.
this trigger is showing no error bt , when i save the new acount only that accnt issaved nt the clone account of it .


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);
}

// 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;

  

}
Best Answer chosen by Chitral Chadda
Shyam BhundiaShyam Bhundia
try this out...


Firstly create the below class to avoid recursion issues from the trigger call 
public Class checkRecursive{
	private static boolean run = true;
    public static boolean runOnce(){
	    if(run){
	    	run=false;
	     	return true;
	    }else{
	        return run;
	    }
    }
}

Update the trigger as follows...note we are checking to see if the trigger has already ran once
trigger partneracClone on Account (after insert) {
   Account [] accToSave = new Account[]{};
   
   if(checkRecursive.runOnce()){

        // 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: trigger.new]);

        // clone
        for (Account acc : trigger.new){   
            Account theClone = new Account();  
            theClone.Name =  acc.Name;
            theClone.Type =  acc.Type;
            theClone.Phone = acc.Phone;
            theClone.Fax =  acc.Fax;
            theClone.Description =  acc.Description;

            accToSave.add(theClone);
        }

        insert accToSave;

    }
}


Hope this helps

All Answers

Deepak Kumar ShyoranDeepak Kumar Shyoran
This is because you wrote the above trigger on Update Event and your trigger not fires on Insert call.

Modify your trigger event as to After Insert and try again.
trigger partneracClone on account (after insert) {

}

Shyam BhundiaShyam Bhundia
try this out...


Firstly create the below class to avoid recursion issues from the trigger call 
public Class checkRecursive{
	private static boolean run = true;
    public static boolean runOnce(){
	    if(run){
	    	run=false;
	     	return true;
	    }else{
	        return run;
	    }
    }
}

Update the trigger as follows...note we are checking to see if the trigger has already ran once
trigger partneracClone on Account (after insert) {
   Account [] accToSave = new Account[]{};
   
   if(checkRecursive.runOnce()){

        // 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: trigger.new]);

        // clone
        for (Account acc : trigger.new){   
            Account theClone = new Account();  
            theClone.Name =  acc.Name;
            theClone.Type =  acc.Type;
            theClone.Phone = acc.Phone;
            theClone.Fax =  acc.Fax;
            theClone.Description =  acc.Description;

            accToSave.add(theClone);
        }

        insert accToSave;

    }
}


Hope this helps
This was selected as the best answer
Abilash Kosigi 8Abilash Kosigi 8
Hi,

I have tried a bit sophisticated trigger with a dyanamic DML Query and Clone method of SObject class. I am almost there except this error which comes when a new account is added. Can anyone of you dig out and let me know.

The error is 

Review all error messages below to correct your data.
Apex trigger accountClone caused an unexpected exception, contact your administrator: accountClone: execution of AfterInsert caused by: System.QueryException: Variable does not exist: Trigger.new:



Trigger:

trigger accountClone on Account (after insert) {
Account[] accSave = new Account[] {};

if(CheckRecursiveTrigger.runOnce())
{
Account theClone= new Account();

Account A=new Account();

String DMLQuery;
        DMLQuery = 'Select ';
Map<String, Schema.SObjectField> fieldsMap = Schema.SObjectType.Account.fields.getMap();
for (Schema.SObjectField field : fieldsMap.values())
     {
      DMLQuery += field.getDescribe().getName() + ',';
      System.debug(field.getDescribe().getName());
     }
        DMLQuery = DMLQuery.substring(0, DMLQuery.length() -1); 
DMLQuery += ' from Account where Id IN : Trigger.new';
           
       
    A = Database.query(DMLQuery);


  theClone =  A.clone(false,true,false,true);

accSave.add(theClone);
}

insert accSave;  


}
Abilash Kosigi 8Abilash Kosigi 8
Please refer the post Account clone Trigger (with Dynamic SOQL and Clone method). Here you get perfectly working code.
Here is the link

https://developer.salesforce.com/forums/#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Apex_Code_Development&criteria=ALLQUESTIONS&id=906F0000000AeXoIAK