+ Start a Discussion
Aruna VasahanAruna Vasahan 

Lightning Component: Passing parameter to Server side controller

Hi guys,
          I'm now working with a community based on Napili template. Here my task is to fetch the record id from the record page and to display the details of the record in a separate page. I've wrote some coding for it but Since I'm new to lightning concept I'm not able to achieve this. I have given my code below.
<aura:component controller="ParamController" implements="force:lightningQuickAction,force:hasRecordId">
 <aura:attribute name="recordId" type="String" default="___"/>
    Record Id is {!v.recordId}
<ui:button label="Call server" press="{!c.echo}"/>
 </aura:component>
({
    echo : function(cmp,event,helper){
        
      
        var recordId = cmp.getParam("v.recordId");
     //   console.log("Current Record Id: " + attributeValue);

      //  var target = event.getSource();
        //cmp.set("v.recordId", target.get("v.recordId"));
     

        var action = cmp.get("c.serverEcho");
      
        action.setParams({ recordId : cmp.get("v.recordId") });

        action.setCallback(this, function(response) {
            var state = response.getState();
            
            if (state === "SUCCESS") {
              
                alert("From server: " + response.getReturnValue());

           
            }
            //else if (cmp.isValid() && state === "INCOMPLETE") {
            else if (state === "INCOMPLETE") {
                // do something
            }
            //else if (cmp.isValid() && state === "ERROR") {
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                 errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }
        });

        $A.enqueueAction(action);
    }
})
public class ParamController {
     @AuraEnabled
 public static Account serverEcho(Id recordId) {

      
  Account acct=[SELECT Type,Industry,Rating FROM Account WHERE Account.Id=:RecordId];
  return acct;
  
    }

}

I want to pass the fetched record Id to the apex class and to display the record dtails using SOQL query.Please help me in accompolishing my task.
Thanks and Regards
Aruna

 
SonamSonam (Salesforce Developers) 
Hi Aruna,

I've gone through the code and looks like you have not called the echo function

COMPONENT: <aura:component controller="ParamController" implements="force:lightningQuickAction,force:hasRecordId”> <AURA:HANDLER NAME="INIT" VALUE="{!THIS}" ACTION="{!C.DOINIT}" /> <aura:attribute name="recordId" type="String" default="___"/> Record Id is {!v.recordId} <ui:button label="Call server" press="{!c.echo}"/> </aura:component>


COMPONENT CONTROLLER:

({ doInit : function(cmp,event,helper){ var recordId = cmp.getParam("v.recordId"); // console.log("Current Record Id: " + attributeValue); // var target = event.getSource(); //cmp.set("v.recordId", target.get("v.recordId")); var action = cmp.get("c.serverEcho"); action.setParams({ recordId : cmp.get("v.recordId") }); action.setCallback(this, function(response) { var state = response.getState(); if (state === "SUCCESS") { alert("From server: " + response.getReturnValue()); } //else if (cmp.isValid() && state === "INCOMPLETE") { else if (state === "INCOMPLETE") { // do something } //else if (cmp.isValid() && state === "ERROR") { else if (state === "ERROR") { var errors = response.getError(); if (errors) { if (errors[0] && errors[0].message) { console.log("Error message: " + errors[0].message); } } else { console.log("Unknown error"); } } }); $A.enqueueAction(action); } })