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
Yamile Pacheco CuevaYamile Pacheco Cueva 

Ignore duplicate result in visualforce page

I have a visualforce page with a standard controller and a an extension that queries a child and grandchild of the standard controller. The grandchild records have a duplicate match rule. However, my visualforce form only updates existing records. Users do not create new records in it. By the time they get to my visualforce page to edit, they have already said "save and ignore" when they first created the record.

What do I have to do to ignore the duplicate rule for the visualforce page? I would need a sample code as I am new to apex. 
 
NagendraNagendra (Salesforce Developers) 
Hi Pacheco,

You need to use the DmlOptions.DuplicateRuleHeader https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Database_DMLOptions_DuplicateRuleHeader.htm class. The isAllowSave method is used to determine if a rule allows saving or not (e.g. provide a warning or an error).

The following modification should suffice:
public PageReference saveAll() { update childlist; List<Child__c> children = new List<GrandChild__c>(); for (Child__c c : childList) { grandchildlist.addAll(c.GrandChild__r); } Database.DmlOptions opts = new Database.DmlOptions(); opts.DuplicateRuleHeader.allowSave = true; Database.update(grandchildlist, opts); update SiteO; return controller.save(); }
Hope this helps.

Thanks,
Nagendra