0

I found many resources available on stackexchange regarding this issue. We cannot make a callout after doing any dml. But i do not think i am doing any DML operation in my code.

Logical flow of my code is: Create/Update account in Org 2 > Process Builder is invoked and in turn it calls apex class(AccountManagerRest.AccountMigrator(list of IDs)), passing Account Ids > Calling a rest resource Post method to create/update account in Org 1.

Error : We can't save this record because the “Account Migrator” process failed. Give your Salesforce admin these details. An Apex error occurred: System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out Error ID: 988335765-184809 (1313514187) .

Apex class: Pasting code here.

Rest Service: Pasting code here.

Is internally any DML is being called? Because i read here that process builder invokes after saving the record, at step #14.

Any suggestions on how to tackle this problem. Thanks.

4
  • Put that into the future action – zaitsman May 15 '20 at 5:29
  • Thanks...I guess then I have to use a traditional trigger and can no longer use the process builder to execute the call. Makes sense! – KKumar May 15 '20 at 5:59
  • You can use the sprocess builder as long as you deffer the callout (quable job, future call, etc) – Sergio Alcocer May 15 '20 at 6:08
  • I used trigger instead of process builder. It is working now. But still unsure why I was getting this error. – KKumar May 15 '20 at 10:56
1

As you've mentioned that the process builder is executed when an AccountArecord is created/updated, that is why you are getting pending DML operation error.

As the API is being called in the same context in which that Account data is being updated/created, and the operation is not yet complete. The update/insert transaction completes ONLY when all validations+triggers+Process builders(including their invocations)+workflows +other related rules are completed.

To resolve this, you'll have to either pass the record Id from Process Builder 's invocable method to a future method, which will do the callout. Or you can do so using Batch (with Callouts tag, say which runs every hours) and run it for all records that have been created in the past one hour.

Whether you use future, batch, Queueable method, its up to you & the existing code base in your org. (eg: If the record itself is being created in a future context, then you can't call another future method in the same context)

The bottom line is that you have to separate these two operations.

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.