function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDCLLLSFDCLLL 

Hi All,I'm Unable to the Pass Parameter from Lightning component to Apex Class. Below is the code , Could someone let me know what's the mistake?

Contoller :

public class AccountComponent
{
    @AuraEnabled
    
    public static string accsearch(string name,string phone)
    {
        system.debug(name);
        system.debug(phone);
        list<account>accList = new list<account>();
        account a = new account();
        a.name = name;
        a.phone = phone;
        accList.add(a);
        try
        {
            insert accList;
            return 'Successfully inserted';
        }
        catch(exception e)
        {
            system.debug(e);            
            return e.getMessage();
        }        
    }
}

Lightning Component :

<aura:component controller="AccountComponent" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    
    <aura:attribute name="Accountstatus" type="string"/>
    <aura:attribute name="AccountName" type="string"/>
    <aura:attribute name="Phone" type="string"/> 
    <lightning:input type="string" label="Enter Name" value="{!v.AccountName}"/>
    <lightning:input type="string" label="Enter Phone" value="{!v.Phone}"/>
    <lightning:button label="Save" onclick="{!c.accsearch}"/>
    
    {!v.Accountstatus}
    <br/>        
</aura:component>

Controller.js :
({
    accsearch : function(component, event, helper)
    {
        var action = component.get('c.accsearch');
        var name = component.get('v.AccountNam');
        action.setParams({name : name});
        var phone = component.get('v.Phone');
        action.setParams({phone : phone});      
        action.setCallback(this,function(response)
                           {
                               var getrecordstatus = response.getReturnValue();
                               component.set('v.Accountstatus',getrecordstatus);
                               console.log(':',getrecordstatus);
                           },'SUCCESS');
        $A.enqueueAction(action);
    }
})
Alain CabonAlain Cabon

There is a typo:    var name = component.get('v.AccountName');

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_actions_call.htm
 
({
    accsearch : function(component, event, helper)
    {
        var action = component.get('c.accsearch');
        var name = component.get('v.AccountName');
        action.setParams({name : name});
        var phone = component.get('v.Phone');
        action.setParams({phone : phone});      
       
         action.setCallback(this, function(response) {
            var state = response.getState();          
            component.set('v.Accountstatus',state);
            console.log('state:',getrecordstatus);
            
            if (state === "SUCCESS") {
                // Alert the user with the value returned from the server
                alert("From server: " + response.getReturnValue());       
            }
            else if (state === "INCOMPLETE") {
                // do something
                console.log("INCOMPLETE");
            }
            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);      
    }
})
Alain CabonAlain Cabon
Perhaps because the same name is used for the controllers (Apex and Javascript : accsearch ). Not sure.

By the way,  state can be changed.

var state = response.getState();         
 component.set('v.Accountstatus',state);
console.log('state:',state );

 // annotation just before
 @AuraEnabled
 public static string accsearch(string name,string phone)