Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save saicharanreddyk/4e7c5bccee7a36c1ccbae80c350e7385 to your computer and use it in GitHub Desktop.
Save saicharanreddyk/4e7c5bccee7a36c1ccbae80c350e7385 to your computer and use it in GitHub Desktop.
Lightning - Simple use case ( card, apex controller, layout, aura:if )
Component
**************************************************************
<aura:component controller="classWithParameters">
<aura:attribute name="name" type="String" />
<aura:attribute name="phone" type="String" />
<aura:attribute name="industry" type="String" />
<aura:attribute name="type" type="String" />
<aura:attribute name="result" type="String" />
<aura:attribute name="flag" type="Boolean" default="true"/>
<aura:attribute name="flag1" type="Boolean" default="false"/>
<lightning:card iconName="standard:account">
<aura:set attribute="title">
Account <br/>
New Account
</aura:set>
<aura:set attribute="actions">
<lightning:buttonGroup>
<lightning:button label="submit" onclick="{!c.submitData}"/>
<lightning:button label="cancel" onclick="{!c.cancel}"/>
</lightning:buttonGroup>
</aura:set>
<lightning:layout multipleRows="true">
<aura:if isTrue="{!v.flag}">
<lightning:layoutitem size="6">
<lightning:input label="Enter Name" aura:id="one" type="string" required="true" value="{!v.name}"/>
<lightning:input label="Enter Phone" aura:id="two" type="string" required="true" value="{!v.phone}"/>
<lightning:input label="Enter Industry" aura:id="three" type="string" required="true" value="{!v.industry}"/>
<lightning:input label="Enter Type" aura:id="four" type="string" required="true" value="{!v.type}"/>
</lightning:layoutitem>
<aura:set attribute="else">
<lightning:layoutitem size="6">
Id: {!v.result} <br/>
Name : {!v.name}<br/>
Phone: {!v.phone}<br/>
Industry: {!v.industry}<br/>
Type: {!v.type}<br/>
</lightning:layoutitem>
</aura:set>
</aura:if>
</lightning:layout>
</lightning:card>
</aura:component>
**************************************************************
Controller
**************************************************************
({
submitData : function(component, event, helper) {
var action = component.get("c.createAcc");
var a = component.find("one").get("v.value");
var b = component.find("two").get("v.value");
var c = component.find("three").get("v.value");
var d = component.find("four").get("v.value");
action.setParams({
"name": a,
"phone": b,
"industry": c,
"type": d
});
action.setCallback(this,function(response){
var state= response.getState();
if(state==='SUCCESS'){
var result1 = response.getReturnValue();
component.set("v.result", result1);
component.set("v.flag", false);
}else{
}
});
$A.enqueueAction(action);
},
cancel : function(component, event, helper) {
component.set("v.name", null);
component.set("v.phone", null);
component.set("v.industry", null);
component.set("v.type", null);
component.set("v.flag", true);
}
})
**************************************************************
Apex Class
**************************************************************
public class classWithParameters {
@AuraEnabled
public static String createAcc(String name, String phone, String industry, String type){
String result;
try{
Account acc=new Account();
acc.Name=name;
acc.Phone=phone;
acc.Industry=industry;
acc.Type=type;
insert acc;
result = 'Account Created Successfully with Id'+acc.Id;
}catch(Exception e){
result= 'Account Creation failed with error message: '+e.getMessage();
}
return result;
}
}
**************************************************************
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment