+ Start a Discussion
craigmhcraigmh 

Trigger Issue - Wrong Error Message? - Other Issues

Here is the error message in the email (IDs hidden):

 

Apex script unhandled trigger exception by user/organization: 005xxxxxxxxxxxx/00Dxxxxxxxxxxxx

TRIGGERNAME: execution of AfterUpdate

caused by: System.DmlException: Insert failed. First exception on row 0 with id 00kxxxxxxxxxxxx; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

Trigger.TRIGGERNAME: line 70, column 1

 

The trigger copies a custom object that holds the products for Lead. When the Lead is converted to an Opportunity, the trigger creates an OpportunityLineItem for each Lead Product, like so (after update on the Lead, pbIDs is a Map<string, Id> with the PricebookEntry IDs):

 

for(LeadProduct__c prod:leadProducts) {
	OpportunityLineItem newProd = new OpportunityLineItem();
	newProd.PricebookEntryId = prod.PricebookEntry__c;
	newProd.OpportunityId = l.ConvertedOpportunityId;
	newProd.Quantity = prod.Quantity__c;
	newProducts.add(newProd);
}
insert newProducts;

 

I was thinking that there may be some trigger doing an Insert with an ID, but wouldn't the error email have the name of that trigger? It has the name of mine. And as you can clearly see, my trigger is not specifying the ID for the records that it's inserting.

 

What's even stranger is that the User ID specified in the email does NOT exist. I checked it, and got an "Insufficient Privileges: You do not have the level of access necessary to perform the operation you requested. Please contact the owner of the record or your administrator if access is necessary." I send the ID to my system admin, and THEY get the same exact issue. So I'm thinking that this may be occurring in a test, since this process is working correctly, except for one user that doesn't actually exist.

All Answers

craigmhcraigmh

More information:

  • the OpportunityLineItem IDs in the error emails are also referencing records that do not exist
  • this trigger has not been changed since I wrote it in October 2010 until this issue arose
  • I have not been able to trigger this issue. I have not received error emails from the Sandbox instance, only Production.
  • There is no correlation with records created, and the error emails.
  • I remember reading documentation about a specific "Test" User for each organization, something related to the Summer '12 changes. Does anyone know I can find this information again? I couldn't find it in the Summer '12 changes PDF.
craigmhcraigmh

Looks like that was the issue.

 

I was declaring "newProducts" outside of the loop through the trigger Leads. Moved the declaration inside, haven't had any more error emails.