When should I use JavaScript for database access?

Lightning Data Service and JavaScript Remote Objects (Visualforce) are designed to expose a "SOQL- and DML-like API" for use by client JavaScript code. They provide a way to query and update the database without having to go through the legacy AJAX Toolkit or Salesforce REST APIs that incur charges to the daily API limit.

Visualforce Developer's Guide has an excellent topic that describes the best practices around using this feature, entitled Best Practices for Using Remote Objects (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_remote_objects_considerations.htm), which I highly recommend that you read and digest fully. The Lightning Aura Component Developer Guide also has samples describing the Lightning Data Service (https://developer.salesforce.com/docs/atlas.en-us.218.0.lightning.meta/lightning/data_service.htm) and the Lightning Web Component Developer Guide (https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.data_ui_api).

The main aspect from the references that I would like to highlight is the one that relates to your awareness of transactional boundaries in close conjunction with maintaining a good Separation of Concerns. Resist the temptation to invoke multiple database operations within a single JavaScript code block, as each will be executed in its own Salesforce execution context and, thus, transaction. This means that if an error occurs in your JavaScript code, previous operations will not be rolled back.

If you find yourself in this situation, it is also likely that you should be positioning such code in your application's Service layer and using the applicable communication method to call an Apex controller method that can invoke your Service layer method. At this point, you can perform multiple database operations knowing that they will be wrapped by a transaction.

That said, if you have use cases that result in a single database operation, then you can, of course, consider using this feature, safely assured that your Apex Trigger and Domain layer code will continue to enforce your data validation and integrity.

Finally, note that querying records from JavaScript does not invoke your Selector code. So the fields queried and populated in the resulting SObject records on the client will not always be the same in all cases throughout your JavaScript code.

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

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