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
noor Zabinoor Zabi 

How to insert a new record opportunity via lightning Component

I am not able insert a record in opprotunity object via lightnig components. I don't where i going wrong. 

Component: -
<aura:component controller="BookingController"  implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction"  access="global">

    
<aura:attribute name="BookObj" type="String" default="{'sobjecttype' : 'Opportunity'}"/>
<aura:attribute name="StaffId" type="String"/>  
<aura:attribute name="Name" type="String" default=""/>
<aura:attribute name="BookingDate" type="Date" default=""/>
<aura:attribute name="Kilometer" type="String" default=""/>
<aura:attribute name="Pickup" type="String" default=""/>
<aura:attribute name="Drop" type="String" default=""/>
<aura:attribute name="Cab" type="String"/>
<aura:attribute name="SelectPayment" type="String[]" default=""/>
<aura:attribute name="AcctId" type="String" default="Account[]"/>
<aura:attribute name="DriverId" type="String" default="Employee_Information__c"/>
    <aura:attribute name="Stage" type="String" default="New"/>

<br/>
<div class="slds-page-header">
        <div class="slds-align_absolute-center">
            <div class="slds-text-heading_large">
                <div class="slds-m-top_xx-large">
                    Booking Cab
                </div>
            </div>
        </div>
    </div>
    
<br/>
<lightning:recordEditForm aura:id="recordEditForm" objectApiName="Opportunity">
    <lightning:messages/>
    <lightning:inputField fieldName="Name" type="String" value="{!v.Name}"/>
    <lightning:inputField fieldName="CloseDate" type="Date"  value="{!v.BookObj.BookingDate}"/>
    <lightning:inputField fieldName="Pickup_Location__c" type="String" value="{!v.BookObj.Pickup}"/>
    <lightning:inputField fieldName="Dropping_Location__c" type="String" value="{!v.BookObj.Drop}"/>
    <lightning:inputField fieldName="Kilometer__c" type="String" value="{!v.BookObj.Kilometer}"/>
    <lightning:inputField fieldName="Select_Option__c" type="String" value="{!v.BookObj.Cab}"/>
    <lightning:inputField fieldName="Select_Payment__c" type="String" value="{!v.BookObj.SelectPayment}"/>
    <lightning:inputField label="Customer id" fieldName="AccountId"  type="String" selectedRecord="{!v.BookObj.AccntId}"/>
    <lightning:inputField fieldName="Driver__c" selectedRecord="{!v.BookObj.AccntId}" type="String" required="true"/>
    <lightning:inputField fieldName="StageName"  value="{!v.BookObj.Stage}"/>
</lightning:recordEditForm>

<lightning:button label="Book" onclick="{!c.doBook}"></lightning:button>
</aura:component>

Controller: -

({
  doBook : function(component, event, helper) { 
    var action = component.get("c.SaveRecord");
    action.setParams({ 
        "Opp": component.get("v.BookObj")
    });
    action.setCallback(this, function(a) {
           var state = a.getState();
            if (state === "SUCCESS") {
                var name = a.getReturnValue();
               alert("hello from here"+Name);
            }
        });
    $A.enqueueAction(action)
}
})


Apex Class: -
public with sharing class BookingController {
    
    @AuraEnabled
    public static Opportunity SaveRecord(Opportunity oppObj){
         System.debug('opportunity::' +oppObj.Name);
        insert oppObj;
        return oppObj;
        
    }
}
Best Answer chosen by noor Zabi
mukesh guptamukesh gupta
Hi Noor,

IF you are using recordEditFrom the you don't need any apex code So please use below code:-

Component:-
<aura:component controller="BookingController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global" >
    <aura:attribute name="BookObj" type="String" default="{'sobjecttype' : 'Opportunity'}"/>
    
    <aura:attribute name="SelectPayment" type="String[]" default=""/>
    <aura:attribute name="AcctId" type="String" default="Account[]"/>
    <aura:attribute name="DriverId" type="String" default="Employee_Information__c"/>
    


<aura:attribute name="disabled" type="Boolean" default="false" />
    <aura:attribute name="saved" type="Boolean" default="false" />
    <aura:attribute name="showSpinner" type="Boolean" default="true" />
    <aura:attribute name="iconName" type="String" default="Opportunity" /> 
    <aura:attribute name="recordId" type="String" />
    <aura:attribute name="pageReference" type="Object"/>
    <lightning:navigation aura:id="navService"/>
    
    <aura:if isTrue="{!v.showSpinner}">
        <lightning:spinner />
    </aura:if>
    <aura:if isTrue="{!!v.saved}">
        
    <lightning:card iconName="{!'standard:'+v.iconName}" title="{! 'New '+v.BookObj}">
        <div class="slds-p-left_large slds-p-right_medium"> 
            
        <lightning:recordEditForm
           onload="{!c.handleLoad}"
            onsubmit="{!c.handleSubmit}"
            onsuccess="{!c.handleSuccess}"
            objectApiName="{!v.BookObj}">
           <!--  the messages component is for error messages -->
            <lightning:messages />

            
             <lightning:inputField fieldName="Name" />
            <lightning:inputField fieldName="CloseDate" />
            <lightning:inputField fieldName="Pickup_Location__c"/>
            <lightning:inputField fieldName="Dropping_Location__c" />
            <lightning:inputField fieldName="Kilometer__c" />
            <lightning:inputField fieldName="Select_Option__c" />
            <lightning:inputField fieldName="Select_Payment__c"/>
            <lightning:inputField label="Customer id" fieldName="AccountId"  type="String" value ="{!v.AccntId}"/>
            <!--<lightning:inputField fieldName="Driver__c" selectedRecord="{!v.BookObj.AccntId}" type="String" required="true"/> NTO CLEAR--> 
            <lightning:inputField fieldName="StageName"/>
            
            
            <div class="slds-m-top_medium">
                <lightning:button disabled="{!v.disabled}" variant="brand" type="submit" name="save" label="Save" />
            </div>
        </lightning:recordEditForm>
        <aura:set attribute="else">
            <p>Saved! New record id is {!v.recordId}</p>
        </aura:set>
            </div>
    </lightning:card>
    </aura:if>

</aura:component>


 Controller.js
({
    handleLoad : function(component, event, helper) {
	component.set("v.showSpinner", false);
	//use this to pre populate fields wth some data
     
	},
    handleSubmit : function(component, event, helper) {
       //We don't need to put basic validation here as that are handle by lightning:inputfield and recordEditForm
       //event.preventDefault(); use this to stop default flow
    },
   handleSuccess : function(component, event, helper) {
	    
	//Redirect to detail page on success
	var payload = event.getParams().response;
        var navService = component.find("navService");
    
        var pageReference = {
            type: 'standard__recordPage',
            attributes: {
                "recordId": payload.id,
                "objectApiName": component.get("v.sObjectName"),
                "actionName": "view"
            }
        }
        event.preventDefault();
        navService.navigate(pageReference);
    }
})

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh