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
Eric Anderson 54Eric Anderson 54 

INVALID_FIELD_FOR_INSERT_UPDATE Test class

Hi there everyone,

I have a test class that I am trying to create that is giving me grief. I have a test class that has added an entry to a Master object (Requests) and a related object (Time Entry). However, in order to get better coverage, I need to have at least three 'Time Entry' objects created so that various conditions are tested. There is also some totalling going on, so i need to have multiple Time entry objects in the same master object. The creation of the master object works ok, as well as the insert of the initial related object. However, when I try to create the 2nd and 3rd objects (Test2 & Test3) into 'Time Entry' I get the error message: 'INVALID_FIELD_FOR_INSERT_UPDATE, can not specify Id in an insert call:[Id]'

Below is what I believe to be the relevant portion of the code.
 
//Instantiate a new instance of the Custom Request Object.
        Request__c ReqObj = new Request__c();
    //Populate the name of the new request object.
        ReqObj.Name = 'Test request';
    //Populate 'What is being requested.
        ReqObj.What_is_being_requested__c = 'Entry to test apex code.';
    //Insert the new request object into Salesforce.
		insert ReqObj;	
        
    //Obtain the object Id for the request object created.     
        string ReqeustId = ReqObj.id;
		
	//Instantiate a new instance of the Time_Entry object.
		Time_Entry__c te = new Time_Entry__c();
    //Populate the required fields of the Time Entry object.
		te.Name ='Test1';												 
        te.Date_Worked__c = System.today();
        te.Hours_Worked__c = '01';
        te.Minutes_Worked__c = '15';
        te.Work_Description__c = 'This is a test entry';
        te.Activity__c = 'Research';
        te.Related_Object__c = ReqeustId.substring(0, 15);
    //Insert the new Time_Entry object into Salesforce.
		insert te;														 
		
    //Populate the required fields of the Time Entry object.
		te.Name ='Test2';												 
        te.Date_Worked__c = System.today();
        te.Hours_Worked__c = '02';
        te.Minutes_Worked__c = '00';
        te.Work_Description__c = 'This is a test entry';
        te.Activity__c = 'Research';
        te.Related_Object__c = ReqeustId.substring(0, 15);
    //Insert the new Time_Entry object into Salesforce.
		insert te;	
        
    //Populate the required fields of the Time Entry object.
		te.Name ='Test3';												 
        te.Date_Worked__c = System.today();
        te.Hours_Worked__c = '01';
        te.Minutes_Worked__c = '15';
        te.Work_Description__c = 'This is a test entry';
        te.Activity__c = 'Development';
        te.Related_Object__c = ReqeustId.substring(0, 15);
    //Insert the new Time_Entry object into Salesforce.
		insert te;														 
        
	//Indicate the starting of the test process. 
        Test.StartTest();




Any assistance would be greatly appreciated.

Thanks! - Eric -
Best Answer chosen by Eric Anderson 54
Ajinkya1225Ajinkya1225
Gotcha!

So, basically you need to create the instance for the other two Time_Entry__c you are trying to insert.
I have modified your code, this should resolve your issue.
//Instantiate a new instance of the Custom Request Object.
        Request__c ReqObj = new Request__c();
    //Populate the name of the new request object.
        ReqObj.Name = 'Test request';
    //Populate 'What is being requested.
        ReqObj.What_is_being_requested__c = 'Entry to test apex code.';
    //Insert the new request object into Salesforce.
		insert ReqObj;	
        
    //Obtain the object Id for the request object created.     
        string ReqeustId = ReqObj.id;
		
	//Instantiate a new instance of the Time_Entry object.
		Time_Entry__c te = new Time_Entry__c();
    //Populate the required fields of the Time Entry object.
	te.Name ='Test1';												 
        te.Date_Worked__c = System.today();
        te.Hours_Worked__c = '01';
        te.Minutes_Worked__c = '15';
        te.Work_Description__c = 'This is a test entry';
        te.Activity__c = 'Research';
        te.Related_Object__c = ReqeustId.substring(0, 15);
    //Insert the new Time_Entry object into Salesforce.
		insert te;														 
		
    //Populate the required fields of the Time Entry object.
       Time_Entry__c te1 = new Time_Entry__c();
	te1.Name ='Test2';												 
        te1.Date_Worked__c = System.today();
        te1.Hours_Worked__c = '02';
        te1.Minutes_Worked__c = '00';
        te1.Work_Description__c = 'This is a test entry';
        te1.Activity__c = 'Research';
        te1.Related_Object__c = ReqeustId.substring(0, 15);
    //Insert the new Time_Entry object into Salesforce.
		insert te1;	
        
    //Populate the required fields of the Time Entry object.
       Time_Entry__c te2 = new Time_Entry__c();
	te2.Name ='Test3';												 
        te2.Date_Worked__c = System.today();
        te2.Hours_Worked__c = '01';
        te2.Minutes_Worked__c = '15';
        te2.Work_Description__c = 'This is a test entry';
        te2.Activity__c = 'Development';
        te2.Related_Object__c = ReqeustId.substring(0, 15);
    //Insert the new Time_Entry object into Salesforce.
		insert te2;														 
        
	//Indicate the starting of the test process. 
        Test.StartTest();

