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

Account Clone Trigger (with Dynamic SOQL & Clone method)
<This post is in continuation to older posts>
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;
}
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;
}
if this is the issue you can check it easly :
You modiy this code like this way to check the issue
Since you are inserting records we have to avoid read only fields and formula fields. So our final code of block can be like this
Other code will be same. check with above changes either it works fine or not. If this is the solution for your problem mark it as solution.
Trigger:
trigger accountClone on Account (after insert) {
Account[] accSave = new Account[] {};
if(CheckRecursiveTrigger.runOnce())
{
Account theClone= new Account();
Account k = [Select Id from Account where Id IN : trigger.new];
String idVar = k.Id;
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 = :idVar ';
A = Database.query(DMLQuery);
theClone = A.clone(false,true,false,true);
accSave.add(theClone);
}
insert accSave;
}
CheckRecursiveTrigger Class:
public class CheckRecursiveTrigger {
private static boolean run = true;
public static boolean runOnce(){
if (run)
{
run=false;
return true;
}
else
{
return false;
}
}
}
Account k = [Select Id from Account where Id IN : trigger.new];
String idVar = k.Id;
Here, I first fetched the result to Account object and then assigned 'Id' value to String variable.