Insert Record in Lightning Component Salesfroce

Apex Class :- "StudentController"
// Here, "Student" is a Custom Object, insert details in Student Object.
public class StudentController {
@AuraEnabled
public static Student__c SaveStudentDetails(Student__c stndObj){
insert stndObj;
return stndObj;
}

@AuraEnabled
public static List<String> getPickListValuesIntoList(String objectType, String selectedField){
List<String> pickListValuesList = new List<String>();
Schema.SObjectType convertToObj = Schema.getGlobalDescribe().get(objectType);
Schema.DescribeSObjectResult res = convertToObj.getDescribe();
Schema.DescribeFieldResult fieldResult = res.fields.getMap().get(selectedField).getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for( Schema.PicklistEntry pickListVal : ple){
pickListValuesList.add(pickListVal.getLabel());
}
return pickListValuesList;
}

}
Picklist Component:- "StudentPicklistComponent"
<aura:component controller="StudentController">
<aura:attribute name="sObjectName" type="String" />
<aura:attribute name="fieldName" type="String" />
<aura:attribute name="fieldLabel" type="String"/>
<aura:attribute name="picklistValues" type="String[]" />
<aura:attribute name="picklistvalue" type="String"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<lightning:layout>
<lightning:layoutItem size="12" smallDeviceSize="12" mediumDeviceSize="12" largeDeviceSize="12" flexibility="auto">
<lightning:select label="{!v.fieldLabel}" value="{!v.picklistvalue}">
<option value="" >--Select--</option>
<aura:iteration items="{!v.picklistValues}" var="val">
<option value="{!val}">{!val}</option>
</aura:iteration>
</lightning:select>
</lightning:layoutItem>
</lightning:layout>
</aura:component>
StudentPicklistComponent Controller.JS
({
doInit : function(component, event, helper) {
var action = component.get("c.getPickListValuesIntoList");
action.setParams({
objectType: component.get("v.sObjectName"),
selectedField: component.get("v.fieldName")
});
action.setCallback(this, function(response) {
var state = response.getState();
if(state === 'SUCCESS'){
component.set("v.picklistValues", response.getReturnValue());
}
});
$A.enqueueAction(action);
}
})
Component:- "StudentComponent"
<aura:component Controller="StudentController" implements="force:appHostable,lightning:isUrlAddressable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction,lightning:actionOverride" access="global">
<aura:attribute name="Students" type="Student__c" default="{'sobjectType':'Student__c'}"/>
<aura:attribute name= "StudentPicklIstType" type="List" />

<lightning:layout>
<lightning:layoutItem size="12" smallDeviceSize="12" mediumDeviceSize="12" largeDeviceSize="12">
<h3 class="slds-section__title header_border_radius">
<button class="slds-button slds-section__title-action header_border_radius" aria-expanded="true" aria-controls="expando-unique-id">
<span title="Section Title" class="slds-truncate">
<lightning:icon iconName="standard:location" size="small" alternativeText="Related Contacts" class="slds-m-around_small"/>
Student Information
</span>
</button>
</h3>
</lightning:layoutItem>
</lightning:layout>

<div class="slds-box">
<lightning:layout>
<lightning:layoutItem flexibility="auto" size="6" smallDeviceSize="6" mediumDeviceSize="6" largeDeviceSize="6" padding="horizontal-large">
<lightning:input type="text" value="{!v.Students.Name__c}" label="Student Name" aura:id="stndNameId" required="true"/>
</lightning:layoutItem>
</lightning:layout>


<lightning:layout>
<lightning:layoutItem flexibility="auto" size="6" smallDeviceSize="6" mediumDeviceSize="6" largeDeviceSize="6" padding="horizontal-large">
<lightning:input type="text" value="{!v.Students.Mobile_No__c}" label="Mobile Number" aura:id="stndMbId" required="true"/>
</lightning:layoutItem>
</lightning:layout>


<lightning:layout>
<lightning:layoutItem flexibility="auto" size="6" smallDeviceSize="6" mediumDeviceSize="6" largeDeviceSize="6" padding="horizontal-large">
<lightning:input type="text" value="{!v.Students.Email_Address__c}" label="Email Address" aura:id="StndEmailID" required="true"/>
</lightning:layoutItem>
</lightning:layout>

