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 = '00580000003DcDHAA0';
newOpps.add(opp);
}
insert newOpps;
}
}