Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@takahiro-yonei
Created February 13, 2015 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takahiro-yonei/3e235c425bd00a7522b2 to your computer and use it in GitHub Desktop.
Save takahiro-yonei/3e235c425bd00a7522b2 to your computer and use it in GitHub Desktop.
DMLOptions.DuplicateRuleHeader Sample
Database.DMLOptions dml = new Database.DMLOptions();
dml.DuplicateRuleHeader.AllowSave = true;
Account acc = [Select Name From Account Where Name = 'TAO京都' limit 1];
Contact newContact = new Contact(AccountId = acc.Id,
FirstName = 'ABC',
LastName = 'TAO');
Database.SaveResult sr = Database.insert(newContact, dml);
// 実際にはエラーが発生して登録できなかった...
// -> "作成中のレコードは重複しています。代わりに既存のレコードを使用することをお勧めします。"
if(!sr.isSuccess()){
for(Database.Error duplicateError : sr.getErrors()){
Datacloud.DuplicateResult dr = ((Database.DuplicateError)duplicateError).getDuplicateResult();
System.debug(dr.getDuplicateRule());
System.debug(dr.getErrorMessage());
}
}
if(sr.isSuccess()){
System.debug('Success!');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment