+ Start a Discussion
Nick KeehanNick Keehan 

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: <unknown> duplicates value on record with id: <unknown>: []

hi guys.

Having issue with this Test Class's code coverage. Should be at 100% but sitting at 54%

Class.AccExtension2.Save: line 52, column 1
Class.AccExtension2Test.testVodafoneFab: line 67, column 1

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: <unknown> duplicates value on record with id: <unknown>: []

Test Class - Highlighted fields below seem to be the issue (or the If statement). All three are External ID's and Unique. 

Any Suggestions? 
@isTest 
public class AccExtension2Test 
{
    static testMethod void testCustomerRowID() 
    {
        //Create Account
        Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Siebel Customer Account').getRecordTypeId();
        Account testAccount = new Account();
        testAccount.LastName= 'Last Name';
        testAccount.FirstName= 'First Name';
        testAccount.Phone = '9999999999';
        testAccount.PersonEmail = 'test@testmail.com';
        testAccount.RecordTypeid = devRecordTypeId;             
        testAccount.Siebel_References__c = '1-999997,399999997,1-999997,1-999999997';
        testAccount.Siebel_Customer_Row_ID__c = '1-0000003';    
        testAccount.Clarify_Billing_Account_ID__c = '987873287' ;      
        testAccount.Siebel_Customer_Row_ID__c = '319909009';
        testAccount.Product_Names__c = 'Test Product';
        testAccount.Case_Reference__c = '239879898';
        insert testAccount;
        
        system.assertEquals(testAccount.Phone, '9999999999');
        system.assert(testAccount.id != null);
        
        Test.StartTest(); 
        
        ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
        AccExtension2 testAccPlan = new AccExtension2(sc);
        testAccPlan.save();
        
        Test.StopTest();
    }
     static testMethod void testVodafoneFab() {
        //create case
        Case C = new Case();
        C.Subject = 'test';
        insert C;
        system.assert(C.id != null);
        
        //get case number
        Case CS = [select CaseNumber from Case where id =: C.id];
        
        //Create Account
        Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Siebel Customer Account').getRecordTypeId();
        Account testAccount = new Account();
        testAccount.LastName= 'Last Name';
        testAccount.FirstName= 'First Name';
        testAccount.Phone = '9999999999';
        testAccount.PersonEmail = 'test@testmail.com';
        testAccount.RecordTypeid = devRecordTypeId;             
        testAccount.Siebel_References__c = '1-999989,399999678,1-999678,1-999999678';
        testAccount.Siebel_Customer_Row_ID__c = '1-0009999';    
        testAccount.Vodafone_FAB_Account_Number__c = '8806463614' ;    
        testAccount.Clarify_Billing_Account_ID__c = '879877896' ;      
        testAccount.Product_Names__c = 'Test Product';
        testAccount.Case_Reference__c = CS.CaseNumber;
        testAccount.Worksheet_ID__c = '2BArQ36pR8xz4Ktq6s';
        insert testAccount;
        
        system.assertEquals(testAccount.Phone, '9999999999');
        system.assert(testAccount.id != null);
        
        Test.StartTest(); 
        
        ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
        AccExtension2 testAccPlan = new AccExtension2(sc);
        testAccPlan.save();
        
        Test.StopTest();
        
    }
    
    static testMethod void testNoCustomerRowIDVodafoneTab() {
        //create case
        Case C = new Case();
        C.Subject = 'test';
        insert C;
        system.assert(C.id != null);
        
        //get case number
        Case CS = [select CaseNumber from Case where id =: C.id];
        
        //Create Account
        Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Siebel Customer Account').getRecordTypeId();
        Account testAccount = new Account();
        testAccount.LastName= 'Last Name';
        testAccount.FirstName= 'First Name';
        testAccount.Phone = '9999999999';
        testAccount.PersonEmail = 'test@testmail.com';
        testAccount.RecordTypeid = devRecordTypeId;             
        testAccount.Siebel_References__c = '1-999999,399999999,1-999999,1-999999999';
        testAccount.Siebel_Customer_Row_ID__c = '1-0000000';    
        testAccount.Clarify_Billing_Account_ID__c = '2BArQ36pR8xz4Ktq6s' ;      
        testAccount.Product_Names__c = 'Test Product';
        testAccount.Case_Reference__c = CS.CaseNumber;
        insert testAccount;
        
        system.assertEquals(testAccount.Phone, '9999999999');
        system.assert(testAccount.id != null);
        
        Test.StartTest(); 
        
        ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
        AccExtension2 testAccPlan = new AccExtension2(sc);
        testAccPlan.save();
        
        Test.StopTest();
        
    }
}


Class
public class AccExtension2 {

private final Account objacc;


    public AccExtension2(ApexPages.StandardController controller) {
         this.objacc=(Account)controller.getrecord();
    }
                                          

