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
mweissmweiss 

INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call

Hi, I keep getting the following error: "INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call", even though I am not specifying any Ids anywhere. It will occasionally run without error, but most of the time it encounters an error on the insert, usually for the same record each time (which shouldn't even come back in the queries).

 

Any insight on this would be greatly appreciated!

 

I have posted my code below.

 

List<Session__c> sessionsToUpdate = new List<Session__c>();
List<Revenue_Rec__c> revRecListToCreate = new List<Revenue_Rec__c>();

positiveSessions = [SELECT Id, Ready__c, ReadyHidden__c, Revenue__c, Department__c, Name, ProjectName__c
	FROM Session__c 
	WHERE Ready__c = True AND ReadyHidden__c = True AND LastModifiedDate > :processDate LIMIT 100];
		
negativeSessions = [SELECT Id, Ready__c, ReadyHidden__c, Revenue__c, Department__c, Name, ProjectName__c
	FROM Session__c 
	WHERE Ready__c = False AND ReadyHidden__c = True AND LastModifiedDate > :processDate LIMIT 100];
			
for ( Service_Session__c currentPosSession : positiveSessions ) {
	Revenue_Rec__c posRevRec = CreateRevRec('Milestone', currentPosSession.Revenue__c, currentPosSession.Department__c, currentPosSession.Id, currentPosSession.ProjectName__c, Date.today());

			//update the checkboxes on the session
			currentPosSession.ReadyHidden__c = False;
			
			sessionsToUpdate.add(currentPosSession);
			revRecListToCreate.add(posRevRecog);
			
		} //END FOR LOOP for ( Service_Session__c currentPositiveServiceSession : positiveServiceSessions ) {
		
		for ( Session__c currentNegSession : negativeSessions ) {
			Decimal revAmount = -currentNegSession.Revenue__c;
			Revenue_Rec__c negRevRec = CreateRevRec('Milestone', revAmount, currentNegSession.Department__c, currentNegSession.Id, currentNegSession.ProjectName__c, Date.today());
			
			//update the checkboxes on the service session
			currentNegSession.ReadyHidden__c = False;
			
			sessionsToUpdate.add(currentNegSession);
			revRecListToCreate.add(negRevRecog);
		
		} //END FOR LOOP for ( Session__c currentNegativeSession : negativeSessions ) {
	
insert revRecListToCreate;
update sessionsToUpdate;

 The create method called:

public Revenue_Rec__c CreateRevRecog (String rrType, Decimal revAmount, String dept, String sessId, String projName, Date postDate) {
		Revenue_Rec__c revRec = new Revenue_Rec__c();
		revRec.RevenueAmount__c = revAmount;
		revRec.Department__c = dept;
		System.debug(sessId);
		revRec.Session__c = sessId;
		revRec.PostDate__c = postDate;
		
		return revRec;				
	}

 

 

_Prasu__Prasu_

this code is a part of you Trigger or Apex controller with VFPage? 

mweissmweiss

This code is part of a scheduled Apex class that runs once daily.