You need to sign in to do that
Don't have an account?
Error in apex trigger
Hi all,
When I merge account, I want to get the deleted record id and update a field value for the newly merged record.
trigger accountMerge on Account (after delete) { public ID MasterRecordIdfield = null; Account[] acc= Trigger.old; MasterRecordIdfield = acc[0].MasterRecordId; if(MasterRecordIdfield != null && String.valueOf(MasteRecordIdfield).length() >0) { Account[] acct = [select Id, name from account where id =:MasterRecordIdField]; for(Account acct:acc) { acct.Field_to_update__c = acct.name; update acct; } } }
When I execute this, I m getting the following error, I dont know where I am going wrong. Help Please.
Apex trigger then.accountMerge caused an unexpected exception, contact your administrator: then.accountMerge: execution of AfterDelete caused by: System.DmlException: Update failed. First exception on row 0 with id 0019000000K7lxGAAR; first error: SELF_REFERENCE_FROM_TRIGGER, Object 0019000000K7lxGAAR is currently in a merge operation, therefore a trigger can not update it.: []: Trigger.then.accountMerge: line 12, column 1
Sweetz - my apologies - APEX hubris had set in.
Future methods won't accept parameters of Map<ID, List<ID>>, so the rewrite is a bit less elegant as it only allows for current SFDC behavior of up to three merged SObjects
The trigger
The future class/method
All Answers
Hello,
You cannot recursively update or delete the same object from an Apex trigger. This error often occurs when:
* You try to update or delete an object from within its before trigger.
* You try to delete an object from within its after trigger.
I would suggest you to use the following App which if free of cost for this requirement:
https://appexchange.salesforce.com/listingDetail?listingId=a0N30000003IYLlEAO
Hi Vinita_SFDC,
Thanks for your information. If I want to get a deleted record id and updated record id on merging accounts. What are the things I need to think of? Your help please. When I merge an account, The master record ID of the deleted account is same as the Merged account. With this comparison, how can I pull both Id and Update the field of a merged account and trigger an outbound message.
Sweetz
Your variable naming and for loop seems to be the issue here
perhaps the following which is also bulkified:
Thank you so much for your help @crop1645. When I updated the code you have provided I am getting this error
Apex trigger then.accountMerge caused an unexpected exception, contact your administrator: then.accountMerge: execution of AfterDelete caused by: System.DmlException: Update failed. First exception on row 0 with id 0019000000K7lxGAAR; first error: SELF_REFERENCE_FROM_TRIGGER, Object 0019000000K7lxGAAR is currently in a merge operation, therefore a trigger can not update it.: []: Trigger.then.accountMerge: line 11, column 1
Hi
You can't do update operation in a account on which merging is going on in same time. So you write a another trigger for your task.
Simply use this
Hmm -
If asish1989's suggestion doesn't work, then use a @future method in a separate class to do the updates of the winner Accounts with the loser's account names. One thing I also thought of later is that in my bulkified trigger - 'merge' can merge three accounts - two losers and one winner. Thus, the map needs to be Map<ID, List<ID>> winnerIdToLoserIdListMap.
Thanks Ashish and crop for the help. Now i ma ble to update a field only when two records are merged. I want to capture the two record ids that are deleted during merge process when three accounts are merged. As crop said Map list of id's , I m getting error
Sweetz -- we can't help unless you post your current code
This works fine for two records in merge process. But when i merge three records I want loserid__C to be updated with two loser id's. That is not working. Help please
If you merge three record then two record should be deleted.
This for loop will execute two times.
We know that A map is a collection of key-value pairs where each unique key maps to a single value.
mergeWinToLoserIdMap .put(loser.masterRecordId,loser1.Id);
mergeWinToLoserIdMap .put(loser.masterRecordId,loser2.Id);
Here value will be different, becasue these are two recordId of looser records,but key is same.
That's whay It will override the first value. Finaly the map will contain one id.
Ya. Thats what, Then In that case how will i update a field with two loser Id?
Sweetz
You missed my remark on how the map needs to be different - Map of winnerId to List of loser Ids
I am getting error on trigger "Variable does not exist:mergeWinnnerToLoserIdListMap" on this line FutureMergeAuditLog.mergeAccupdate(mergeWinnerToLoserIdListMap);
In class, Error is "Unsupported parameter type Map<Id,List<Id>>" on this line public static void mergeAccupdate(Map<ID,List<ID>> mergeWinnnerToLoserIdListMap)
Sweetz - my apologies - APEX hubris had set in.
Future methods won't accept parameters of Map<ID, List<ID>>, so the rewrite is a bit less elegant as it only allows for current SFDC behavior of up to three merged SObjects
The trigger
The future class/method
Thanks crop1645 :-) Its working well now :-)