function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sid LightningSid Lightning 

Save Button in Lightning Not Working

Hi,

 want to create a save button on lightning component for my object Shipping Address which is child to parent Account.

There are 6 fields in shipping address, I want those fields to get saved and displayed on the lightning component 

I am tricked about how do i save it into database and get it displayed on my screen.

Here is my code

Apex:
1public class InsertAndDisplayC {
2     
3    @AuraEnabled
4    public static Id saveDetails(Contact con){
5        // DML operation to save Contact Details  
6        INSERT con;
7        return con.Id;
8    }
9}
Component:
01<aura:component controller="InsertAndDisplayC"
02                implements="lightning:isUrlAddressable,force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
03     
04    <aura:attribute name="PageHeading" type="String" default="Create and Display Contacts"/>
05    <aura:attribute name="RegForm" type="Contact" default="{'sobjectType' : 'Contact'}"/>
06    <aura:attribute name="showDetails" type="boolean" default="false" />
07     
08    <div class="slds-m-top--xx-large">
09        <div class="slds-page-header">
10            <div class="slds-align--absolute-center">
11                <div class="slds-text-heading--large">      
12                    {!v.PageHeading}
13                </div>
14            </div>
15        </div>
16    </div>
17    <br/> <br/>
18     
19    <div class = "slds-size--3-of-8">
20        <lightning:input label="Enter First Name" name="fname" value="{!v.RegForm.FirstName}"/>
21        <br/>
22        <lightning:input label="Enter Last Name" name="lname" value="{!v.RegForm.LastName}"/>
23        <br/>
24        <lightning:input label="Enter Phone" name="phone" value="{!v.RegForm.Phone}"/>
25        <br/>
26        <lightning:input label="Enter Email" name="email" value="{!v.RegForm.Email}"/>
27        <br/>
28        <lightning:button label="Submit" onclick="{!c.doSubmit}"/>
29    </div>
30    <br/>
31    <aura:if isTrue="{!v.showDetails}">
32        <div class = "slds-size--3-of-8">
33            <lightning:recordViewForm recordId="{!v.recordId}" objectApiName="Contact">
34                <div class="slds-box">
35                    <lightning:outputField fieldName="Name" />
36                    <lightning:outputField fieldName="Phone" />
37                    <lightning:outputField fieldName="Email" />
38                </div>
39            </lightning:recordViewForm>
40        </div>
41    </aura:if>
42</aura:component>
Controller:
01({
02    doSubmit : function(component, event, helper) {
03        var regForm = component.get("v.RegForm");
04        var action = component.get("c.saveDetails");
05        action.setParams({con  : regForm});
06        action.setCallback(this, function(response) {
07            var state = response.getState();         
08            if (state === "SUCCESS") {
09                var res = response.getReturnValue();
10                component.set('v.recordId',res);
11                component.set("v.showDetails", true);
12                alert('Successfully Saved' + res);
13                component.set('v.RegForm','');
14            }
15            else if (state === "ERROR") {
16                var errors = response.getError();
17                if (errors) {
18                    if (errors[0] && errors[0].message) {
19                        console.log("Error message: " +
20                                    errors[0].message);
21                    }
22                }
23                else {
24                    console.log(response.getReturnValue());
25                }
26            }
27        });
28        $A.enqueueAction(action);
29    },
30})
CSS:
01.THIS {
02 
03}
04 
05.THIS.slds-size--3-of-8 {
06     
07    margin-left: 430px;
08}
09 
10.THIS label.slds-form-element__label{
11     
12    font-size: 1.00rem;
13    color: blue;
14}
Application:
view sourceprint?
1<aura:application extends="force:slds">
2    <c:InsertAndDisplay/>
3</aura:application>
Rounak SharmaRounak Sharma
hi sid,
Please compare and change your code accordingly
Component
<aura:component implements="flexipage:availableForRecordHome,force:appHostable,lightning:actionOverride,force:hasRecordId">
    <aura:attribute name="Speaker" type="Speaker__c" />
    <aura:attribute name="recordId" type="String" />
    <div class="slds-p-bottom-large slds-left_large" style="width:600px">
        <lightning:recordEditForm aura:id="recordViewForm"
                                  recordID="{!v.recordId}"
                                  recordTypeID="{!v.speaker}"
                                  objectApiName="Speaker__c"
                                  onsuccess="{!c.onSuccess}">
            <lightning:messages/>
            <lightning:inputField fieldName="First_Name__c" />
            <lightning:inputField fieldName="Last_Name__c" />
            <lightning:inputField fieldName="Email__c" />
            <lightning:inputField fieldName="Bio__c" />
            <lightning:button aura:id="submit" type="submit" label="Update Speaker" class="slds-m-top_medium"/>        
        </lightning:recordEditForm>
        
    </div>
    
</aura:component>
Controller:
({
    onSuccess : function(component, event, helper) {
        var resultToast = $A.get("e.force:showToast");
        resultToast.setParams({
            "title": "Success!",
            "message": "Record Saved Successfully"
        });
        resultToast.fire();
        var homeEvent = $A.get("e.force:navigateToObjectHome");
        homeEvent.setParams({
            "scope": "Speaker__c"
        });
        homeEvent.fire();
    }
})
Sid LightningSid Lightning
Hi Rounak,

I wants the saved field to be displayed on same page. Does yur code cover date. 

Also, where is the Apex controller call in your code
Rounak SharmaRounak Sharma
hi sid,
Please check the below url, you will get your answer 
https://rajvakati.com/2018/05/29/lightningrecordform/