+ Start a Discussion

How to do an atomic operation with salesforce api?

Let's say that I'd like to implement some operation in an atomic fashion.  The specific example is that I'd like to implement my own auto-counters that only fire in a specific data-based situation.  So, I have an s-control that the user clicks on to "get next counter".  This can go off to a "counter" custom object, get the current value, increment it, and save it back so the next usage will have the new value.

The problem is that this isn't an atomic operation, and there is no way to implement a critical section across all the potential users of the s-control at the same time.  So, if the timing were really unlucky, two users could get the same value which defeats the purpose.  (Yes, this is unlikely, but...)

The best I could do would be to roll my own with a semaphore custome object of some kind.  But, that means an extra set of calls wrapped around my critical sections as well as browser timeouts, backoffs, etc.  And, it's a pain to code for every situation in different s-controls.

Does Salesforce have any API method for doing this that I'm not aware of?  And, assuming not, can we put in a request for some sort of server-side semaphore object?  Or, perhaps an atomic sort of:

sforceclient.execute("Object Type", "ObjectID","Formula definition"); call.

I envision this doing a Retrieve for all fields on the requested ID, executing the Formula (using the same syntax, rules, etc. as the Formula Field definitions), and then an Update.  All as one server side call.

Example:  sforceClient.execute("Opportunity","00630000002zTEJ","{!Something}={!Something}+1");

Thanks, Steve Bower.

P.S. I only have a developer account, so I don't think I can make the enhancement request myself.

Why don't you just use an auto number field ? (either directly, or as a sequence table)
Hello Simon,  Well, The whole "sequence number" thing is sort of a side-issue to the question regarding  atomic transactions.

Sure, using an auto-number field would work if you were ok with non-sequential numbers.  But, what if you're not.  Using a sequence table (I presume you mean setting up a different custom object with a single auto-number field and adding a record as needed to get an auto-number?)  Sure, that would work for this specific example, but it's pretty ugly, etc.

Aside from that, my real question was about atomic transactions.  The "increment a field" was just my simplest example.

Any thoughts?

Thanks, Steve Bower.

I believe the general idea we're going for is to be able to declarivitiy set these kinds of things up via workflow.