I am building a fairly complex web resource that uses the Dynamics Web API to read/write data to Dynamics. I have a situation where I need to create an entity that requires the ID from two other entities that I need to create. To do this, I use two asynchronous calls to create the two entities and then I use the data returned from the two asynchronous calls to fill in data records in my third entity I create. The problem I am having is that when someone clicks Save & Close, the Close occurs before my asynchronous calls complete. The first create call is sent and then the Close occurs without the second or third create occurring. I also fear that on Save, I most likely have a race condition with my Web API calls where if someone clicked Close fast enough after Save all the data may not be saved. Is there any way to get around this problem with asynchronous calls?
Some possible solutions I have explored that either don't work or I don't really like
- Disable Save & Close (possibly still have a race condition when doing Save and then Close)
- Batch Processing (not sure if this would even work, but way more difficult if it does)
- Use timeouts to pause close (don't think this will work)
- Cancel Close & reissue Close when save is complete (there has to be a better way, right?)