QUESTION:
I have a Lead scenario set to Auto-Merge/Do Not Insert/Redirect to Existing, but when I insert a new duplicate Lead it is not being merged/blocked and instead a warning is being created. I have "Report Duplicate" as the "Match on Update Action" so it appears to see the insert as an update....why? And, is there a way to force it to see it as an insert and execute the auto-merge?
ANSWER:
The problem will occur when there are other before or after insert triggers on the object being inserted and one or more are firing BEFORE DupeBlocker's after insert trigger.
For example, an after insert trigger fires before DupeBlocker's after insert trigger and performs an update. When that update happens DupeBlocker is executing in the after update event and sees that as the first operation in the trigger and handles it accordingly. Then, when the update and your after insert trigger complete, DupeBlocker executes again for the after insert event but sees it's already executed once in the apex context and won't perform another matching action.
The order of execution looks like:
Lead Insert
On Insert Trigger Starts
On Insert Trigger Finishes
Your Trigger Starts
DML update is performed
Before Update Triggers
After Update Triggers (DupeBlocker executes)
Your Trigger Finishes
DupeBlocker After Insert Trigger Executes (but no matching is performed)
There's currently no way to enforce order between triggers that fire on the same event, like after insert. In one org your trigger might fire first and cause this behavior while in another org DupeBlocker's might fire first and perform as you expect. The workaround we recommend is to either perform any updates in the before insert/update triggers so that you can update the fields on the trigger.New records directly without a DML statement, use workflow updates, or perform insert/update DML statements asynchronously with an @future method.
We also highly recommend voting up the following Salesforce Idea: Trigger execution order should be deterministic
Another alternative is to temporarily disable DupeBlocker in your code (for the current apex context only), then perform any needed update/insert and then re-enable it afterwards. However, keys won't be built or updated for the records that were inserted/updated during that time. If the field being updated in your trigger is NOT a part of any DupeBlocker scenario rules or filters, then this may be the easiest solution.
To disable DupeBlocker temporarily so that an insert or update in apex code won't prevent matching for the originally inserted/updated record you can use the following code:
// DB3 Version:
// Disable DupeBlocker
CRMfusionDBR101.DB_Api.disableDupeBlocker();
// Perform your inserts/updates
// Enable DupeBlocker
CRMfusionDBR101.DB_Api.enableDupeBlocker();
------------------
// DB2 Version:
// Disable DupeBlocker
CRMfusionDBR101.DB_Globals.triggersDisabled = true;
// Perform your inserts/updates
// Enable DupeBlocker
CRMfusionDBR101.DB_Globals.triggersDisabled = false;
NOTE: This solution refers to multiple after insert triggers for Leads but the same would apply for Account, Contacts or any other object for which has been enabled in DupeBlocker.