     Account cust = new Account();   
     Case cse = new Case();      
     Case MyCase = new Case();       
     Account bill = new Account();  
     Account serv = new Account();
     Opportunity opps = new Opportunity();


public PageReference Save() {
if(objacc.Siebel_Customer_Row_ID__c != null){

        // Top Level Account
        cust.LastName= objacc.LastName;
        cust.FirstName= objacc.FirstName;
        cust.Phone= objacc.Phone; 
        cust.PersonMobilePhone= objacc.PersonMobilePhone; 
        cust.PersonEmail= objacc.PersonEmail;          
        cust.Siebel_References__c = objacc.Siebel_References__c ;           
        cust.Siebel_Customer_Row_ID__c = objacc.Siebel_Customer_Row_ID__c;           
        cust.RecordTypeId = '012D00000003KdL';
        upsert cust Siebel_Customer_Row_ID__c;

         // Middle Level Account
         bill.LastName= objacc.LastName;
         bill.FirstName= objacc.FirstName;         
         bill.Parent_Account__c = cust.id; 
         bill.Siebel_Billing_Account_ID__c = objacc.Siebel_References__c.substring(9,18);        
         bill.Siebel_References__c = objacc.Siebel_References__c ;                    
         bill.RecordTypeid = '012D00000003KdQ';      
         upsert bill Siebel_Billing_Account_ID__c;

         // Child Account
         serv.LastName= objacc.LastName;
         serv.FirstName= objacc.FirstName;          
         serv.Parent_Account__c = bill.id;
         serv.Product_Names__c = objacc.Product_Names__c;    
         serv.Siebel_References__c = objacc.Siebel_References__c ;                                        
         serv.RecordTypeid = '012D00000003KdV';
         serv.Vodafone_FAB_Account_Number__c = objacc.Vodafone_FAB_Account_Number__c;
         serv.Worksheet_ID__c=  objacc.Worksheet_ID__c;
         serv.Case_Reference__c = objacc.Case_Reference__c;   
         insert serv;
    
    
         // Sale Loaded to Child Account
         opps.AccountId = serv.id;
         opps.Name = 'New Sale';
         opps.Product_Name_String__c = objacc.Product_Names__c;
         opps.CloseDate = system.today();
         opps.RecordTypeid = '012200000000re7';
         opps.Customer_Type__c = 'Fixed';
         opps.Call_Reason__c = 'Non Retention';  
         opps.Call_Reason_Detail__c = objacc.Call_Reason_Detail__c;      
         opps.StageName = 'Closed Won';    
         opps.PriceBook2id = '01sD0000000d3L2';                 
         opps.Retention_Promotions_Offered_V2__c = objacc.Promotions_Offered__c;                       
         insert opps;
         return new PageReference('/'+ opps.id); 
         
         }

            else{
            If(objacc.Vodafone_FAB_Account_Number__c != null){

         // Legacy Processes (no parentage)
        cust.LastName= objacc.LastName;
        cust.FirstName= objacc.FirstName;
        cust.Phone= objacc.Phone; 
        cust.PersonMobilePhone= objacc.PersonMobilePhone; 
        cust.PersonEmail= objacc.PersonEmail;          
        cust.Siebel_References__c = objacc.Siebel_References__c ;           
        cust.Vodafone_FAB_Account_Number__c = objacc.Vodafone_FAB_Account_Number__c ;           
        cust.RecordTypeid = '012200000000iKt';
        cust.Case_Reference__c = objacc.Case_Reference__c;           
        upsert cust Vodafone_FAB_Account_Number__c;} 
            
            else{
        
         // Legacy Processes (no parentage)
        cust.LastName= objacc.LastName;
        cust.FirstName= objacc.FirstName;
        cust.Phone= objacc.Phone; 
        cust.PersonMobilePhone= objacc.PersonMobilePhone; 
        cust.PersonEmail= objacc.PersonEmail;          
        cust.Siebel_References__c = objacc.Siebel_References__c ;           
        cust.Clarify_Billing_Account_ID__c = objacc.Clarify_Billing_Account_ID__c ;           
        cust.RecordTypeid = '012200000000iKt';
        cust.Case_Reference__c = objacc.Case_Reference__c;           
        upsert cust Clarify_Billing_Account_ID__c ;}

         // Opportunity loaded against Legacy Customer
         opps.AccountId = cust.id;
         opps.Name = 'New Sale';
         opps.Product_Name_String__c = objacc.Product_Names__c;
         opps.CloseDate = system.today();
         opps.RecordTypeID = '012200000000re7';
         opps.Customer_Type__c = 'Fixed';
         opps.Call_Reason__c = 'Non Retention';  
         opps.Call_Reason_Detail__c = objacc.Call_Reason_Detail__c;      
         opps.StageName = 'Closed Won';    
         opps.PriceBook2Id = '01sD0000000d3L2';                       
         opps.Retention_Promotions_Offered_V2__c = objacc.Promotions_Offered__c;                       
         insert opps;
      
      }
 
 
 
          Case MyCase = new Case(); 
          If(objacc.Case_Reference__c != ''){ 
          MyCase = [Select Id, CaseNumber, subject from Case where CaseNumber = :objacc.Case_Reference__c Limit 1];

          MyCase.Referral_Status__c = 'Success';  
          MyCase.Referral_Type__c = 'New';          
          MyCase.VF_Account_Number_C3__c = objacc.Siebel_Customer_Row_ID__c;         
          update MyCase;
        } 


      
      return new PageReference('/'+ opps.id);


    }

}


 
Raj VakatiRaj Vakati
Looks like you have an external Id te system with same values 


