You need to sign in to do that
Don't have an account?
edgarM
Problem merge two Accounts from Apex.
Hi.
I'm triying merge two accounts from a trigger but this throw a Exception: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old, this is the code.
public class mergeSample { public void convinaCuenta(Account[] cuentas) { for(Account cuenta:cuentas) { //This account already exist Account cuentahija=new Account(Id='0018000000YliZZ');
//Try to merge accounts
merge cuenta cuentahija; } } }
i hope every one can help me, thank.
Am assuming you pass the Trigger.new context variable to method convinaCuenta().... Since it will be passed by reference (not by value!), it will attempt to tamper with the contents of Trigger.new.... which is not allowed.
You best pass the Trigger.new by copying it to another variable first and then passing this copy to convinaCuenta() or add the records from Trigger.new inside this method to a new List and merging that. Just as long as the Trigger.new remains untouched :)
Cab you post in what context you are invokeing yours trigger and details code .
initially the the code is:
class
public class mergeSample { public void convinaCuenta(Account[] cuentas) { for(Account cuenta:cuentas) { . Account cuentahija=new Account(Id='0018000000YliZZ'); merge cuenta cuentahija; } } }
trigger
trigger tr_MergeAccount on Account (after insert) { mergeSample me=new mergeSample(); me.convinaCuenta(Trigger.new); }
the error is same.
I am also facing the sam problem...
could you please let me know the solution for it.