You need to sign in to do that
Don't have an account?

How to create records when form is isaved
Hello Guys,
Can you help to achieve my first lightning component ?
I want to insert 2 records (of 2 different objects Obj
1 an Obj2) whenever the user saves a lightning form
I have juste finished the markup(fields are from both objects ) and i want to implement logic (js controller and apex controller) so that when i save the form , records are created with populated fields.
Can you help to achieve my first lightning component ?
I want to insert 2 records (of 2 different objects Obj
<div class="container-fluid"> <h3>Please Enter The Candidate Information</h3> <div class="form-group"> <label>First Name</label> <ui:inputText class="form-control" value="{!v.Obj1.First_Name__c}"/> </div> <div class="form-group"> <label>Last Name</label> <ui:inputText class="form-control" value="{!v.Obj1.Last_Name__c}"/> </div> <div class="form-group"> <label>Email Address</label> <ui:inputText class="form-control" value="{!v.Obj2.Email__c}"/> </div> <div class="form-group"> <label>SSN</label> <ui:inputText class="form-control" value="{!v.Obj2.SSN__c}"/> </div> </div> <div class="col-md-4 text-center"> <ui:button class="btn btn-default" press="{!c.create}">Create</ui:button> </div>
1 an Obj2) whenever the user saves a lightning form
I have juste finished the markup(fields are from both objects ) and i want to implement logic (js controller and apex controller) so that when i save the form , records are created with populated fields.
Please use the code for your refence and am considering Obj1, Obj2 as custom objects in below
Apex Class:
public with sharing class CreateRecordSample {
@AuraEnabled
public static void createRecord (Obj1__c Obj1,Obj2__2 Obj2){
try{
if(Obj1 != null){
insert Obj1;
}
if(Obj2 != null){
insert Obj2;
}
} catch (Exception ex){
}
}
}
Then add the above class Name in the Aura Component Controller value like this<aura:component implements="" controller="CreateRecordSample ">
Use the below JS Controller.
({
create : function(component, event, helper) {
var action = component.get("c.createRecord");
//Setting the Apex Parameter
action.setParams({
obj1 : component.get("v.obj1"),
obj2 : component.get("v.obj2")
});
//Setting the Callback
action.setCallback(this,function(a){
//get the response state
var state = a.getState();
//check if result is successfull
if(state == "SUCCESS"){
alert('Record is Created Successfully');
} else if(state == "ERROR"){
alert('Error in calling server side action');
}
});
//adds the server-side action to the queue
$A.enqueueAction(action);
}
})
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Maharajan.C
As per your requirement, here is a lightning form which saves the data into the lead object on click of Create button:
But if you save the record in another object we need a specific condition on which it saves into it.
//---------Aura_Component-------------//
<aura:component access="global" controller="AuraCls" implements="force:lightningQuickAction">
<aura:attribute name="objLead" type="sObject" default="{'sObjectType':'Lead'}" />
<div>
<lightning:layout multipleRows="true">
<lightning:layoutItem padding="around-small" flexibility="auto" size="12">
<lightning:layout>
<lightning:layoutItem padding="around-small" flexibility="auto" size="3">
</lightning:layoutItem>
<lightning:layoutItem padding="around-small" flexibility="auto" size="6">
<div class="page-section page-main detailForm">
<h1 style="font-weight: bold;text-align: center;font-size: 2em;">Lightning Form</h1><br/>
<div>
<lightning:layout multipleRows="true">
<lightning:layoutItem flexibility="auto" size="5">
<lightning:input label="FirstName" aura:id="fNameId" messageWhenValueMissing="This is a required question" value="{!v.objLead.FirstName}" required="true" placeholder="Your answer" />
</lightning:layoutItem>
<lightning:layoutItem flexibility="auto" size="2">
</lightning:layoutItem>
<lightning:layoutItem flexibility="auto" size="5">
<lightning:input label="LastName" aura:id="lNameId" messageWhenValueMissing="This is a required question" value="{!v.objLead.LastName}" required="true" placeholder="Your answer" />
</lightning:layoutItem>
</lightning:layout><br/>
<lightning:input type="email" value="{!v.objLead.Email}" aura:id="emailId" messageWhenValueMissing="This is a required question" placeholder="Your email" label="Email address" required="true" />
<br/>
<lightning:input class="inputCss" label="Mobile No." aura:id="mobId" messageWhenValueMissing="This is a required question" value="{!v.objLead.MobilePhone}" required="true" placeholder="Your answer" />
<br/>
<lightning:button variant="brand" label="Create" title="Brand action" onclick="{!c.SaveRecord}" />
</div>
</div>
</lightning:layoutItem>
<lightning:layoutItem class="mobRespSide" flexibility="auto" padding="around-small" size="3">
</lightning:layoutItem>
</lightning:layout>
</lightning:layoutItem>
<lightning:layoutItem flexibility="auto" padding="around-small" size="12">
</lightning:layoutItem>
</lightning:layout>
</div>
</aura:component>
//---------Aura_Controller-------------//
({
SaveRecord: function(component, event, helper) {
var fName = component.get("v.objLead.FirstName");
console.log('fName--------->>>>>' + fName);
var lName = component.get("v.objLead.LastName");
console.log('fName--------->>>>>' + lName);
var email = component.get("v.objLead.Email");
console.log('email--------->>>>>' + email);
var mobPhone = component.get("v.objLead.MobilePhone");
console.log('mobPhone--------->>>>>' + mobPhone);
//validate the set of fields in the form
if (fName != null && fName != '' && lName != null && lName != '' && email != null && email != '' && mobPhone != null &&
mobPhone != '' ) {
helper.saveLeadList_helper(component, event, helper); // Helper Calling----
} else {
if (fName == null || fName == undefined) {
component.find('fNameId').showHelpMessageIfInvalid();
}
if (lName == null || lName == undefined) {
component.find('lNameId').showHelpMessageIfInvalid();
}
if (email == null || email == undefined) {
component.find('emailId').showHelpMessageIfInvalid();
}
if (mobPhone == null || mobPhone == undefined) {
component.find('mobId').showHelpMessageIfInvalid();
}
}
}
})
//---------Aura_Helper-------------//
({
saveLeadList_helper: function(component, event, helper) {
var sObjRec = component.get("v.objLead");
try {
console.log('sObjRec--------->>>>>' + sObjRec);
var action = component.get("c.saveLeadRec");
action.setParams({
"leadRec": sObjRec
});
action.setCallback(this, function(r) {
if (r.getState() === 'SUCCESS') {
var storedResponse = r.getReturnValue();
console.log('storedResponse----->>' + JSON.stringify(storedResponse.Id));
var sObjId = storedResponse.Id;
if (storedResponse != null) {
alert('The form is successfully submitted');
}
} else {
console.log('ERROR');
console.log(r.getError());
}
});
$A.enqueueAction(action);
} catch (ex) {
console.log("Exception~");
console.log(ex);
}
}
})
//---------Aura_Class-------------//
public class AuraCls {
@AuraEnabled
public static Lead saveLeadRec(Lead leadRec) {
try{
System.debug('leadRecord---->>>>>'+leadRec);
if(leadRec!= null){
leadRec.Status = 'Open - Not Contacted';
leadRec.Company = 'CA';
insert leadRec; //Insert the Lead object record
System.debug('---Lead-----Insertion successful-------');
}
else{
System.debug('---Lead-----Insertion Failed-------');
}
return leadRec;
}catch(Exception ex){
System.debug('Error at line number-'+ex.getLineNumber()+' & '+'Error message is-'+ex.getMessage());
return null;
}
}
}
I hope this solution will help you. Please mark it as best answer if it helps.
Thanks.
Deepali Kulshrestha
I have implemented your solution, but records are not created :
Apex Controller :
The button to save the form
Can you tell what's wrong with this code ? I need to insert 3 records for Account, Contact and MBT__c objects