Visualforce to LWC: Creating Records using Lightning Data Service

Visualforce to LWC: Creating Records using Lightning Data Service

In my previous article I showed you how we used to create records in Visualforce and how can do it in Lightning Web Components using the lightning-record-form. We also agreed that there are 3 ways to access data in lightning:

  • Record Form Base Components
  • Lightning Data Service
  • Apex

It's important to note that Record Form Base Components is also a part of the Lightning Data Service. Lightning Data Service or LDS is the preferred and easiest way to work with salesforce data. LDS is built on top of the User Interface API, it provides security and maintains consistent, up-to-date data and metadata across multiple components.

No alt text provided for this image

Today I will be using LDS to create records in Lightning Web Components. We will create a component to insert a contact using the LDS function createRecord.

No alt text provided for this image

HTML:

No alt text provided for this image

Line 6-8: We use the lightning-input component to capture the data that should be inserted in the 3 fields (FirstName, LastName, Phone).

Line 12-13: Our lightning-button when clicked will trigger the createCon event handler.

Line 18: We're using a variable called message to display a custom message if the contact is saved successfully or if an error occurs during the saving operation.

JS:

No alt text provided for this image

Line 2: We import the createRecord LDS function.

Line 10-11: This change handler is triggered whenever a change occurs in the lightning-input and what it does is basically capture the text entered in the lightning-input and stores it in a variable called firstName which we defined in line 5. The same happens for lastnameCH and phoneCH.

Line 20: createCon event handler will fire when we click the button.

Line 24: We invoke the LDS function that we imported in line 2, and we pass to it the apiName of the object and the fields array. When we invoke an LDS function imperatively, it returns a javascript promise (then , catch).

Lines 24–26: In the then method, we define what happens if the contact is created successfully.

Lines 26–27: In the catch method, we define what happens if the contact creation fails.

You can try this component in your own org! An example that would trigger the catch method is if you leave the last name field empty, because this field is required on the Contact object.

Please note that LDS functions allow you to work with single records only. 

Gidi Abramovich

Salesforce Solutions Architect

4 年

Farah, the lines explanations are very useful for non-developers like me. Thank you for sharing your content with us!

Walid El Horr

17x Salesforce Certified Application Architect | Principal Salesforce Engineer at Canada Life

4 年

I like the animated Gif and the line by line instructions!

要查看或添加评论,请登录

Farah Sherif Ghanem的更多文章

  • When to call an Apex Action from a Flow?

    When to call an Apex Action from a Flow?

    Flows can do almost everything these days, the question I always had was what can flows NOT do? Why not use flows to…

    16 条评论
  • Remove Rich Text Formatting in Flows

    Remove Rich Text Formatting in Flows

    There are times in which you want to copy text from a Rich Text Area field to a Long Text Area field. When you try to…

    27 条评论
  • WITH SECURITY_ENFORCED in Salesforce Apex

    WITH SECURITY_ENFORCED in Salesforce Apex

    In this article I will be talking about the WITH SECURITY_ENFORCED clause and why as Developers we use it in our SOQL…

    11 条评论
  • Prepare for Salesforce JavaScript Developer 1 from Scratch

    Prepare for Salesforce JavaScript Developer 1 from Scratch

    Salesforce Javascript Developer 1 Certification is a proctored exam from Salesforce.com.

    2 条评论
  • Build a Custom Search Component with LWC

    Build a Custom Search Component with LWC

    The best way to create an amazing user experience on Salesforce Platform is by using Lightning Web Components. In this…

    3 条评论
  • Visualforce to LWC: Creating Records using Apex in LWC

    Visualforce to LWC: Creating Records using Apex in LWC

    There are 3 ways to create records in Lightning Web Components: Record Form Base Components Lightning Data Service Apex…

    2 条评论
  • Sharing and Visibility Designer Certification

    Sharing and Visibility Designer Certification

    Several individuals from the Ohana have been asking me about the Application Architect certification and how to pass…

    16 条评论
  • Introduction to Salesforce CPQ

    Introduction to Salesforce CPQ

    Ever wondered what is Salesforce CPQ? Salesforce CPQ (Configure, Price, Quote) is an application built on the…

    6 条评论
  • Visualforce to LWC: Creating Records using lightning-record-form

    Visualforce to LWC: Creating Records using lightning-record-form

    Hello Ohana, Welcome back to the Visualforce to LWC series. In this article I will be discussing how to add records to…

    2 条评论
  • Visualforce to LWC: Conditional Rendering

    Visualforce to LWC: Conditional Rendering

    Hello Ohana, Welcome back to the Visualforce to LWC series. In this article I will be discussing Conditional Rendering…

    2 条评论

社区洞察

其他会员也浏览了