<lightning:layout>
<lightning:layoutItem flexibility="auto" size="6" smallDeviceSize="6" mediumDeviceSize="6" largeDeviceSize="6" padding="horizontal-large">
<c:StudentPicklistComponent aura:id="MailTypeid"
sObjectName="Student__c"
fieldName="Mail_Type__c"
picklistvalue="{!v.Students.Mail_Type__c}"
fieldLabel="Mail Type"/>
</lightning:layoutItem>
</lightning:layout>
</div>
<aura:if isTrue= "{!(v.Students.Mail_Type__c != 'E-Mail')}">
<lightning:layout>
<lightning:layoutItem size="12" smallDeviceSize="12" mediumDeviceSize="12" largeDeviceSize="12">
<h3 class="slds-section__title header_border_radius">
<button class="slds-button slds-section__title-action header_border_radius" aria-expanded="true" aria-controls="expando-unique-id">
<span title="Section Title" class="slds-truncate">
<lightning:icon iconName="standard:location" size="small"
alternativeText="Related Contacts" class="slds-m-around_small"/>
Student Address Information
</span>
</button>
</h3>
</lightning:layoutItem>
</lightning:layout>
<div class="slds-box">
<lightning:layout>

<lightning:layoutItem flexibility="auto" size="6" smallDeviceSize="6" mediumDeviceSize="6" largeDeviceSize="6" padding="horizontal-large">
<lightning:input type="text" value="{!v.Students.Mailing_Street__c}" label="Mailing Street" aura:id="ConsumerId" />
</lightning:layoutItem>
</lightning:layout>


<lightning:layout>
<lightning:layoutItem flexibility="auto" size="6" smallDeviceSize="6" mediumDeviceSize="6" largeDeviceSize="6" padding="horizontal-large">
<lightning:input type="text" value="{!v.Students.Mailing_City__c}" label="Mailing City" aura:id="MailingCityId" />
</lightning:layoutItem>
</lightning:layout>


<lightning:layout>
<lightning:layoutItem flexibility="auto" size="6" smallDeviceSize="6" mediumDeviceSize="6" largeDeviceSize="6" padding="horizontal-large">
<lightning:input type="text" value="{!v.Students.MailingStateProvince__c}" label="Mailing State/Province" aura:id="MailingStateid" />
</lightning:layoutItem>
</lightning:layout>


<lightning:layout>
<lightning:layoutItem flexibility="auto" size="6" smallDeviceSize="6" mediumDeviceSize="6" largeDeviceSize="6" padding="horizontal-large">
<lightning:input type="text" value="{!v.Students.Mailing_Zip_Postal_Code__c}" label="Mailing Zip/Postal Code" aura:id="MailingZipid" />
</lightning:layoutItem>
</lightning:layout>

</div>

</aura:if>

<lightning:layout>
<lightning:layoutItem size="12" smallDeviceSize="12" mediumDeviceSize="12" largeDeviceSize="12">
<h3 class="slds-section__title header_border_radius">
<button class="slds-button slds-section__title-action header_border_radius" aria-expanded="true" aria-controls="expando-unique-id">
<span title="Section Title" class="slds-truncate">
<lightning:icon iconName="standard:location" size="small"
alternativeText="Related Contacts" class="slds-m-around_small"/>
Student Description
</span>
</button>
</h3>
</lightning:layoutItem>
</lightning:layout>
<div class="slds-box">
<lightning:layout>
<lightning:layoutItem flexibility="auto" size="6" smallDeviceSize="6" mediumDeviceSize="6" largeDeviceSize="6" padding="horizontal-large">
<lightning:input type="text" value="{!v.Students.Description__c}" label="Description" aura:id="DescriptionId" />
</lightning:layoutItem>
</lightning:layout>
<br/>
<div>
<lightning:button aura:id="btnSubmit" variant="brand" label="Submit" onclick="{!c.studentDetails }"/>
</div>
</div>


</aura:component>

Controller js:- “StudentComponent.JS”

({
studentDetails : function(component, event ,helper){
debugger;
var stnds = component.get("v.Students");
var action = component.get("c.SaveStudentDetails");
action.setParams({
stndObj: stnds
});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
//component.set("v.Students", response.getReturnValue());
alert("The record has been Save successfully.");
window.location.reload();
}
});
$A.enqueueAction(action)
}
})

Application:- “StudentApp”

<aura:application extends="force:slds">
<c:StudentComponent></c:StudentComponent>
</aura:application>
ouput:-

Leave a Reply