Interview Questions – Lightning Framework – Part 9 – Lightning Data Service

Interview Questions – Lightning Framework – Part 9 – Lightning Data Service

Checkout earlier posts in interview question series to read previous questions.

71. What is Lightning Data Service?

Lightning Data Service aka LDS allows you to create, retrieve, delete or update Salesforce records without any need of Server side controller. To understand it, you can think of LDS as standard controller of Lightning Component.

Data access with Lightning Data Service is simpler than the equivalent using a server-side Apex controller. Read-only access can be entirely declarative in your component’s markup. For code that modifies data, your component’s JavaScript controller is roughly the same amount of code, and you eliminate the Apex entirely. All your data access code is consolidated into your component, which significantly reduces complexity.

72. Which tag allows to use LDS?

force:recordData allows you to include LDS in your component. This tag can be used to perform any DML operation with the help of LDS. Example:

    <force:recordData aura:id="recordLoader"
      recordId="{!v.recordId}"
      targetFields="{!v.simpleRecord}"
      targetError="{!v.recordError}"
      recordUpdated="{!c.handleRecordUpdated}"
      />

73. Can LDS be used on multiple records?

No, LDS can only be used on a single records. Operations on multiple records are not supported yet.

74. What are different components based on LDS or force:recordData?

There are multiple components developed using LDS.

  • lightning:recordViewForm – For viewing a single record. You can display complete information of the record without the help of server side controller. You can even fetch all the fields from page layout in this component.
  • lightning:recordEditForm – For editing a single record. You can edit and read complete information of the record without the help of server side controller. Creation of new records is also supported.
  • lightning:recordForm – This is the combination of above 2 components. This is a very powerful component and allows you to display, create or edit a record without any server side controller.

75. What are considerations of using Lightning Data Service?

Lightning Data Service supports primitive DML operations—create, read, update, and delete. It operates on one record at a time, which you retrieve or modify using the record ID. Lightning Data Service supports spanned fields with a maximum depth of five levels. Support for working with collections of records or for querying for a record by anything other than the record ID isn’t available. If you must support higher-level operations or multiple operations in one transaction, use standard @AuraEnabled Apex methods.

Lightning Data Service shared data storage provides notifications to all components that use a record whenever a component changes that record. It doesn’t notify components if that record is changed on the server, for example, if someone else modifies it. Records changed on the server aren’t updated locally until they’re reloaded. Lightning Data Service notifies listeners about data changes only if the changed fields are the same as in the listener’s fields or layout.

Also Read : Designing And Developing Lightning Component – Best Practices

76. How can we handle record changes in LDS?

You can perform any client side computation and run your complex tasks when the record is updated. For that, handle recordUpdated event of force:recordData component. Example:

—Component Code—

<force:recordData aura:id="forceRecord"
    recordId="{!v.recordId}"
    layoutType="FULL"
    targetRecord="{!v._record}"
    targetFields="{!v.simpleRecord}"
    targetError="{!v._error}"
    recordUpdated="{!c.recordUpdated}" />

—Controller Code—

({
  recordUpdated: function(component, event, helper) {
    var changeType = event.getParams().changeType;
    if (changeType === "ERROR") { /* handle error; do this first! */ }
    else if (changeType === "LOADED") { /* handle record load */ }
    else if (changeType === "REMOVED") { /* handle record removal */ }
    else if (changeType === "CHANGED") { /* handle record change */ }
})

77. List out advantages for Lightning Data Service?

  • Perform DML operations without the need of Apex Code or Server Controller.
  • Inbuilt strong cache mechanism to cache loaded information in browser.
  • If multiple components operating on same record, the record is loaded only once and shared among the components.

78. How does LDS share records among components?

Lightning Data Service records are shared across multiple components, loading records provides the component with a copy of the record instead of a direct reference. If a component loads a record in VIEW mode, Lightning Data Service automatically overwrites that copy with a newer copy of the record when the record is changed. If a record is loaded in EDIT mode, the record is not updated when the record is changed. This prevents unsaved changes from appearing in components that reference the record while the record is being edited, and prevents any edits in progress from being overwritten. Notifications are still sent in both modes.

79. Which LDS method is used to save/update a record?

saveRecord method is used to save a record using Lightning Data Service.

component.find(“forceRecord”).saveRecord($A.getCallback(function(saveResult) {
            // NOTE: If you want a specific behavior(an action or UI behavior) when this action is successful
            // then handle that in a callback (generic logic when record is changed should be handled in recordUpdated event handler)
            if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
                // handle component related logic in event handler
            } else if (saveResult.state === "INCOMPLETE") {
                console.log("User is offline, device doesn't support drafts.");
            } else if (saveResult.state === "ERROR") {
                console.log('Problem saving record, error: ' + JSON.stringify(saveResult.error));
            } else {
                console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
            }
        }));

80. Which LDS method is used to delete a record?

deleteRecord method is used to delete a record using Lightning Data Service.

component.find("forceRecord").deleteRecord($A.getCallback(function(deleteResult) {
            // NOTE: If you want a specific behavior(an action or UI behavior) when this action is successful
            // then handle that in a callback (generic logic when record is changed should be handled in recordUpdated event handler)
            if (deleteResult.state === "SUCCESS" || deleteResult.state === "DRAFT") {
                // record is deleted
                console.log("Record is deleted.");
            } else if (deleteResult.state === "INCOMPLETE") {
                console.log("User is offline, device doesn't support drafts.");
            } else if (deleteResult.state === "ERROR") {
                console.log('Problem deleting record, error: ' + JSON.stringify(deleteResult.error));
            } else {
                console.log('Unknown problem, state: ' + deleteResult.state + ', error: ' + JSON.stringify(deleteResult.error));
            }
        }));
  •  

Manish Choudhari

I am a certified Salesforce Application & System Architect and Developer working on Salesforce Technology since 2014. Currently, I have 14 Salesforce certifications along with OCPJP (Oracle Certified Profession JavaSE6 Programmer) working in Salesforce.com Hyderabad as a Technical Engineer. Writing technical blogs, learning new technologies and frameworks and sharing knowledge is my hobby.

This Post Has One Comment

Leave a Reply

Close Menu