Created
February 6, 2020 12:50
-
-
Save saicharanreddyk/7abfdd7a86ade50cd10bd2b0d8ed9886 to your computer and use it in GitHub Desktop.
Lightning - How to pass sobject as a paratemer to the apex class?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
How to pass sobject as a paratemer to the apex class? | |
In this below example we are trying to insert the records in apex example by taking the sObject as parameter to Apex class. | |
While initializing the attribute as below we have to initilize the Account by default="{sObjectType:'Account'} | |
<aura:attribute name="acc" type="Account" default="{sObjectType:'Account'}" /> | |
>> We are reading the input values from component and from the component | |
>> In helper we are getting the details ( Account acc details) and then we are passing the values as param | |
action.setParams({"acc":account}); | |
in the callback we are getting success after the account is created. | |
Apex Class | |
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
public class Example_Five { | |
@AuraEnabled | |
public static String callAccount(Account acc){ | |
String result; | |
try{ | |
insert acc; | |
result='Success'; | |
}catch(Exception e){ | |
result=e.getMessage(); | |
} | |
return result; | |
} | |
} | |
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
Component | |
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
<aura:component controller="Example_Five"> | |
<aura:attribute name="acc" type="Account" default="{sObjectType:'Account'}"/> | |
<lightning:card title="Account"> | |
<lightning:input label="Account Name" value="{!v.acc.Name}" /> | |
<lightning:input label="Account Phone" value="{!v.acc.Phone}" /> | |
<lightning:input label="Account Industry" value="{!v.acc.Industry}" /> | |
<lightning:input label="Account Rating" value="{!v.acc.Rating}" /> | |
<aura:set attribute="footer"> | |
<lightning:button label="submit" onclick="{!c.callMe}" /> | |
</aura:set> | |
</lightning:card> | |
</aura:component> | |
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
Controller | |
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
({ | |
callMe : function(component, event, helper) { | |
helper.callHelper(component); | |
} | |
}) | |
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
helper | |
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
({ | |
callHelper : function(component) { | |
var account =component.("v.acc"); | |
var action=component.get("c.callAccount"); | |
action.setParams({"acc":account}); | |
action.setCallback(this,function(response){ | |
var state=response.getState(); | |
if(state==='SUCCESS'){ | |
var result=response.getReturnValue(); | |
console.log(result); | |
} | |
}); | |
$A.enqueueAction(action); | |
} | |
}) | |
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment