• Brandon Hurley 5
  • NEWBIE
  • 15 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies

I'm trying to insert an account record if it's not a duplicate. I'm referencing the findDuplicates method documentation found here: link (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Datacloud_FindDuplicates.htm#apex_Datacloud_FindDuplicates_methods

Account acct = new Account();
acct.Name = 'Acme';
acct.BillingStreet = '123 Fake St';
acct.BillingCity = 'Springfield';
acct.BillingState = 'VT';
acct.BillingCountry = 'US';
        
List<Account> acctList = new List<Account>();
acctList.add(acct); 

if (Datacloud.FindDuplicates.findDuplicates(acctList).size() ​== 0) {
// If the new account doesn't have duplicates, insert it.
    insert(acct);
}
Here is my code:
trigger clientImportTrigger on Client_Import__c (before insert) {
    List<Account> accountsToInsert = new List<Account>();
    Id personAccRecTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('PersonAccount').getRecordTypeId();
    
    for (Client_Import__c A : trigger.new){
//create personAccount
        List<Account> dupeTestPersonAcc = new List<Account>();
        string pAddress = (A.addressLine1__c + ', '+ A.addressLine2__c);
                
        Account newPersonAcc = new Account();
        	newPersonAcc.FirstName = A.firstName__c;
            newPersonAcc.LastName = A.lastName__c;
            newPersonAcc.PersonEmail= A.email__c;
            newPersonAcc.Phone = A.phone__c;
            newPersonAcc.Phone_Type__c = A.phoneType__c;
            newPersonAcc.PersonMailingStreet = pAddress;        	
            newPersonAcc.PersonMailingcity = A.City__c;
            newPersonAcc.PersonMailingState = A.State__c;
            newPersonAcc.PersonMailingPostalCode = A.Zip__c;
            newPersonAcc.PersonMailingCountry = A.Country__c;
            newPersonAcc.Language_Preference__c = A.Language__c;
        	newPersonAcc.RecordTypeId = personAccRecTypeId;
        	dupeTestPersonAcc.add(newPersonAcc); 
        	//accountsToInsert.add(newPersonAcc);
        
        //if the new person account doesn't have duplicates, put it in the list to be inserted later with the rest
        System.debug(Datacloud.FindDuplicates.findDuplicates(dupeTestPersonAcc).size());
        if (Datacloud.FindDuplicates.findDuplicates(dupeTestPersonAcc).size() == 0){
        	accountsToInsert.add(newPersonAcc);
        	
        }
    }
    insert(accountsToInsert);
}

​​​​​​The problem I'm having is the findDuplicates(sObject).size() is always equal to 1, instead of 0 when I run the test class that just inserts a single record. Bascially, I can't figure out how to create the "if statement" so that a record is inserted if no duplicate is found. Any help is appreciated.

I'm trying to insert an account record if it's not a duplicate. I'm referencing the findDuplicates method documentation found here: link (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Datacloud_FindDuplicates.htm#apex_Datacloud_FindDuplicates_methods

Account acct = new Account();
acct.Name = 'Acme';
acct.BillingStreet = '123 Fake St';
acct.BillingCity = 'Springfield';
acct.BillingState = 'VT';
acct.BillingCountry = 'US';
        
List<Account> acctList = new List<Account>();
acctList.add(acct); 

if (Datacloud.FindDuplicates.findDuplicates(acctList).size() ​== 0) {
// If the new account doesn't have duplicates, insert it.
    insert(acct);
}
Here is my code:
trigger clientImportTrigger on Client_Import__c (before insert) {
    List<Account> accountsToInsert = new List<Account>();
    Id personAccRecTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('PersonAccount').getRecordTypeId();
    
    for (Client_Import__c A : trigger.new){
//create personAccount
        List<Account> dupeTestPersonAcc = new List<Account>();
        string pAddress = (A.addressLine1__c + ', '+ A.addressLine2__c);
                
        Account newPersonAcc = new Account();
        	newPersonAcc.FirstName = A.firstName__c;
            newPersonAcc.LastName = A.lastName__c;
            newPersonAcc.PersonEmail= A.email__c;
            newPersonAcc.Phone = A.phone__c;
            newPersonAcc.Phone_Type__c = A.phoneType__c;
            newPersonAcc.PersonMailingStreet = pAddress;        	
            newPersonAcc.PersonMailingcity = A.City__c;
            newPersonAcc.PersonMailingState = A.State__c;
            newPersonAcc.PersonMailingPostalCode = A.Zip__c;
            newPersonAcc.PersonMailingCountry = A.Country__c;
            newPersonAcc.Language_Preference__c = A.Language__c;
        	newPersonAcc.RecordTypeId = personAccRecTypeId;
        	dupeTestPersonAcc.add(newPersonAcc); 
        	//accountsToInsert.add(newPersonAcc);
        
        //if the new person account doesn't have duplicates, put it in the list to be inserted later with the rest
        System.debug(Datacloud.FindDuplicates.findDuplicates(dupeTestPersonAcc).size());
        if (Datacloud.FindDuplicates.findDuplicates(dupeTestPersonAcc).size() == 0){
        	accountsToInsert.add(newPersonAcc);
        	
        }
    }
    insert(accountsToInsert);
}

​​​​​​The problem I'm having is the findDuplicates(sObject).size() is always equal to 1, instead of 0 when I run the test class that just inserts a single record. Bascially, I can't figure out how to create the "if statement" so that a record is inserted if no duplicate is found. Any help is appreciated.