2

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;
    }
}
  • Whats your logic behind who the Owner should be? If you think of that you will be able to build something to populate that – EricSSH Apr 9 '15 at 18:04
  • The opp should be assigned to the specific sales manager, hence the reason his user id is hard coded in the trigger. – Chris K Apr 9 '15 at 20:57
  • I get why you hard coded it, but it still shouldn't be done. – EricSSH Apr 9 '15 at 21:01
12

You have your DML statement inside of the for loop. So it is trying to insert the list each time through the loop. This will work fine for a single transaction, but the minute you do this with more than 1 transaction, it will fail.

In the case of multiple transactions, the first time through the loop, it will work fine, as there is only 1 opp in it. The second time through the loop, your list now contains 1 new opp and 1 opp that you already inserted, so its trying to re-insert that opp, thus causing the error.

Move the DML outside of the loop like below

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;
}

Just as a general best practice as well, you also might want to avoid hard coding IDs in your code as it could cause issues when deploying between different sandbox and production orgs.

Check out the Apex Best Practices, it includes the hardcoding and the DML outside of loops (see #2 and #10). Should help you out in the future.

| improve this answer | |
  • Thank you, sfdc_ninja. I should have caught that. Much appreciate the help! – Chris K Apr 9 '15 at 20:56
  • No problem. Glad to help – Chris Duncombe Apr 9 '15 at 20:57
0

You shouldn't be performing DML inside a loop, This exception comes if you try to insert already inserted record . Account a =new Account(); a.name='Hello'; insert a; insert a;

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.