Code By Code – Lightning Framework – Chapter 20 – Lightning Data Service – Create Or Save A Record

Code By Code – Lightning Framework – Chapter 20 – Lightning Data Service – Create Or Save A Record

Chapter 20

Topics for this chapter:

  • Create Or Save A Record Using LDS

Create A Record Using LDS

  • To create a record using Lightning Data Service, declare force:recordData without assigning a recordId.
  • Next, load a record template by calling the getNewRecord function on force:recordData.
  • Finally, apply values to the new record, and save the record by calling the saveRecord function on force:recordData.
// Prepare a new record from template
       component.find(“contactRecordCreator”).getNewRecord(
           “Contact”, // sObject type (objectApiName)
           null, // recordTypeId
           false, // skip cache?
           $A.getCallback(function() {
               var rec = component.get(“v.newContact”);
               var error = component.get(“v.newContactError”);
               if(error || (rec === null)) {
                   console.log(“Error initializing record template: ” + error);
                   return;
               }
               console.log(“Record template initialized: ” + rec.sobjectType);
           })
       );

Saving a Record Using LDS

  • To save a record using Lightning Data Service, call saveRecord on the force:recordData component, and pass in a callback function to be invoked after the save operation completes.
  • The Lightning Data Service save operation is used in two cases.
    • To save changes to an existing record
    • To create and save a new record
  • To save changes to an existing record, load the record in EDIT mode and call saveRecord on the force:recordData component.
component.find(“recordHandler”).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));
           }
       }));

AddCarExperience Component

Component Markup

Component Controller

Component Helper

Lightning Component Development Day9 - Application Event, Use LDS to Create Or Save Record

Next In This Guide

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