Database transaction scope and client calls

As discussed in Chapter 4, Apex Execution and Separation of Concerns, the transaction context spans the execution context for a request made to the Salesforce server from a client. When writing the client code in JavaScript, be careful when using CRUD-based communication options such as Lightning Data Service, JavaScript Remote Objects, and the Salesforce API, as the execution scope in the client does not translate to a single execution scope on the server.

Each CRUD operation that the client makes is made in its own execution scope and, thus, its own transaction. This means that if your client code hits an error midway through a user interaction, prior changes to the database are not rolled back. To wrap several CRUD operations in a single database transaction, implement the logic in Apex and call it through one of the previous means, such as Visualforce JavaScript Remote Actions or a Lightning Server Side Action. Note that this is generally a good practice anyway from a service-orientated application design perspective.

If your client code is able to utilize the Salesforce REST APIs (as per the preceding table), you can also consider using the Salesforce Composite and Tree APIs. These APIs allow you to group multiple records together in one operation, and thus they will be performed in a single transaction.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.185.194