+ Start a Discussion
VarunCVarunC 

Merge contacts on account

I need to know how can I detect a merge contact operation in triggers?

 

I've created a trigger that checks for a custom object record related to contact record and prevent the deletion of that custom object recordif it has certain status field value.

 

Now, when I try to merge 2 contacts, this operation somehow triggers a DELETE operation on related records (my custom object). And since I prevent the custom records to be deleted if they hold some status value, so Merge Operation fails ...:( ..

 

The hard part in understanding is that .. why would merge operation Delete records instead of Updating them with other updated Contact record value?

VarunCVarunC
Nobody knows how merge actually works? :( ... please.. I'm kind of stuck here ...
Nick1746323Nick1746323

Hi,

 

When records are merged, the "master" record is updated and the other(s) are deleted. The ones that are deleted have a value in the "MasterRecordId" field. So you can check for that in a delete trigger.

 

I think merging contacts will merge the standard related records like activities but not custom objects - Is your custom object a master-detail on contact? That would explain the cascade delete effect

Message Edited by Nick1746323 on 11-04-2009 12:05 PM
VarunCVarunC
No, my custom object record have Lookup relationship of Contact & Account in it. And the custom object records does not have this Field "MasterRecordId" :( ...
Nick1746323Nick1746323

I meant the MasterRecordId field on Contact.

 

Do you have any other triggers on Contact or your custom object which might be causing the delete ?

VarunCVarunC

Ok now that u mentrioned .. yes actually I am preventing the delete of contact if it has any custom record in some status :( ..

 

so I guess that error is from the Contact delete .. so merge con6tact  is delting the contact record and that record would be the Duplicate reocrd I intende to merge ?

Nick1746323Nick1746323

Hi again,

is your trigger "before delete"? I'm betting that  if you change it to "after delete", everything will be fine,

The reason being, the merge process will update the contact lookup fields after the "before" trigger fires, but before the "after" trigger fires.

VarunCVarunC

Yes it before delete ... :( .. an after delete will not work for my app as it will allow deletion of contact in that case ....

 

Actually problem looks like merge wants to delete the duplicate contact and i am not allowing to delete contact if it is related to a custom record in particular status.

 

 

Nick1746323Nick1746323
it doesn't matter, you can user addError() to control deletion of contacts whether its before or after,