try this
 
@isTest 
public class AccExtension2Test 
{
    static testMethod void testCustomerRowID() 
    {
        //Create Account
        Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Siebel Customer Account').getRecordTypeId();
        Account testAccount = new Account();
        testAccount.LastName= 'Last Name';
        testAccount.FirstName= 'First Name';
        testAccount.Phone = '9999999999';
        testAccount.PersonEmail = 'test@testmail.com';
        testAccount.RecordTypeid = devRecordTypeId;             
        testAccount.Siebel_References__c = '1-999997,399999997,1-999997,1-999999997';
        testAccount.Siebel_Customer_Row_ID__c = '1-0000003';    
        testAccount.Clarify_Billing_Account_ID__c = '987873287' ;      
        testAccount.Siebel_Customer_Row_ID__c = '319909009';
        testAccount.Product_Names__c = 'Test Product';
        testAccount.Case_Reference__c = '239879898';
        insert testAccount;
        
        system.assertEquals(testAccount.Phone, '9999999999');
        system.assert(testAccount.id != null);
        
        Test.StartTest(); 
        
        ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
        AccExtension2 testAccPlan = new AccExtension2(sc);
        testAccPlan.save();
        
        Test.StopTest();
    }
     static testMethod void testVodafoneFab() {
        //create case
        Case C = new Case();
        C.Subject = 'test';
        insert C;
        system.assert(C.id != null);
        
        //get case number
        Case CS = [select CaseNumber from Case where id =: C.id];
        
        //Create Account
        Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Siebel Customer Account').getRecordTypeId();
        Account testAccount = new Account();
        testAccount.LastName= 'Last Name';
        testAccount.FirstName= 'First Name';
        testAccount.Phone = '9999999999';
        testAccount.PersonEmail = 'test@testmail.com';
        testAccount.RecordTypeid = devRecordTypeId;             
        testAccount.Siebel_References__c = '1-999989,399999678,1-999678,1-999999678';
    testAccount.Siebel_Customer_Row_ID__c = '1-823646983489623';    
        testAccount.Vodafone_FAB_Account_Number__c = '8806463614' ;    
        testAccount.Clarify_Billing_Account_ID__c = '879877896' ;      
        testAccount.Product_Names__c = 'Test Product';
        testAccount.Case_Reference__c = CS.CaseNumber;
        testAccount.Worksheet_ID__c = '2BArQ36pR8xz4Ktq6s';
        insert testAccount;
        
        system.assertEquals(testAccount.Phone, '9999999999');
        system.assert(testAccount.id != null);
        
        Test.StartTest(); 
        
        ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
        AccExtension2 testAccPlan = new AccExtension2(sc);
        testAccPlan.save();
        
        Test.StopTest();
        
    }
    
    static testMethod void testNoCustomerRowIDVodafoneTab() {
        //create case
        Case C = new Case();
        C.Subject = 'test';
        insert C;
        system.assert(C.id != null);
        
        //get case number
        Case CS = [select CaseNumber from Case where id =: C.id];
        
        //Create Account
        Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Siebel Customer Account').getRecordTypeId();
        Account testAccount = new Account();
        testAccount.LastName= 'Last Name';
        testAccount.FirstName= 'First Name';
        testAccount.Phone = '9999999999';
        testAccount.PersonEmail = 'test@testmail.com';
        testAccount.RecordTypeid = devRecordTypeId;             
        testAccount.Siebel_References__c = '1-999999,399999999,1-999999,1-999999999';
        testAccount.Siebel_Customer_Row_ID__c = '1-7646544654656565';    
        testAccount.Clarify_Billing_Account_ID__c = '2BArQ36pR8xz4Ktq6s' ;      
        testAccount.Product_Names__c = 'Test Product';
        testAccount.Case_Reference__c = CS.CaseNumber;
        insert testAccount;
        
        system.assertEquals(testAccount.Phone, '9999999999');
        system.assert(testAccount.id != null);
        
        Test.StartTest(); 
        
        ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
        AccExtension2 testAccPlan = new AccExtension2(sc);
        testAccPlan.save();
        
        Test.StopTest();
        
    }
}

 
Nick KeehanNick Keehan
Thanks Raj. Still the same error after changing above.
System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: <unknown> duplicates value on record with id: <unknown>: []

Any other suggestions.

Nick