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.