ApexClass:
public class AccountContactInsert {
@AuraEnabled
public static String insertrecords(Account acc, Contact con){
SavePoint sp = Database.setSavePoint();
try{
insert acc;
con.AccountID = acc.id;
insert con;
}
catch(Exception e){
Database.rollback(sp);
}
return null;
}
}
Lightning Component:
<aura:component controller="AccountContactInsert"
implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,lightning:availableForFlowScreens,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name='acc' type='Account' default="{SobjectType :'Account'}"/>
<aura:attribute name='con' type='Contact' default="{SobjectType :'Contact'}"/>
<div class="slds-align_absolute-center slds-box" style="width:500px;height:750px">
<div class="demo-only demo-only--sizing slds-grid slds-wrap">
<div class="slds-size_2-of-2">
<div class="slds-box_x-small slds-text-align_center slds-m-around_x-small">
<lightning:input label='Account Name' value='{!v.acc.Name}' aura:id="AccNameId" /></div>
</div>
<div class="slds-size_1-of-2">
<div class="slds-box_x-small slds-text-align_center slds-m-around_x-small">
<lightning:input label='FirstName' value='{!v.con.FirstName}' aura:id="ConFirstId"/></div>
</div>
<div class="slds-size_1-of-2">
<div class="slds-box_x-small slds-text-align_center slds-m-around_x-small">
<lightning:input label='LastName' value='{!v.con.LastName}' aura:id="ConLastId"/></div>
</div>
<div class="slds-size_2-of-2">
<div class="slds-box_x-small slds-text-align_center slds-m-around_x-small">
<lightning:input label='Mailing Address' value='{!v.acc.Mailing_Address__c}' aura:id="AccMailingId"/>
</div>
</div><div class="slds-size_2-of-2">
<div class="slds-box_x-small slds-text-align_center slds-m-around_x-small">
<lightning:input label='Phone' value='{!v.con.Phone}' type="phone" aura:id="ConPhoneId"/>
</div>
</div><div class="slds-size_2-of-2">
<div class="slds-box_x-small slds-text-align_center slds-m-around_x-small">
<lightning:input aura:id="txtEmail" label="Email" value='{!v.con.Email}' />
</div>
</div><div class="slds-size_2-of-2">
<div class="slds-box_x-small slds-text-align_center slds-m-around_x-small">
<lightning:input label='Home Phone' value='{!v.con.OtherPhone}' type="phone" pattern="[0-9]{10}"/>
</div>
</div>
<div class="slds-size_2-of-2">
<div class="slds-box_x-small slds-text-align_center slds-m-around_x-small">
<lightning:button label='submit' variant="success" onclick='{!c.submitFunction}' />
</div>
</div>
</div>
</div>
</aura:component>
JS Controller:
({
submitFunction : function(component, event, helper) {
var accRecord = component.get('v.acc');
if(accRecord.Name == undefined){
var inputCmp = component.find("AccNameId");
inputCmp.setCustomValidity('Please Enter Account Name');
inputCmp.reportValidity();
//inputCmp.checkValidity();
}
var conRecord = component.get('v.con');
if(conRecord.FirstName == undefined){
var inputCmp = component.find("ConFirstId");
inputCmp.setCustomValidity('Please Enter Contact First Name');
inputCmp.reportValidity();
}
if(conRecord.Phone == undefined){
var inputCmp = component.find("ConPhoneId");
inputCmp.setCustomValidity('Please Enter Contact Phone Number');
inputCmp.reportValidity();
}
if(conRecord.LastName == undefined){
var inputCmp = component.find("ConLastId");
inputCmp.setCustomValidity('Please Enter Contact Last Name');
inputCmp.reportValidity();
}
if(accRecord.Mailing_Address__c == undefined){
var inputCmp = component.find("AccMailingId");
inputCmp.setCustomValidity('Please Enter Account Mailing Address');
inputCmp.reportValidity();
}
if(conRecord.Email == undefined){
var inputCmp = component.find("txtEmail");
inputCmp.setCustomValidity('Please Enter Contact Email Address');
inputCmp.reportValidity();
}
var emailField = component.find("txtEmail");
var emailFieldValue = emailField.get("v.value");
var regExpEmailformat = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if(!emailFieldValue.match(regExpEmailformat)) {
emailField.setCustomValidity("Enter valid Email");
emailField.reportValidity();
}
var phoneCmp = component.find('ConPhoneId');
var phoneCmpValue = phoneCmp.get("v.value");
var phoneRegexFormat = /^\d{10}$/;
if(!phoneCmpValue.match(phoneRegexFormat)) {
phoneCmp.setCustomValidity("Enter valid phone number");
phoneCmp.reportValidity();
}
var action = component.get('c.insertrecords');
action.setParams({'acc':accRecord, 'con':conRecord});
action.setCallback(this, function(response){
var state = response.getState();
if (state === "SUCCESS") {
/* var toastEvent = $A.get("e.force:showToast");//toast message does not displaying in application view
toastEvent.setParams({
"title": "Success!",
"duration" : 5000,
"type" : 'success',
"message": "The record has been created successfully."
});
toastEvent.fire();*/
// refresh/reload the page view
$A.get('e.force:refreshView').fire();
var r = response.getReturnValue();
component.set('v.acc',r);
component.set('v.con',r);
}
});
$A.enqueueAction(action);
}
})