0

Had a project where the client wanted to allow the data to bypass contact duplicate rules they had in place. So figured inserting the new contacts with Database.DMLOptions.DuplicateRuleHeader.allowSave would do the trick. It will indeed let you save over duplicate rules in place; however, if the record fails a validation rule it will fail silently

Example: There is a validation rule on Account, that throws an error when 'Fail_Me__c' = true.

Database.DMLOptions dmlOptions = new Database.DMLOptions();
dmlOptions.DuplicateRuleHeader.allowSave = true;
List<SObject> toInsert = new List<SObject>();

Account iPass = new Account();
iPass.Name = 'I Pass validation';
iPass.Fail_Me__c = false;
toInsert.add(iPass);

Account iFail = new Account();
iFail.Name = 'I Fail validation';
iFail.Fail_Me__c = true; // should get an error
toInsert.add(iFail);

Database.insert(toInsert, dmlOptions);

/*shouldn't get here but we do*/
System.debug('*** iPass.Id: ' + iPass.Id);
System.debug('*** iFail.Id: ' + iFail.Id);

Completes successfully, no errors, with a result of: USER_DEBUG|[15]|ERROR|*** iPass.Id: 00136000002X7U5AAK USER_DEBUG|[17]|ERROR|*** iFail.Id: null

Maybe I'm not using it correctly, but something seems off here right?

  • Did you try Database.SaveResult[] srList = Database.insert(toInsert, dmlOptions); ? and then check the isSuccess() method for each list member in srList ? – cropredy Jan 14 '16 at 2:34
0

What if you specify dmlOptions.optAllOrNone = true? By default, that option is false. And you then have to check the Database.SaveResult object to determine which, if any of your records has errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.