You need to sign in to do that
Don't have an account?

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
// 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
To:
Try the below code.
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
not working , when i save the new account there is no cloned account
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
my requiremnt is for trigger to run when new account is inserted , it should clone that account