How to create transaction?

Please share an example of using context.createTransaction() in frontend and for doing the same in Action code

Hi Pranit,

To execute a series of database-operations from front-end code you should use the Transaction-API as you noted. Hereā€™s an example:

var tx = context.createTransaction();
tx.addOperation({
  op: "ADD",
  records: [rec1,rec2]
});
tx.addOperation({
  op: 'UPD',
  records: [rec3,rec4]
});
tx.commit().then(function(res) {
    alert("Success");
});

You can create, update and delete records in an atomic fashion this way (the operations are named ā€˜ADDā€™,ā€˜UPDā€™ and ā€˜DELā€™). We are currently putting a lot of errort into improving our documentation and will be detailing this over there shortly.

For handling transactions inside an action there is already an example here (scroll down to the section labeled ā€œWorking with the databaseā€:

https://docs.appivo.com/articles/scripts/

Thanks,

/Johan/

2 Likes