+ Start a Discussion
whitfty1whitfty1 

INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

I didn't receive this error while working in the sandbox, it was only when I deployed it to the live site and ran the test class there.  I receive the error message when I try to update the case ownerid.  When I've searched for this error message, they have mentioned  that this can be a problem when updating/inserting within a for loop but their scenarios are always a bit different then mine.  Am I missing something?

trigger Reassign on Case (before insert) {
   
   for(Case c: Trigger.new){
     Partner[] acctPrt = [Select AccountToId FROM Partner WHERE AccountFromId IN (Select accountid FROM contact where Email = :c.SuppliedEmail)]; 
     System.debug('[acctPrt.size= ' + acctPrt.size() + '], [suppliedemail= ' + c.SuppliedEmail + ']');
	    if(acctPrt.size() > 0) {
	     if(acctPrt[0].AccountToId != NULL){
	     	  System.debug('[acctPrt[0].AccountToId= ' + acctPrt[0].AccountToId + ']');
	     Account acct = [Select isPartner FROM Account where ID=  :acctPrt[0].AccountToId];
	     System.debug('[acct.IsPartner= ' + acct.IsPartner + ']');
		     if (acct.IsPartner == true) { 
		     	 User[] portalUser = [Select ID FROM User where AccountID=  :acctPrt[0].AccountToId];
			     	  if(portalUser.size() > 0) {  
			       c.OwnerId = portalUser[0].Id;   
			       //c.OwnerId = acctPrt[0].AccountToId;
			        c.Status='Waiting Activation';
			        c.StatusReason__c='Assigned';
			          System.debug('[c.OwnerID= ' + c.OwnerID + '], [c.Status= ' + c.Status + ']');			       
			     	  }
		        } 
	        }
	    }
   }
  
}