Salesforce – Cross Object Duplicate Matching

Recently I had a requirement where we need to make sure that there could be only one type of a particular related record for an Account. The user simply creates one of these records using a Salesforce quick action so the solution need to work in this scenario. I thought of the usual ways to do this like having a unique field that references the Account Id (not a nice end user experience) or building a trigger to check (better end user experience but any changes require developer skills).

I wanted to make the solution easy to change or turn off if needed. I looked at the standard Salesforce duplicate match but it didn’t work across objects. I wanted to check if this type of record for unique for a particular account but I didn’t have the Account Id to check both the Account lookup field and the type field in my object – as the combination of these two would need to be unique.

The problem was that there was no value in the Account lookup when the duplicate matching – as it was checking before the record was saved due to the matching happening after the before trigger (Salesforce Order of Execution). I realised that if I created a custom field called accountID__c and populated it with the Account Id in the before trigger – I could then use it in my standard duplicate matching setup.
In the duplicate matching setup I specified that the combination of the type field on my custom object and the accountID__c must be unique.

This before trigger on the custom object simply calls the method to the account id in the accountId__c field:

[sourcecode type=”plaintext”]

public static void setAccountID(List<Custom_Object__c> lNewRecords){

for(Custom_Object__c theRecord : lNewRecords){

theRecord.AccountID__c = theRecord.account__c;



This mean thats that an administrator can change how this works, turn it off or change the error messages without making any changes to the code. I have used the Account object here but this could be adapted for any object in Salesforce and I think it is a nice way to extending standard functionality without becoming dependent on a developer to make changes.

Leave a Reply

Your email address will not be published. Required fields are marked *