function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Keenan WojniczKeenan Wojnicz 

FIELD_CUSTOM_VALIDATION_EXCEPTION, Duplicate name found when deploying to production

I have a class in production with 51% code coverage, so in the sandbox I rewrote the test class to improve the coverage to 96%. 
When I try to deploy that updated test class to production i get the following error and I am in great need of some help.

TestZebraScoreController.testClass(), Details: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Duplicate name found: [] Class.TestZebraScoreController.testClass: line 64, column 1

Here is my test class that I am deploying.
public class TestZebraScoreController {
  @isTest
  private static void testClass(){
    Account acc = new Account(Name='test');
    insert acc;
    List<Zebra_Label_and_Tooltip__c > listZLT = new List<Zebra_Label_and_Tooltip__c>();
    Zebra_Label_and_Tooltip__c zlat1 = new Zebra_Label_and_Tooltip__c();
    zlat1.Name = 'Access to Power';
    zlat1.Label_on_score_0__c = 'test';
    zlat1.Label_on_score_4__c = 'test';
    zlat1.Tooltip_on_score_0__c = 'test';
    zlat1.Tooltip_on_score_4__c = 'test';
    listZLT.add(zlat1);
    
    Zebra_Label_and_Tooltip__c zlat2 = new Zebra_Label_and_Tooltip__c();
    zlat2.Name = 'Company Profile';
    zlat2.Label_on_score_0__c = 'test';
    zlat2.Label_on_score_4__c = 'test';
    zlat2.Tooltip_on_score_0__c = 'test';
    zlat2.Tooltip_on_score_4__c = 'test';
    listZLT.add(zlat2);
    
    Zebra_Label_and_Tooltip__c zlat3 = new Zebra_Label_and_Tooltip__c();
    zlat3.Name = 'Funding';
    zlat3.Label_on_score_0__c = 'test';
    zlat3.Label_on_score_4__c = 'test';
    zlat3.Tooltip_on_score_0__c = 'test';
    zlat3.Tooltip_on_score_4__c = 'test';
    listZLT.add(zlat3);
    
    Zebra_Label_and_Tooltip__c zlat4 = new Zebra_Label_and_Tooltip__c();
    zlat4.Name = 'Operations';
    zlat4.Label_on_score_0__c = 'test';
    zlat4.Label_on_score_4__c = 'test';
    zlat4.Tooltip_on_score_0__c = 'test';
    zlat4.Tooltip_on_score_4__c = 'test';
    listZLT.add(zlat4);
    
    Zebra_Label_and_Tooltip__c zlat5 = new Zebra_Label_and_Tooltip__c();
    zlat5.Name = 'ROI';
    zlat5.Label_on_score_0__c = 'test';
    zlat5.Label_on_score_4__c = 'test';
    zlat5.Tooltip_on_score_0__c = 'test';
    zlat5.Tooltip_on_score_4__c = 'test';
    listZLT.add(zlat5);
    
    Zebra_Label_and_Tooltip__c zlat6 = new Zebra_Label_and_Tooltip__c();
    zlat6.Name = 'Service';
    zlat6.Label_on_score_0__c = 'test';
    zlat6.Label_on_score_4__c = 'test';
    zlat6.Tooltip_on_score_0__c = 'test';
    zlat6.Tooltip_on_score_4__c = 'test';
    listZLT.add(zlat6);
    
    Zebra_Label_and_Tooltip__c zlat7 = new Zebra_Label_and_Tooltip__c();
    zlat7.Name = 'Technology';
    zlat7.Label_on_score_0__c = 'test';
    zlat7.Label_on_score_4__c = 'test';
    zlat7.Tooltip_on_score_0__c = 'test';
    zlat7.Tooltip_on_score_4__c = 'test';
    listZLT.add(zlat7);
    
    
    insert listZLT;
    
    Opportunity opp = new Opportunity(Name='testopp', AccountId = acc.Id, StageName = 'IOA Completed', CloseDate = Date.today());
    insert opp;
    ApexPages.currentPage().getParameters().put('opportunityId', opp.Id);
    ZebraScoreController zsc = new ZebraScoreController();
    zsc.Save();
    zsc.Cancel();
  }
}

 
Best Answer chosen by Keenan Wojnicz
ManojjenaManojjena
Hi Keenan,
Try with below code it will help !!
public class TestZebraScoreController {
  @isTest
  private static void testClass(){
  Test.startTest();
    Account acc = new Account(Name='testAcc');
    insert acc;
	List<Zebra_Label_and_Tooltip__c > listZLT = new List<Zebra_Label_and_Tooltip__c>();
	for(Integer count=0;count >=5 ;count++){
	  Zebra_Label_and_Tooltip__c zlat1 = new Zebra_Label_and_Tooltip__c();
			zlat1.Name = 'Access to Power'+count;
			zlat1.Label_on_score_0__c = 'test'+count;
			zlat1.Label_on_score_4__c = 'test1'+count;
			zlat1.Tooltip_on_score_0__c = 'test2';
			zlat1.Tooltip_on_score_4__c = 'test3';
			listZLT.add(zlat1);
	}
    insert listZLT;
    
    Opportunity opp = new Opportunity(Name='testopp', AccountId = acc.Id, StageName = 'IOA Completed', CloseDate = Date.today());
    insert opp;
    ApexPages.currentPage().getParameters().put('opportunityId', opp.Id);
    ZebraScoreController zsc = new ZebraScoreController();
    zsc.Save();
    zsc.Cancel();
	Test.stopTest();
  }
}

Let me know if it helps !!
Thanks 
Manoj

All Answers

ArmouryArmoury
Have you checked any validation rule for the object Zebra_Label_and_Tooltip__c?
ManojjenaManojjena
Hi Keenan,
Try with below code it will help !!
public class TestZebraScoreController {
  @isTest
  private static void testClass(){
  Test.startTest();
    Account acc = new Account(Name='testAcc');
    insert acc;
	List<Zebra_Label_and_Tooltip__c > listZLT = new List<Zebra_Label_and_Tooltip__c>();
	for(Integer count=0;count >=5 ;count++){
	  Zebra_Label_and_Tooltip__c zlat1 = new Zebra_Label_and_Tooltip__c();
			zlat1.Name = 'Access to Power'+count;
			zlat1.Label_on_score_0__c = 'test'+count;
			zlat1.Label_on_score_4__c = 'test1'+count;
			zlat1.Tooltip_on_score_0__c = 'test2';
			zlat1.Tooltip_on_score_4__c = 'test3';
			listZLT.add(zlat1);
	}
    insert listZLT;
    
    Opportunity opp = new Opportunity(Name='testopp', AccountId = acc.Id, StageName = 'IOA Completed', CloseDate = Date.today());
    insert opp;
    ApexPages.currentPage().getParameters().put('opportunityId', opp.Id);
    ZebraScoreController zsc = new ZebraScoreController();
    zsc.Save();
    zsc.Cancel();
	Test.stopTest();
  }
}

Let me know if it helps !!
Thanks 
Manoj
This was selected as the best answer