Created
February 13, 2015 12:38
-
-
Save takahiro-yonei/3e235c425bd00a7522b2 to your computer and use it in GitHub Desktop.
DMLOptions.DuplicateRuleHeader Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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