LightningRecordIdExample.cmp
The first line of the component is where we specify the controller class and implement our interfaces.
<aura:component controller="LightningRecordIdExampleController" implements="flexipage:availableForAllPageTypes,force:hasRecordId"></aura:component>
And as you've probably figured out from the snippet above, the interface used to get the record Id is ...
force:hasRecordId
". - It adds an attribute named recordId to your component. This attribute is of type String, and its value is an 18-character Salesforce record ID, for example: 001xx000003DGSWAA4. If you added it yourself, the attribute definition would look like the following markup:
<aura:attribute name="recordId" type="String"></aura:attribute>
Note If your component implements force:hasRecordId, you don’t need to add a recordId attribute to the component yourself. If you do add it, don’t change the access level or type of the attribute or the component will cause a runtime error.
- When your component is invoked in a record context in Lightning Experience or the Salesforce app, the recordId is set to the ID of the record being viewed.
Now that we have our record Id, let's take a look at the Lightning controller class.
LightningRecordIdExample.js
In our controller class, we have a single function call doInit the gets called upon page initialization.
doInit : function(component, event, helper){
This function creates a one-time instance of the getAccount function in the server-side controller.
var action = component.get("c.getAccount");
Next, we set the accountId parameter by getting the recordId attribute value from the component.
action.setParams({
"accountId": component.get("v.recordId")
});
Now that we have defined the action and added the record Id as a parameter, all that's left to do is to queue the action and handle the callback response.
action.setCallback(this, function(response) {
var state = response.getState();
if (component.isValid() && state === "SUCCESS") {
component.set("v.account", response.getReturnValue());
...
That's pretty much it! You can check out the server-side controller class below.
No comments:
Post a Comment