salesforce lightning navigate to record

Bynayakdillip

salesforce lightning navigate to record

$A.get(“e.force:navigateToSObject”) help you to navigate the sobject record for more detail please check below example

Apex Controller Class

public with sharing class AccountsController {
@AuraEnabled
public static list getAllAccounts()
{
return [select id,name,phone from account where phone!=null];
}
}

Component

<aura:component implements="flexipage:availableForAllPageTypes" access="global" controller="AccountsController" >
	<aura:attribute name="accounts" type="list"></aura:attribute>
     <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:iteration var="account" items="{!v.accounts}">
        <lightning:card variant="Narrow" title="{!account.Name}">
        <aura:set attribute="actions">
            <lightning:button name="details" label="Details" onclick="{!c.goToRecord}"  />
        </aura:set>
        <aura:set attribute="footer">
            <lightning:badge label="{!account.Name}"/>
        </aura:set>
        <p>
           <a onclick="{!c.goToRecord}">{!account.Name}</a> 
        </p>
        <p>
            {!account.Phone}--{!account.Id}
        </p>
    </lightning:card>
    </aura:iteration>
</aura:component>

Controller JS

({
    doInit : function(component, event, helper) {
        
        var action=component.get("c.getAllAccounts");
        action.setCallback(this,function(response){
            
            var state=response.getState();
            if(state==="SUCCESS")
            {
                component.set("v.accounts",response.getReturnValue());
            }
            
            
            
        });
        $A.enqueueAction(action);
		
	},
    goToRecord:function(component, event, helper)
    {
        
        var sobjectEvent=$A.get("e.force:navigateToSObject");
        sobjectEvent.setParams({
            
            "recordId": component.get("account.Id")

        });
        sobjectEvent.fire();
        
    }
    
})

Made a lightning app using Lightning app builder to run it

About the author

nayakdillip administrator

Leave a Reply