To check for conflicts before you update a record, pass const clientOptions = {'ifUnmodifiedSince' : lastModifiedDate}. Get lastModifiedDate via a wire service adapter that returns a record object: const lastModifiedDate = record.fields.LastModifiedDate.value;.
recordInput has several properties.
recordInput Property
Type
Description
Required?
fields
Map
Map of field names to field values. See Update a Record in the User Interface API Developer Guide.
apiName
String
To create a record, specify the API name of an object from which to create the record. To update a record, use null or don’t pass this property.
triggerOtherEmail
Boolean
For a case, specifies whether to send email to users outside the organization. In Salesforce, this email can be triggered by creating, editing, or deleting a contact for a case. The default value is false.
triggerUserEmail
Boolean
For a case or a lead, specifies whether to send email to users in the organization. In the Salesforce user interface, this email can be triggered by various events: resetting a password, creating a user, changing the case owner, or adding comments to a case. The default value is false. For case owner changes, also set useDefaultRule=true, or no email is sent.
useDefaultRule
Boolean
For a case or lead, specifies whether to use the default (active) assignment rule. If you specify useDefaultRule, don’t specify an assignmentRuleId. The default value is false. For an Account, specifies whether to apply the default territory assignment rules.
allowSaveOnDuplicate
Boolean
Specifies whether to save a duplicate record. The default value is false.
Returns
A Promise object that resolves with the updated record. The record contains data for the fields in the record layout.
Usage
Before you use this wire adapter, make sure that there isn’t an easier way to update the data. To create a form for working with records, consider the lightning-record-*-form components.
Create a custom form only if you need more customization than the lightning-record-*-form components allow.
Note
This example creates a custom form that updates a record. It uses a getSingleRecord Apex controller to load the FirstName and LastName fields for a single contact record. (Name is a compound field. To edit the name, you must use the FirstName and LastName fields.)
As a custom behavior, the Update Contact button is disabled when one or both name fields are empty. The Id field is for reference only and is disabled so users can’t edit it.
Changes are updated using updateRecord. Requiredness is not enforced on the client-side. To display field-level errors on invalid fields, call reportValidity().
If you use the lightning-record-*-form components, all fields are displayed based on your Salesforce schema. For example, it automatically handles requiredness on the client-side for required fields. To customize how the form works, use updateRecord. For example, you can prevent users from leaving the non-required FirstName field blank to improve data completeness, or disable the button when specific fields aren’t valid.
Note
1import{LightningElement, track, wire}from "lwc";2import{ShowToastEvent}from "lightning/platformShowToastEvent";3import{updateRecord}from "lightning/uiRecordApi";4import{refreshApex}from "@salesforce/apex";5import getSingleContact from "@salesforce/apex/ContactController.getSingleContact";6import FIRSTNAME_FIELD from "@salesforce/schema/Contact.FirstName";7import LASTNAME_FIELD from "@salesforce/schema/Contact.LastName";8import ID_FIELD from "@salesforce/schema/Contact.Id";910export default class LdsUpdateRecord extends LightningElement{11 disabled = false;12 @track error;1314 @wire(getSingleContact)15 contact;1617 handleChange(event){18 // Display field-level errors and disable button if a name field is empty.19 if(!event.target.value){20 event.target.reportValidity();21 this.disabled = true;22}else{23 this.disabled = false;24}25}2627 updateContact(){28 const allValid = [...this.template.querySelectorAll("lightning-input")].reduce(29(validSoFar, inputFields)=>{30 inputFields.reportValidity();31 return validSoFar && inputFields.checkValidity();32},33 true,34);3536 if(allValid){37 // Create the recordInput object38 const fields = {};39 fields[ID_FIELD.fieldApiName] = this.contactId;40 fields[FIRSTNAME_FIELD.fieldApiName] = this.template.querySelector(41 "[data-field='FirstName']",42).value;43 fields[LASTNAME_FIELD.fieldApiName] =44 this.template.querySelector("[data-field='LastName']").value;4546 const recordInput = {fields};4748 updateRecord(recordInput)49 .then(()=>{50 this.dispatchEvent(51 new ShowToastEvent({52 title: "Success",53 message: "Contact updated",54 variant: "success",55}),56);57 // Display fresh data in the form58 return refreshApex(this.contact);59})60 .catch((error)=>{61 this.dispatchEvent(62 new ShowToastEvent({63 title: "Error creating record",64 message: error.body.message,65 variant: "error",66}),67);68});69}else{70 // The form is not valid71 this.dispatchEvent(72 new ShowToastEvent({73 title: "Something is wrong",74 message: "Check your input and try again.",75 variant: "error",76}),77);78}79}80}
The getSingleContact Apex method returns a single contact record.
1public with sharing class ContactController{2 @AuraEnabled(cacheable=true)3 public static Contact getSingleContact(){4 return[5 SELECT Id, Name, Title, Phone, Email, Picture__c6 FROM Contact7 WITH USER_MODE8 LIMIT 19];10}11}
This release is in preview. Features described here don't become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can't guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.