Skip to content

Instantly share code, notes, and snippets.

@afawcett
Created May 30, 2017 02:15
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save afawcett/d510e2d7a25daaf8b8ea9e96bbd3b4e0 to your computer and use it in GitHub Desktop.
Save afawcett/d510e2d7a25daaf8b8ea9e96bbd3b4e0 to your computer and use it in GitHub Desktop.
Lightning Component for Lightning Edit Action Override
<aura:component implements="lightning:actionOverride,force:hasRecordId,force:hasSObjectName">
<aura:attribute name="record" type="Object" />
<aura:attribute name="componentRecord" type="Object" />
<aura:attribute name="recordError" type="String" />
<force:recordData
aura:id="recordLoader"
recordId="{!v.recordId}"
layoutType="FULL"
mode="EDIT"
targetRecord="{!v.record}"
targetFields="{!v.componentRecord}"
targetError="{!v.recordError}" />
<div class="slds-page-header">
<div class="slds-media">
<div class="slds-media__figure">
<lightning:icon iconName="custom:custom67" />
</div>
<div class="slds-media__body">
<p class="slds-text-body_small slds-line-height_reset">WIDGET</p>
<h1 class="slds-page-header__title slds-truncate slds-align-middle"
title="{!v.componentRecord.Name}">{!v.componentRecord.Name}</h1>
</div>
<lightning:buttonGroup>
<lightning:button label="Save" onclick="{!c.onSave}" />
<lightning:button label="Cancel" onclick="{!c.onCancel}" />
</lightning:buttonGroup>
</div>
</div>
<aura:if isTrue="{!not(empty(v.recordError))}">
<ui:message title="Error" severity="error">
{!v.recordError}
</ui:message>
</aura:if>
<lightning:layout>
<lightning:layoutitem padding="around-small">
<lightning:input
name="name"
label="Name"
type="text"
value="{!v.componentRecord.Name}"/>
</lightning:layoutitem>
<lightning:layoutitem padding="around-small">
<lightning:input
name="description"
label="Description"
type="text"
value="{!v.componentRecord.Description__c}"/>
</lightning:layoutitem>
<lightning:layoutitem padding="around-small">
<lightning:input
name="color"
label="Color"
type="color"
value="{!v.componentRecord.Color__c}"/>
</lightning:layoutitem>
</lightning:layout>
</aura:component>
({
onSave : function(component, event, helper) {
// Ask Lightning Data Service to save the record
component.find("recordLoader").saveRecord($A.getCallback(function(saveResult) {
if (saveResult.state === "SUCCESS") {
// Display popup confirmation to the user
var resultsToast = $A.get("e.force:showToast");
resultsToast.setParams({
"title": "Saved",
"message": "The record was updated."});
resultsToast.fire();
// Navigate back to the record view
var navigateEvent = $A.get("e.force:navigateToSObject");
navigateEvent.setParams({ "recordId": component.get('v.recordId') });
navigateEvent.fire();
}
else {
// Basic error handling
component.set('v.recordError',
'Error: ' + saveResult.state + ', message: ' + JSON.stringify(saveResult.error));
}
}));
},
onCancel : function(component, event, helper) {
// Navigate back to the record view
var navigateEvent = $A.get("e.force:navigateToSObject");
navigateEvent.setParams({ "recordId": component.get('v.recordId') });
navigateEvent.fire();
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment