1

I have a problem with the code below giving me the following error:

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

@testSetup
static void setUp(){
    List<Account> accList = new List<Account>();
    String billingState = 'NY';
    for(Integer i=0; i<2; i++){
        accList.add(new Account(Name='TestAccountABC ' + i, BillingState = billingState));
    }
    insert accList;
}

I have a similar setUp() class that I use to make leads for a different test class that works the same way(for loop that adds new leads to list that I insert after the loop) but that one works. So I am wondering what mistake I am doing in my code and a way to fix it.

Thank you!

EDIT: As was pointed out the code above is actually fine. Since Stack Trace for the error gave me the row where my setUp() class was inserting records I assumed the error was there. Thank you to the both of you manjit_singh and NITHESH K for helping me figure it out.

2
  • i have tried your code, its working fine. Nothing Found any error in that. – NITHESH K Jul 27 '17 at 9:50
  • Can you add more code – NITHESH K Jul 27 '17 at 9:51
1

If you see your error closely it says that the error is not while inserting Account but while inserting Contact.

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

003 is SFDC prefix for a Contact. So the error is not in the code you posted but somewhere else. Somewhere where you are inserting the contact.

Cause:

It looks like that you are specifying an ID while inserting a record which is not allowed by SFDC.

Since you are inserting and Account and assuming you are inserting contact as well. Just make sure you have not done this accidentally.

con.Id = acc.Id

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.