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
edgarMedgarM 

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.
HarmpieHarmpie

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 :)

Message Edited by Harmpie on 10-07-2009 08:19 AM
edgarMedgarM
Thank Harmpie, i try it.
gv007gv007

Cab you post in what context you are invokeing yours trigger and details code .

edgarMedgarM

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. 

 

 

 

 

gv007gv007
Thie is an salesforce.com provided standard account merge operation their with the help of htis trigger u want send some parameters outside or just you want perform account merge operation.
Rocks_SFDCRocks_SFDC

I am also facing the sam problem...

could you please let me know the solution for it.