function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MrdiniMrdini 

Eeek! A bug! Undo! Redo!

Hi,

Whilst I am doing this in PHP, this is more of a general query, so placing it in this forum - hope this is ok!

I have a script that does the following (simplified)...
Insert into Accounts.
Retrieve Account ID.
Insert into Contacts using retrieved Account ID
Retrieve Contact ID
Insert into Opportunity using retrieved Account ID
Retrieve Opportunity ID
Insert into Opportunity Line Item using retrieved Opportunity ID
Insert into custom object using Account, Contact & Opportunity ID

Now, summing up, there are five INSERTs done using the SF API. The problem here is that since SFDC doesn't seem to have transactional support in the API (only in Apex), how can I roll back cleanly all the above INSERTs if there was an error towards the end?

Is there a way of somehow putting all the INSERTS into one single action?

There are other scripts with similar issues, but the above illustrates the problem quite well...

TIA for any inputs you may have,

Y.
SteveBowerSteveBower

Sadly, I think you've got it right. 

The API does not supply transactions or transactional consistency.  If you're in PHP, you must roll your own.  E.G. Keep track of the ID's you've created successfully, and in case of a failure, go back and delete them.

There is no way to bulk them and have the ID's automatically propogate for you.

Note however that workflow, triggers, etc. will already have fired for the inserts, so secondary effects, if any, will also have to be handled by you if you need to rollback.

If you have APEX, you'd be better off using PHP to invoke a saved procedure to insert the whole chain for you.  Salesforce provides default transactional control around an entire APEX code block.  (They also provide savepoints and rollback in case you need partial recovery, but that's not your case.)

Best, Steve.