Skip to content

Instantly share code, notes, and snippets.

@karkranikhil
Created August 2, 2019 03:39
Show Gist options
  • Select an option

  • Save karkranikhil/48e6613cf21e9c40e01c86f2557a6a13 to your computer and use it in GitHub Desktop.

Select an option

Save karkranikhil/48e6613cf21e9c40e01c86f2557a6a13 to your computer and use it in GitHub Desktop.
Handle Record Changes and Errors
<aura:component implements="force:hasRecordId,flexipage:availableForRecordHome">
<aura:attribute name="record"
type="Object"
description="The record object to be displayed"/>
<aura:attribute name="accountRecord"
type="Object"
description="A simplified view record object to be displayed"/>
<aura:attribute name="recordSaveError"
type="String"
description="An error message bound to force:recordData"/>
<force:recordData aura:id="accountRecordId"
recordId="{!v.recordId}"
fields="Name"
mode="EDIT"
targetRecord="{!v.record}"
targetFields="{!v.simpleRecord}"
targetError="{!v.recordSaveError}"/>
<!-- Display an editing form -->
<div class="Record Details">
<lightning:card iconName="action:edit" title="Edit Account">
<div class="slds-p-horizontal--small">
<lightning:input label="Account Name" value="{!v.accountRecord.Name}"/>
<br/>
<lightning:button label="Save Account" variant="brand" onclick="{!c.handleSaveRecord}" />
</div>
</lightning:card>
</div>
<!-- Display Lightning Data Service errors, if any -->
<aura:if isTrue="{!not(empty(v.recordSaveError))}">
<div class="recordError">
{!v.recordSaveError}
</div>
</aura:if>
</aura:component>
({
handleSaveRecord : function(component, event, helper) {
component.find("accountRecordId").saveRecord($A.getCallback(function(saveResult) {
if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
console.log("Save completed successfully.");
} 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));
var errMsg = "";
for (var i = 0; i < saveResult.error.length; i++) {
errMsg += saveResult.error[i].message + "\n";
}
component.set("v.recordSaveError", errMsg);
} else {
console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
}
}));
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment