You need to sign in to do that
Don't have an account?
Chitral 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;
}
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;
}
Firstly create the below class to avoid recursion issues from the trigger call
Update the trigger as follows...note we are checking to see if the trigger has already ran once
Hope this helps
All Answers
Modify your trigger event as to After Insert and try again.
Firstly create the below class to avoid recursion issues from the trigger call
Update the trigger as follows...note we are checking to see if the trigger has already ran once
Hope this helps
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;
}
Here is the link
https://developer.salesforce.com/forums/#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Apex_Code_Development&criteria=ALLQUESTIONS&id=906F0000000AeXoIAK