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; first error: MISSING_ARGUMENT, Id not specified in an update call: []

I am getting error as 
System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: [] while executing my test class for trigger Please suggest:

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;
               update 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;
        } 
        insert sm;
            system.test.stopTest();
        
    }
}


 
v varaprasadv varaprasad
Hi Abhishek,

Please check once below modified code : 
@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;
        } 
        update sm;
            system.test.stopTest();
        
    }
}

Hope this helps you.

Thanks
Varaprasad
 
David @ ConfigeroDavid @ Configero
Your unit test method is causing this error at line "update sms;" because your Survey Mcd record you initialize has no record Id and you are calling update.  Your recost must exist first, to be able to update it.

You can fix the unit test by calling insert sms, update the field values, then update sms;

You also might want to evaluate if your trigger should run only on update, or insert as well.

Please mark this answer as correct if it has helped you!
Abhishek Tiwari 25Abhishek Tiwari 25
Updated test Class :

code coverage is 100% but  below error is coming : 
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

@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();
        }
}

 
Abhishek Tiwari 25Abhishek Tiwari 25
I have updated my test class as below  but still i am getting  the same error as below:
System.DmlException: Update failed. First exception on row 0 with id a1h17000003NcuzAAC; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please select only one checkbox: []

@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=false;
            sms.Comment__c=false;
               insert sms;
        List <Survey_Mcd__c> sm= [select id,Comment__c,X5_Scale_Rating__c,Question__c,Survey_Name__c,Yes_No__c 
                                  from Survey_Mcd__c where id=:sms.id LIMIT 1];
         for ( Survey_Mcd__c v:sm)
        {
         system.debug('----ID--'+v.id);
            v.Comment__c=true;
           v.Question__c= '3) Please provide any additional comments on the service you received from GBS Finance?';
            v.Survey_Name__c='McDonald';
                            } 
    update sm;
        Survey_Mcd__c sms1= new Survey_Mcd__c();
                sms1.id=sms.id;
                 sms1.Yes_No__c=true;
            sms1.X5_Scale_Rating__c=true;
            sms1.Comment__c=true;
               update sms1;
            system.test.stopTest();
        }
}
David @ ConfigeroDavid @ Configero
Please refer to your custom object settings and the validation rule that you are violating for Survey_Mcd__c with that error message.

Also please mark the appropriate answer as resolved, as it appears your original question/problem has been solved, but you are experiencing proper validation messages, not code errors any longer.
Abhishek Tiwari 25Abhishek Tiwari 25
Hi David : I dont have any validation rules for Survey_Mcd__c in trigger if i comment the below line  then no error in test class,please suggest
sms.addError('Please select only one checkbox');
David @ ConfigeroDavid @ Configero
I missed the trigger way up above, so that addError() is an apex validation, which has the same effect as a workflow validation rule.

The if conditional criteria is being met, and it is adding a validation error to the record.  It seems to suggest your trigger is written incorrectly.  Please describe what your trigger is supposed to do.
Amit Chaudhary 8Amit Chaudhary 8
Hi,

I hope your this issue is resolved in below post
1) https://developer.salesforce.com/forums/ForumsMain?id=9060G0000005SKyQAM
@isTest
private class TestsurveypageListViewApex {
	static testMethod void myUnitTest1() 
	{
  
         Test.startTest();
			PageReference pageRef = Page.SurveyManagerPage;
			Test.setCurrentPage(pageRef);

			Survey_Mcd__c smc = new Survey_Mcd__c();
			smc.Survey_Name__c = 'McDonald';
			smc.Question__c = 'Test';
			smc.X5_Scale_Rating__c = true;
			insert smc;
		  
			surveypageListViewApex obj = new surveypageListViewApex();
			obj.new1();
		  
      Test.stopTest();
    }
}

Let us know if you need more help on same