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
ChrisKozChrisKoz 

Error Message: createOpp: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0 with id 006C000000ybKu1IAE; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

I'm pretty new to Apex and writing a simple trigger for when an account submits a form on our website (tracked on the formSubmit__c object) to create an Opportunity and am getting the following error message:

createOpp: execution of AfterInsert
 
caused by: System.DmlException: Insert failed. First exception on row 0 with id 006C000000ybKu1IAE; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
 
Trigger.createOpp: line 16, column 1

My code is below. I received this error message yesterday when our feed created a new account when a new prospect submitted the form (we don't use Leads, only Accounts). I went to the new account's record, made a trivial edit on the form submit record, and it created the Opportunity just fine. If our feed is creating a new account as someone submits the form, it should still be able to fetch the account ID since this is an after insert trigger and the account a lookup field on the form submit record. What__c is the associated lookup field from form submit to account. Any help would be appreciated!

trigger createOpp on formSubmit__c (after insert,after update) {
  List<Opportunity> newOpps = new List<Opportunity>();
  for (formSubmit__c f : Trigger.new) {
    if (f.Subject__c.startswith ('Form Submit: Account Submitted a form. Please Contact immediately')) {
    Opportunity opp = new Opportunity();
    opp.Name        = 'Information Request Lead';
    opp.StageName   = 'Qualification';
    opp.Short_Description__c   = 'Information Reuqest';
    opp.Type   = 'Inbound';
    opp.CloseDate   = Date.today() + 7;
    opp.AccountId   = f.What__c; // Use the trigger record's Account ID
    opp.OwnerId   = '00580000003DcBHAA0';
    newOpps.add(opp);
      }
  insert newOpps;
    }
}

 
Sforce.NinjaSforce.Ninja
Are there any before insert triggers in the org?
ChrisKozChrisKoz
There is a before trigger on the Task object, but no other before triggers. There is a workflow to update the opportunity name, but that's obviously an after.
David ZhuDavid Zhu
I guess this line causes the error.
opp.AccountId   = f.What__c; // Use the trigger record's Account ID
You may add "system.debug(f.what__c)" to check what the value is.