My organization has a requirement that prevents two leads from being merged together when they each have a different value in a certain field (named UUID__c
). We created an after delete trigger that we want to fire on merges that informs the user they cannot complete the merge when the two records have different values in the UUID__c
field. However, instead of the trigger showing our custom error message, it states that the operation cannot be completed because a record has been deleted. In actuality, this is not the case as the trigger prevented the merge from completing and both records still exist.
Our problem revolves around posting the error message upon validation. throw new MergeException(MULIPLE_UUID_ERROR)
never ends up firing. Any help or advice would be very welcome. The pertinent code that gets called in a trigger context is below.
public with sharing class LeadMergeValidator {
public static final String MULTIPLE_UUID_ERROR = 'Cannot merge leads when more than one record has a different UUID';
@TestVisible static LeadMergeValidator mock;
public static LeadMergeValidator construct(){
if( Test.isRunningTest() && mock != null ){
return mock;
} else{
return new LeadMergeValidator();
}
}
public void validateMergedLeads( Set<Id> mergedLeadIds ) {
List<Lead> mergedLeads = [Select Id,
UUID__c,
MasterRecord.UUID__c,
ParentAccount__c,
MasterRecord.ParentAccount__c
FROM Lead
WHERE
Id IN :mergedLeadIds
ALL ROWS
];
for( Lead l : mergedLeads ){
if( !String.isEmpty(l.UUID__c)
&& !String.isEmpty(l.MasterRecord.UUID__c)
&& l.UUID__c != l.MasterRecord.UUID__c ){
throw new MergeException(MULTIPLE_UUID_ERROR);
}
}
}
public class MergeException extends Exception{}
}