Newer Version Available
Configure Components for Record-Specific Actions
force:hasRecordId is a marker interface. A marker interface is a signal to the component’s container to add the interface’s behavior to the component.
The recordId attribute is set only when you place or invoke the component in an explicit record context. For example, when you place the component directly on a record page layout, or invoke it as an object-specific action from a record page or object home. In all other cases, such as when you invoke the component as a global action, or create the component programmatically inside another component, recordId isn’t set, and your component shouldn’t depend on it.
Example of a Component for a Record-Specific Action
This extended example shows a component designed to be invoked as a custom
object-specific action from the detail page of an account record. After creating the
component, you need to create the custom action on the account object, and then add
the action to an account page layout. When opened using an action, the component
appears in an action panel that looks like this:
The component definition begins by implementing both the force:lightningQuickActionWithoutHeader and the force:hasRecordId interfaces. The first makes it available for use as an action and prevents the standard controls from displaying. The second adds the interface’s automatic record ID attribute and value assignment behavior, when the component is invoked in a record context.
1<aura:component controller="QuickContactController"
2 implements="force:lightningQuickActionWithoutHeader,force:hasRecordId">
3
4 <aura:attribute name="account" type="Account" />
5 <aura:attribute name="newContact" type="Contact"
6 default="{ 'sobjectType': 'Contact' }" /> <!-- default to empty record -->
7
8 <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
9
10 <!-- Display a header with details about the account -->
11 <div class="slds-page-header" role="banner">
12 <p class="slds-text-heading_label">{!v.account.Name}</p>
13 <h1 class="slds-page-header__title slds-m-right_small
14 slds-truncate slds-align-left">Create New Contact</h1>
15 </div>
16
17 <!-- Display the new contact form -->
18 <lightning:input aura:id="contactField" name="firstName" label="First Name"
19 value="{!v.newContact.FirstName}" required="true"/>
20
21 <lightning:input aura:id="contactField" name="lastname" label="Last Name"
22 value="{!v.newContact.LastName}" required="true"/>
23
24 <lightning:input aura:id="contactField" name="title" label="Title"
25 value="{!v.newContact.Title}" />
26
27 <lightning:input aura:id="contactField" type="phone" name="phone" label="Phone Number"
28 pattern="^(1?(-?\d{3})-?)?(\d{3})(-?\d{4})$"
29 messageWhenPatternMismatch="The phone number must contain 7, 10, or 11 digits. Hyphens are optional."
30 value="{!v.newContact.Phone}" required="true"/>
31
32 <lightning:input aura:id="contactField" type="email" name="email" label="Email"
33 value="{!v.newContact.Email}" />
34
35 <lightning:button label="Cancel" onclick="{!c.handleCancel}" class="slds-m-top_medium" />
36 <lightning:button label="Save Contact" onclick="{!c.handleSaveContact}"
37 variant="brand" class="slds-m-top_medium"/>
38
39</aura:component>- account—holds the full account record, after it’s loaded in the init handler
- newContact—an empty contact, used to capture the form field values
The component’s controller has all of the interesting code, in three action handlers.
1({
2 doInit : function(component, event, helper) {
3
4 // Prepare the action to load account record
5 var action = component.get("c.getAccount");
6 action.setParams({"accountId": component.get("v.recordId")});
7
8 // Configure response handler
9 action.setCallback(this, function(response) {
10 var state = response.getState();
11 if(state === "SUCCESS") {
12 component.set("v.account", response.getReturnValue());
13 } else {
14 console.log('Problem getting account, response state: ' + state);
15 }
16 });
17 $A.enqueueAction(action);
18 },
19
20 handleSaveContact: function(component, event, helper) {
21 if(helper.validateContactForm(component)) {
22
23 // Prepare the action to create the new contact
24 var saveContactAction = component.get("c.saveContactWithAccount");
25 saveContactAction.setParams({
26 "contact": component.get("v.newContact"),
27 "accountId": component.get("v.recordId")
28 });
29
30 // Configure the response handler for the action
31 saveContactAction.setCallback(this, function(response) {
32 var state = response.getState();
33 if(state === "SUCCESS") {
34
35 // Prepare a toast UI message
36 var resultsToast = $A.get("e.force:showToast");
37 resultsToast.setParams({
38 "title": "Contact Saved",
39 "message": "The new contact was created."
40 });
41
42 // Update the UI: close panel, show toast, refresh account page
43 $A.get("e.force:closeQuickAction").fire();
44 resultsToast.fire();
45 $A.get("e.force:refreshView").fire();
46 }
47 else if (state === "ERROR") {
48 console.log('Problem saving contact, response state: ' + state);
49 }
50 else {
51 console.log('Unknown problem, response state: ' + state);
52 }
53 });
54
55 // Send the request to create the new contact
56 $A.enqueueAction(saveContactAction);
57 }
58
59 },
60
61 handleCancel: function(component, event, helper) {
62 $A.get("e.force:closeQuickAction").fire();
63 }
64})The first action handler, doInit, is an init handler. Its job is to use the record ID that’s provided via the force:hasRecordId interface and load the full account record. Note that there’s nothing to stop this component from being used in an action on another object, like a lead, opportunity, or custom object. In that case, doInit will fail to load a record, but the form will still display.
- Prepares the server action to save the new contact.
- Defines a callback function, called the response handler, for when the server completes the action. The response handler is discussed in a moment.
- Enqueues the server action.
- Closes the action panel by firing the force:closeQuickAction event.
- Displays a “toast” message that the contact was created by firing the force:showToast event.
- Updates the record page by firing the force:refreshView event, which tells the record page to update itself.
The handleCancel action handler closes the action panel by firing the force:closeQuickAction event.
The component helper provided here is minimal, sufficient to illustrate its use. You’ll likely have more work to do in any production quality form validation code.
1({
2 validateContactForm: function(component) {
3 var validContact = true;
4
5
6 // Show error messages if required fields are blank
7 var allValid = component.find('contactField').reduce(function (validFields, inputCmp) {
8 inputCmp.showHelpMessageIfInvalid();
9 return validFields && inputCmp.get('v.validity').valid;
10 }, true);
11
12 if (allValid) {
13 // Verify we have an account to attach it to
14 var account = component.get("v.account");
15 if($A.util.isEmpty(account)) {
16 validContact = false;
17 console.log("Quick action context doesn't have a valid account.");
18 }
19
20 return(validContact);
21 }
22 }
23})Finally, the Apex class used as the server-side controller for this component is deliberately simple to the point of being obvious.
1public with sharing class QuickContactController {
2
3 @AuraEnabled
4 public static Account getAccount(Id accountId) {
5 // Perform isAccessible() checks here
6 return [SELECT Name, BillingCity, BillingState FROM Account WHERE Id = :accountId];
7 }
8
9 @AuraEnabled
10 public static Contact saveContactWithAccount(Contact contact, Id accountId) {
11 // Perform isAccessible() and isUpdateable() checks here
12 contact.AccountId = accountId;
13 upsert contact;
14 return contact;
15 }
16
17}