Hello Salesforce community,
I have a problem with Callouts and Commits in APEX: Error is: "You have uncommitted work pending."
Fact in Salesforce:
- You can't do a callout when you have outstanding (uncommited) DML statements
For a number of SObjects X__c I need to do the following:
- Do a callout to a webservice to get data which builds the X__c record
- Create an Account and fill the ForeignKey in X__c with the ID of the created Account.
But the Account.id is only available AFTER the insert. And I can't do the insert because the loop has other callouts to do.
- Generate all the X__c objects with all their Callouts
- Store result in a List of X__c objects
- Put all created Accounts also in a List
- Somehow 'connect' each Account to its matching X__c (in a third List?)
- Upsert both lists at the end.
This seems to me like a difficult solution just because Salesforce doesn't allow callouts between DML statements.
Is there another, better, way to program this?
Thanks in advance for your help.