I am able to load the static resource but I am unable to insert the loaded accounts into salesforce. I want to do this to be able to test various methods that depend on first selecting a particular salesforce record and then perform some type of update logic on it.

I have a CSV in the following format added via static resources:

"llemonaid.com","LEMONAID CO","316666665","33 A ST NW","CEDAR RAPIDS","IA","522222003","United States"

The following is my load and insert code that is failing with an error: System.DmlException: Insert failed. First exception on row 0 with id 001E000000z1iMpIAI; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

private class MatchController_Tests
   static testMethod void testUpdateAccountRecord()
      MatchController controller = new MatchController();

      List<sObject> accountsList = Test.loadData(Account.sObjectType, 'testAccounts');
      List<Account> insertList = new List<Account>();

      //Copying into another List was another attempt I made when the first
      // List<sObject> failed to insert.
      for (Integer i = 0; i < accountsList.size(); i++) 
          Account acct = (Account) accountsList[i]; 

      System.assert(insertList.size() == 100);

      insert insertList;

1 Answer 1


The documentation says:

loadData(Schema.SObjectType, String)

Inserts test records from the specified static resource .csv file and for the specified sObject type, and returns a list of the inserted sObjects.

so the objects are already inserted when the method returns and so have new ID values assigned and can't therefore be inserted again. If you want to change any values you can update or upsert.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .