function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vishal yadav 86vishal yadav 86 

Bypass duplicate rules

Can we customize standard error message for duplicate rule using apex.
ShirishaShirisha (Salesforce Developers) 
Hi Vishal,

Greetings!

Please find the sample code as below:
 
trigger manageAccountDuplicates on Account (before insert, before update){

    List<String> uniqueValues = new List<String>();
    for(Account record : Trigger.new){
        uniqueValueList.add(record.Unique_Value__c);
    }

    Map<String,Account> uniqueValueMap = new Map<String,Account>();
    for(Account record : [
        SELECT Unique_Value__c FROM Account
        WHERE Unique_Value__c IN :uniqueValueList
    ]){
        uniqueValueMap.put(record.Unique_Value__c, record);        
    }

    for(Account record : Trigger.new){
        if(uniqueValueMap.containsKey(record.Unique_Value__c)){
            if(trigger.isInsert || (trigger.isUpdate && record.id<>uniqueValueMap.get(record.Unique_Value__c).id)){
                record.addError('An account matching this criteria already exists');
            }           
        }
    }
}

You can change the logic according to your business logic.

Reference:https://salesforce.stackexchange.com/questions/131617/prevent-duplicate-account-creation-with-custom-error-message

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
vishal yadav 86vishal yadav 86
Thanks!
Need bit clarity.
Can you ping me here 9030426884
 If u don't mind.
ShirishaShirisha (Salesforce Developers) 
Hi Vishal,

Sorry,I couldn't connect with you at the given number.However,you can ask any question here and I'll try to answer.

Thank you!
vishal yadav 86vishal yadav 86
My requirement is like standard duplicate rule is used to block duplicate leads .
But the error message shown in that rule should be customized using APEX.
Is that possible?
ShirishaShirisha (Salesforce Developers) 
No Vishal,unfortunately it is not possible to customize the error message generated by the Standard duplicate rule.We need to write the logic to check the duplicate records and then customize the error message as given in the code.

Please mark it as best answer if this is helpful.

Thank you!