3

In Aura, it's standard practice to call an @AuraEnabled Apex method for all database CRUD operations.

With LWC, Trailhead promotes the use of lightning/uiRecordApi (User Interface API under the hood) for retrieving records from the database.

Tantalizingly, the lightning/uiRecordApi documentation makes reference to several methods that seem to be tailored toward more advanced use cases, e.g.:

  • createRecord()
  • createRecordInputFilteredByEditedFields()
  • generateRecordInputForCreate()
  • generateRecordInputForUpdate()
  • updateRecord()

I'd like to use a standard set of tools for forms that create, read, update and delete records, but I'm concerned about the fact that they have been largely omitted from Trailhead examples, and haven't been much discussed on StackExchange. (Note: there is one createRecord() example in the lwc-recipes)

What factors should I consider before I use these APIs? For those who have attempted to use them, what advantages/disadvantages do they have in comparison with Apex?

  • 5
    It is a new way to do it. I still prefer to use APEX for DML where exception handling is still better. I am also a fan of the Factory pattern where we encapsulate the DML per object in a class for better control. – Avijit Chakraborty Dec 6 '19 at 18:01
3

My take on this would be, lightning/uiRecordApi is very very primitive, it's as good as using Standard Rest/Soap API to do CRUD from SF or from 3rd party.

Following factors you need to consider before you go this route:

  1. lightning/uiRecordApi does not support bulk records updates, this means you have to call it multiple times(In case you wanna update multiple rows of the data table or multiple objects)
  2. As Avijit mentioned, there is no transaction control, which implies, if you update/insert multiple from JS, there is no complete rollback.
  3. You would want to sanitize your data on Server Side apex code, the reason being JS being browser-based is easy to hack, checking data sanity in triggers and validation rule might be bit late. The goal is to prevent bad data as soon as possible
  4. Complex requirements like Disabling trigger while inserting few records cant be performed from lightning/uiRecordApi

All and all, if it's very primitive crud work you wanna do, go for uiRecordApi, if its more complex stick with apex.

Prior to LWC or Lightning, we used to use JSForce to do simple crud, my opinion is uiRecordApi is SF native solution to this.

| improve this answer | |
  • Just the kind of info I was looking for -- thank you! – Matthew Souther Dec 6 '19 at 22:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.