[SalesForce] Practical Use of Lightning Data Service

From the trailhead/docs I understand LDS is similar to Standard Controllers.

I followed this example given in the docs to understand more about it.

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/data_service_example.htm

Please correct me if I am wrong but I believe this LDS is too much of work which can be achieved simply writing code in the controller.

To be more specific (as this forum recommends), can someone let me know of an actual use real life case where LDS is better than not using it ?

Best Answer

I haven't used LDS yet to replace Apex code, or for inserting/updating records. But an incredibly useful ability I found is using LDS to monitor for updates to records and then acting upon that update.

For example, I have one component that presents multiple records in kanban type view. I use an aura:iteration to create an LDS instance for each record, with a handler for updates.

If the user makes an update to any of those records anywhere in the app, even if it's using standard built-in functionality (such as an update record screen somewhere else), my component can detect those updates via LDS and refresh itself.

Using this approach, I was able to do away with things like firing events from all other customer components when records were updated, or trying to catch force:refreshView events if a standard component made an update (which never worked very well).

This is an ability in LDS that cannot be reproduced using Apex code. Even though trigger code might run when records are updated, there's no way for them to tell a component that the update occurred unless you publish a Platform Event as part of trigger execution, and the component listens for the Platform Event. I find LDS to be simpler right now than trying to wire up Platform Events, even though LDS's scope of listening to updates is only within the same user's app, and Platform Events could be more global.

Anyway, hope this info helps.

Related Topic