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
David Holland 6David Holland 6 

Data.com Duplicate Management in VisualForce Page

We have a VF page for creating a new custom object, over writing the standard create/edit page.

When a user tries to save a record, we want to leverage the Duplicate Management checker, which I have managed to do, by doing Database.SaveResult and looping through the errors to see if it is an instance of "Database.DuplicateError". 

This displays all the errors on the VF page, however I wish the user to then be able to ignore the duplicates and allow them to save. I thought that by at that point adding "allowSave" as a header equal to true, this would allow the insert, however it doesn't even run the save method a second time.

Code is below but shortened for ease of viewing.
 
public PageReference save(){
		Database.DMLOptions dml = new Database.DMLOptions(); 
		dml.DuplicateRuleHeader.AllowSave = hasDuplicateResult;
		Database.SaveResult saveResult = Database.insert(supplierToUse, dml);
        if (!saveResult.isSuccess()) {        	
            for (Database.Error error : saveResult.getErrors()){
            	ApexPages.addMessage(
	            	new ApexPages.Message(
	                    ApexPages.Severity.ERROR, 
	                    error.getMessage()
	                )
	            );
                if (error instanceof Database.DuplicateError) {
                    duplicateRecords = (List<Supplier__c>) DuplicateFinder.duplicatesFound(error);
                    hasDuplicateResult = !duplicateRecords.isEmpty();
                    ApexPages.addMessage(
		            	new ApexPages.Message(
		                    ApexPages.Severity.WARNING, 
		                    'Please press save again if you wish to save this supplier anyway.'
		                )
		            );
                }
            }
            return null;
        }
    return new PageReference('/' + supplierToUse.Id);
}

Please can someone let me know what I need to do to correct this!
 
sandeep sankhlasandeep sankhla
Hi Please check below link for more details
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_class_Database_DMLOptions_DuplicateRuleHeader.htm

Thanks,
Sandeep