QUESTION:
I am trying to use the DupeBlocker Customer Matching API option in DupeBlocker 3 to detect a duplicate Account prior to saving, but it is not finding matches. Is there something special I need to do to use this method with Accounts? The org has Person Accounts enabled, but I am having the problem with both Business and Person Account Scenarios.
ANSWER:
The problem is due to the IsPersonAccount field not being populated by Salesforce until the record is saved. When DB_Api.findMatches() is called prior to a save it will never return matches for Account scenarios as the code needs to check if the record is a Person Account (i.e. IsPersonAccount = true/false) to determine which scenarios to check when Person Accounts are enabled in the org.
WORKAROUNDS:
Option 1 (recommended best option):
- Create an account object with any fields needed for matching, and those required to create an account
- Serialize it to a string using JSON.serialize()
- Add the IsPersonAccount field to the string and set it to true (person account) or false (business account)
- Deserialize it back to an account using JSON.deserialize()
Here is sample code for adding the IsPersonAccount flag to a person account without having to save by using JSON serialization/deserialization (adjust accordingly for a business account):
// Load the record type ID for a person account.
RecordType rt = [SELECT Id FROM RecordType WHERE isPersonType = true LIMIT 1];
// Create an Account object with the necessary fields populated.
Account personAccount = new Account(
FirstName = 'Test',
LastName = 'Test',
RecordTypeId = rt.Id );
String accountString = JSON.serialize( personAccount );
accountString = accountString.subString( 0, accountString.length() - 1 ) +
',"IsPersonAccount":true}';
personAccount = (Account)JSON.deserialize( accountString, Account.class );
System.debug( personAccount );
// Call CRMfusionDBR101.DB_Api.findMatches() and process the results as per
// the sample code in the documentation: findMatches() sample code
The debug statement isn't necessary. It's just to demonstrate that the object now has IsPersonAccount set to either true or false depending on if a business or person account is being created.
Option 2:
- Disable DupeBlocker matching via the API by calling CRMfusionDBR101.DB_Api.preventMatching()
- Create a savepoint
- Insert your account record, then requery it to get the IsPersonAccount field populated
- Use DB_Api.findMatches() to search for matches
- If matches are found you can roll back your savepoint and other custom work you require, if they aren't you can commit the savepoint