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
Abhishek Tiwari 25Abhishek Tiwari 25 

System.DmlException: Update failed. First exception on row 0 with id a1h17000003NcsjAAC; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please select only one checkbox: [] Class.TestselectOneCheckbox.myUnitTest: line 18, column 1

I am getting error  in test class as 
System.DmlException: Update failed. First exception on row 0 with id a1h17000003NcsjAAC; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please select only one checkbox: []
Class.TestselectOneCheckbox.myUnitTest: line 18, column 1
Please help :



Trigger:
trigger selectOneCheckbox on Survey_Mcd__c (before update) {
for(Survey_Mcd__c sms : Trigger.New){
  if(Trigger.isUpdate){
  if((sms.Yes_No__c && sms.X5_Scale_Rating__c && sms.Comment__c)  || (sms.Yes_No__c && sms.X5_Scale_Rating__c)  
  || (sms.Yes_No__c && sms.Comment__c) || (sms.X5_Scale_Rating__c && sms.Comment__c)){
  sms.addError('Please select only one checkbox');
   }
  }
 }
}

Test class

@isTest
public class TestselectOneCheckbox {
    static testMethod  void myUnitTest() {
        system.test.startTest();
         
            Survey_Mcd__c sms= new Survey_Mcd__c();
                 sms.Yes_No__c=true;
            sms.X5_Scale_Rating__c=true;
            sms.Comment__c=true;
               insert sms;
        List <Survey_Mcd__c> sm= [select id,Comment__c,X5_Scale_Rating__c,Yes_No__c 
                                  from Survey_Mcd__c where id=:sms.id];
         for ( Survey_Mcd__c v:sm)
        {
            v.Comment__c=true;
            v.X5_Scale_Rating__c=false;
        } 
    update sm;
        Survey_Mcd__c sms1= new Survey_Mcd__c();
                 sms1.Yes_No__c=true;
            sms1.X5_Scale_Rating__c=true;
            sms1.Comment__c=true;
               update sms1;
            system.test.stopTest();
        }
}

 
Ramesh y 10Ramesh y 10
there is an validation rule on Survey_Mcd__c  object which preventing the DML satatment so setup the test data in test class that should satisfy the validation rule logic. 
Amit Chaudhary 8Amit Chaudhary 8
You Added AddError in your trigger that is why you are getting this error. Try to update your code like below
 
@isTest
public class TestselectOneCheckbox {
    static testMethod  void myUnitTest() 
	{
		try{	
			Survey_Mcd__c sms= new Survey_Mcd__c();
			sms.Yes_No__c=true;
			sms.X5_Scale_Rating__c=true;
			sms.Comment__c=true;
			insert sms;
			
			update sms;
		}
		Catch(Exception ee){
			Boolean expectedExceptionThrown =  ee.getMessage().contains('Please select only one checkbox')) ? true : false;
			System.AssertEquals(expectedExceptionThrown, true);
		}
	}	
}

Let us know if this will help you