3

I've been unable to find good documentation or examples of the isAllowSave() method on FindDuplicateResult class.

Salesforce documentation states that isAllowSave,

"Indicates whether the duplicate rule will allow a record that’s identified as a duplicate to be saved. Set to true if duplicate rule should allow save; otherwise, false."

I have created rules (such as below) that allow saves on match, but isAllowSave still returns false.

enter image description here

When should isAllowSave return true?

I know that I can update the header to bypass warnings and allow save, such as:

dml.DuplicateRuleHeader.allowSave = true; 
Database.SaveResult sr = Database.insert(ConObj, dml); 

However, I would like to be able to inform the user if a save is/isn't allowed before saving the record.

This is a test class I was using to determine the status of isAllowSave:

@isTest 
public class DuplicateRulesTest {

    @TestSetup
    static void makeData(){
        Contact newContact = new Contact(FirstName = 'Sam', LastName = 'Washington', email='[email protected]');
        insert newContact;
    }

    //Contacts with same names should be insertable.
    @isTest 
    static void test1(){
        Contact sameName = new Contact(FirstName = 'Sam', LastName = 'Washington', email='[email protected]');
        Boolean saveAllowed = true;
        
        List<Datacloud.FindDuplicatesResult> results = Datacloud.FindDuplicates.findDuplicates(new List<Contact>{sameName});
        if(results.size() > 0){
            for (Datacloud.DuplicateResult dr : results[0].getDuplicateResults()) {
                if(!dr.isAllowSave()) saveAllowed = false;
                System.debug('Rule Name: '+dr.getDuplicateRule());
                System.debug('isAllowSave: '+dr.isAllowSave());
            }
        }
        
        Test.startTest();
            if(saveAllowed){ //never get here because isAllowSave is false at least once.
                try{
                    insert sameName; 
                }catch(DmlException e){
                    System.debug('The following exception has occurred: ' + e.getMessage());
                }
            }
        Test.stopTest();

        List<Contact> result = [SELECT Id, FirstName, LastName, email FROM Contact WHERE FirstName = 'Sam' AND LastName = 'Washington'];
        System.assertEquals(2, result.size(), 'The new contact was not inserted');
    }
}

With only the "2-Warn On First Last Match" rule active, the debug log is as follows:

Rule Name: X4_Warn_On_First_Last_Match
isAllowSave: false
6
  • 3
    What code did you write specifically to test this?
    – sfdcfox
    Commented Mar 15, 2022 at 18:19
  • @sfdcfox updated the description to show test code and debug log results.
    – Dave
    Commented Mar 16, 2022 at 17:12
  • @sfdcfox, Was this code helpful?
    – Dave
    Commented Mar 21, 2022 at 14:45
  • @Dave, have you managed the problem? Commented Jan 31, 2023 at 7:46
  • @AndriiMuzychuk No luck. I ended up abandoning the test. I'm just chalking it up as one of those testing scenarios that SF doesn't support.
    – Dave
    Commented Feb 1, 2023 at 13:54

1 Answer 1

1

Finally Salesforce considered this behaviour as a bug - https://issues.salesforce.com/issue/a028c00000sliU8/isallowsave-method-of-duplicateresult-class-always-returns-false

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .