You need to sign in to do that
Don't have an account?
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.
Any assistance would be greatly appreciated.
Thanks! - Eric -
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 -
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.
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
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
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-
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.
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
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 -