I am trying a build an integration structure for Request and Response mode. Suppose we have three SObjects, A, B, C. B is child of A and C is child of B. They all need to get integrated into a 3rd party environment via webservice callout. And in the response, it gets the external ID back and store into Salesforce.
Suppose we created three records of those Object in short amount of time, namingly we created record a, b, c. Originally I wrote the code like this:
List<A> aList = [Select Id, name, ... From A where Id in ... ]; //... resp = http.send(req); update aList; List<B> bList = [Select Id, name, ... From B ...];
By using this piece of code, I am getting you have uncommitted work pending error. Haven't tried in batch environment though.
So does that mean I should put the integration of different objects into different jobs? Then how do I guarantee the execution sequence? Should I set like 1 minute interval for each job run? Or is there a better solution for this?
Maybe I didn't make it very clear at the first instance. I totally understand why I am getting this error and I am fully aware of the solution to do all the callouts first and then DML. But this is not an option in my scenario. We need to keep the same structure in our 3rd party system as in Salesforce. So without knowing the external ID which is getting back from a's web service call, b doesn't know how it should get restored in 3rd party system. So I am not quite seeing doing every callouts in the first place as an option here - unless I keep all of them just in the memory without doing the DML.