Conditional actions in Transactions

Is it possible to perform an action within a Transaction that is conditional on the result of a previous action?

For example, within the transaction:

  1. Find a user
  2. if user.meeting == null, create a meeting for the user

I understand Counters provide a very basic form of conditional atomic action, but wasn’t able to figure out this more general case.

Hi, @JohnP

Try using a Find operation with the required whereClase. And then use that opResultId in the next operation. All these steps can be executed in one transaction. Here are the documentation that will help you perform this operation:

  • Retrieving objects operation

Retrieving objects - Backendless REST API Documentation

  • Using opResultId as argument

Operation Result - Backendless REST API Documentation

  • Saving a single object

Saving a single object - Backendless REST API Documentation

Regards,
Marina

Yes! That works!

I rewrote the FIND operation of my transaction as:

const userResolvedId = unitOfWork.find("Online", Backendless.DataQueryBuilder
  .create()
  .setWhereClause(`objectId = '${myUserid}' AND meeting is null`)
  .setPageSize(1))
  .resolveTo(0, 'objectId')

and if my user’s meeting is not null (ie, they already have a meeting), resolveTo will throw Error: Index 0 out of bounds for length 0 and the whole transaction fails, which is the desired result.

Is it possible someone could explain or demo this idea using Codeless blocks?

Specifically, we would like to run a transaction where we 1) find objects with a where clause, then 2) if there are none found, create an object, and then finally 3) set a relation for the 1st object in the list of “find” results OR the newly created object from step 2.

@Dima just pinging you in case comments on “solved” issues don’t appear on anyone’s radar… we would like to do conditional logic in a transaction where the result of the first operation informs whether or not to perform a second operation, but using Codeless blocks (see above). Possible?