0

From Trigger on inserting I am making a callout (from method1 of class A) i.e by using @future annotation and updating the field of the same object on which the trigger called, with the values acknowledged by the callout.

I want to call another method (method2 of class A) and make callouts by using the values updated earlier by method1.

The problem is, if I am calling it from the method1 after finishing the updates(DML) its throwing "you have uncommitted work pending. please commit or rollback before calling out" error if I don't make it future method and its giving the error "Future method cannot be called from a future or batch method" if I make it future.

And if do not call it from method1 and call it directly from trigger, there are two ways. One is to call it from isUpdate portion by which it will be called automatically when DML of method1 occurs and again I will get the error "Future method cannot be called from a future or batch method". Second is to call it in isInsert portion but as both method1 and method2 are future methods then they'll be called asynchronously, so if method2 is got to call first then it cannot use the values that would be updated by method1.

P.S : Is there any way to call two methods from a trigger and in both the methods I am using callouts and I want to use values in second method after first method performs DML operation?

  • are you getting some values from first callout that you are then passing in second callout and also using those value in record insertion? – User6670 Feb 9 at 19:48
  • Yes, ideal sequence is like on Insert first callout > Update > second callout. – Navii Feb 9 at 20:01
  • 1
    why not let both the callout complete and then update the record? – User6670 Feb 9 at 20:03
  • Yup, that solves the problem. How can I miss that. Thanks!! – Navii Feb 9 at 20:12
1

You can Make all the callout that need to happen first and then at the end make the DML operations that would stop the
you have uncommitted work pending. please commit or rollback before calling out

| 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.