And another thing, don't forget to write asserts in your test class to check your results :)


Hope this helps!

Can you take a moment to  upvote and mark this answer as solved, if it helped you.
Cheers!
Ajinkya Deshmukh

All Answers

Ajinkya1225Ajinkya1225
Hi Eric,

You have instantiated 'te' on line 14, and inserted it on 24. Things till here work fine.
But after that, you are doing another inserts on 'te' at 35 and 46. Those should be 'UPDATE', not 'INSERT' as the record id for 'te' has already been generated in line 24.

Hope this helps!

Can you take a moment to  upvote and mark this answer as solved, if it helped you.
Cheers!
Ajinkya Deshmukh
Eric Anderson 54Eric Anderson 54
Prior to posting this question, I had tried both Update as well as upsert. It appears that I only have one row in the 'Related' Time entry object. I really need to have three rows. After trying it again based on your suggestion, it appears that my sumarization code is not firing in the test class. however, based on testing I have done with my Visualforce page through Salesforce, the summarization code is firing.

In my visual force page, I am able to enter and review the following:

Request Name: Test request   What is being requested: 'Entry to test apex code'

Time entry name     Description                                     Hours Minutes    Activity
Test1                       This is a test entry                            01       15          Research
Test2                       This is a test entry                            02       00          Research
Test3                       This is a test entry                            01       15          Development

Total Research effort: 3 Hours 15 minutes
Total Development effort: 1 Hour 15 minutes

However, I am not seeing the porition of the code fire that processes more than one row. So, with the update, it looks like like the onlything being processed is the last row of 'Test3'.

Any additional insight would be greatly appreciated.

Thanks!

- Eric Anderson-
Ajinkya1225Ajinkya1225
Gotcha!

So, basically you need to create the instance for the other two Time_Entry__c you are trying to insert.
I have modified your code, this should resolve your issue.
//Instantiate a new instance of the Custom Request Object.
        Request__c ReqObj = new Request__c();
    //Populate the name of the new request object.
        ReqObj.Name = 'Test request';
    //Populate 'What is being requested.
        ReqObj.What_is_being_requested__c = 'Entry to test apex code.';
    //Insert the new request object into Salesforce.
		insert ReqObj;	
        
    //Obtain the object Id for the request object created.     
        string ReqeustId = ReqObj.id;
		
	//Instantiate a new instance of the Time_Entry object.
		Time_Entry__c te = new Time_Entry__c();
    //Populate the required fields of the Time Entry object.
	te.Name ='Test1';												 
        te.Date_Worked__c = System.today();
        te.Hours_Worked__c = '01';
        te.Minutes_Worked__c = '15';
        te.Work_Description__c = 'This is a test entry';
        te.Activity__c = 'Research';
        te.Related_Object__c = ReqeustId.substring(0, 15);
    //Insert the new Time_Entry object into Salesforce.
		insert te;														 
		
    //Populate the required fields of the Time Entry object.
       Time_Entry__c te1 = new Time_Entry__c();
	te1.Name ='Test2';												 
        te1.Date_Worked__c = System.today();
        te1.Hours_Worked__c = '02';
        te1.Minutes_Worked__c = '00';
        te1.Work_Description__c = 'This is a test entry';
        te1.Activity__c = 'Research';
        te1.Related_Object__c = ReqeustId.substring(0, 15);
    //Insert the new Time_Entry object into Salesforce.
		insert te1;	
        
    //Populate the required fields of the Time Entry object.
       Time_Entry__c te2 = new Time_Entry__c();
	te2.Name ='Test3';												 
        te2.Date_Worked__c = System.today();
        te2.Hours_Worked__c = '01';
        te2.Minutes_Worked__c = '15';
        te2.Work_Description__c = 'This is a test entry';
        te2.Activity__c = 'Development';
        te2.Related_Object__c = ReqeustId.substring(0, 15);
    //Insert the new Time_Entry object into Salesforce.
		insert te2;														 
        
	//Indicate the starting of the test process. 
        Test.StartTest();

And another thing, don't forget to write asserts in your test class to check your results :)


Hope this helps!

Can you take a moment to  upvote and mark this answer as solved, if it helped you.
Cheers!
Ajinkya Deshmukh
This was selected as the best answer
Eric Anderson 54Eric Anderson 54
Ajinkya,

Thank you for the clarification! that makes sense to me now. I agree with the need for asserts, that is how I knew it was the last row that was getting processed.

Thanks again for your help!

- Eric -