0

I have a method: public static CreateUpdateAPI (Account acc) { //do WS callout //perform DML }

When i call it from Anonymous console with one Account it runs properly. But when i call the method with 2 accounts, i get this error when trying to do the Callout for the 2nd account. I cannot understand why it things it is one transaction.. Since i call the method for one account, performs the job and then call again the method for another account. On the second account i get this error before calling out. Any suggestions please ?

3

A single transaction runs from the moment an execution context is entered until it ends. This can include multiple DML statements, as the DML operations are not fully committed until the transaction ends successfully. As an example, this execute anonymous script would fail:

Account a = new Account(Name='Test 1');
Account b = new Account(Name='Test 2');
myCLass.CreateUpdateAPI(a);
myClass.CreateUpdateAPI(b);

Instead, bulkify your method to accept a list of records:

public static void CreateUpdateAPI(Account[] records) {
  // Do all callouts first //
  // Then do any DML later //
}

Generally speaking, you must bulkify methods that perform callouts or DML operations anyways to avoid running in to various limits.

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.