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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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:
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)- As Avijit mentioned, there is no transaction control, which implies, if you update/insert multiple from JS, there is no complete rollback.
- 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
- 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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0