You need to sign in to do that
Don't have an account?
SFDCLLL
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);
}
})
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);
}
})
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
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)