+ Start a Discussion
Ash asmanAsh asman 

Lightning Component creating Lead : What's Wrong with My Submit Button?

Hi,
When I click "submit", Action failed: c:Lead$controller$doInit [helper.getAllRecords is not a function]User-added image
My Code For Lead
<aura:component controller="LeadRecord"
            access="global"    implements="force:lightningQuickActionWithoutHeader,force:hasRecordId">
    
    
    <!-- Include Static Resource-->
    <ltng:require styles="/resource/bootstrap/css/bootstrap.min.css"
                  scripts="/resource/bootstrap/js/jquery.js,/resource/bootstrap/js/bootstrap.min.js"/>
    
    <!-- Define Attribute-->
    <aura:attribute name="LeadRec" type="Lead" default="{'sobjectType': 'Lead',
                                                     'FirstName': '',
                                                     'LastName': '',
                                                     'Email': '',
                                                     'Title': '',                            
                                                     'phone': ''
                                                     }"/>
    
    <!-- Display a header with details about the account -->
    <div class="slds-page-header" role="banner">
        <p class="slds-text-heading_label">{!v.Lead.Name}</p>
        <h1 class="slds-page-header__title slds-m-right_small
                   slds-truncate slds-align-left">Create New Lead Data</h1>
    </div>
    
    <div class="slds-form-element">
        <label class="slds-form-element__label" for="form-element-03">
            <abbr class="slds-required" title="required">* </abbr>Frist Name</label>
        <div class="slds-form-element__control">
            <input  aura:id="FirstName"    type="text" id="form-element-03"  value="{!v.LeadRec.FirstName}"  required="" class="slds-input" />
        </div>
    </div>
    <div class="slds-form-element">
        <label class="slds-form-element__label" for="form-element-03">
            <abbr class="slds-required" title="required">* </abbr>Last Name</label>
        <div class="slds-form-element__control">
            <input  aura:id="LastName"         type="text" id="form-element-03"  value="{!v.LeadRec.LastName}" required="" class="slds-input" />
        </div>
    </div>
    
    <div class="slds-form-element">
        <label class="slds-form-element__label" for="form-element-03">
            <abbr class="slds-required"   title="required">* </abbr>Company</label>
        <div class="slds-form-element__control">
            <input aura:id="company"  type="text" id="form-element-03"  value="{!v.LeadRec.Company}"  required="" class="slds-input" />
        </div>
    </div>
    
    <div class="slds-form-element">
        <label class="slds-form-element__label" for="form-element-03">
            <abbr class="slds-required" > </abbr>Title</label>
        <div class="slds-form-element__control">
            <input aura:id="Title"  type="text" id="form-element-03"  value="{!v.LeadRec.Title}"  required="" class="slds-input" />
        </div>
    </div>
    
    <aura:handler name="init" action="{!c.doInit}" value="{!this}" />

    
    <div class="slds-form-element">
        <label class="slds-form-element__label" for="form-element-03">
            <abbr class="slds-required" > </abbr>Email </label>
        <div class="slds-form-element__control">
            <input aura:id="Email"  type="text" id="form-element-03"  value="{!v.LeadRec.Email}"  required="" class="slds-input" />
        </div>
    </div>   
    <div class="slds-form-element">
        <label class="slds-form-element__label" for="form-element-03">
            <abbr class="slds-required" > </abbr>phone </label>
        <div class="slds-form-element__control">
            <input aura:id="phone" type="text" id="form-element-03"  value="{!v.LeadRec.phone}"  required="" class="slds-input" />
        </div>
    </div>   
    
    <div class="col-md-4 text-center" >
        
        <lightning:button variant="brand" label="Submit" onclick="{!c.createLeadInfo}"/>
    </div>
</aura:component>

LeadRecordApex:
public with sharing class LeadRecord {
@AuraEnabled
    public static list<Lead> getRecords(){
        return [select id, FirstName,LastName,Company,phone,Email,Title from Lead];
    }  
    
    
    
    
    @AuraEnabled
    public static Lead createRecord (Lead  LeadRec){
      upsert LeadRec;
        return LeadRec;
}
}

LeadControllerCode:
({
    doInit:function(component,event,helper){
        helper.getAllRecords(component);
    },
    
    
    
    createLeadInfo : function(component, event, helper) {
        console.log('Create record');
        var saveLeadAction=component.get("c.LeadRec");
       
        
        //Validation
        if($A.util.isEmpty(LeadRec.FirstName) || $A.util.isUndefined(LeadRec.FirstName)){
            // alert('First Name is Required');
            return;
        }            
        if($A.util.isEmpty(LeadRec.LastName) || $A.util.isUndefined(LeadRec.LastName)){
            // alert('Last Name is Required');
            return;
        }
        if($A.util.isEmpty(LeadRec.Email) || $A.util.isUndefined(LeadRec.Email)){
            // alert('Email is Required');
            return;
        }
        if($A.util.isEmpty(LeadRec.Title) || $A.util.isUndefined(LeadRec.Title)){
            // alert('Email is Required');
            return;
        }
        
        
        
        if($A.util.isEmpty(LeadRec.Company) || $A.util.isUndefined(LeadRec.Company)){
            
            return;
        }
        //Calling the Apex Function
        var saveLeadAction = component.set("c.LeadRec");
        
        //Setting the Apex Parameter
        saveLeadAction.setParams({
            "Lead":component.get("v.LeadRec")
        });
        
        //Setting the Callback
        saveLeadAction.setCallback(this,function(a){
            //get the response state
            var state = a.getState();
            
            //check if result is successfull
            if(state == "SUCCESS"){
                //Reset Form
                var newCandidate = {'sobjectType': 'Lead',
                                    'FirstName': '',
                                    'LastName': '',
                                    'Company': '',
                                    'Title': '',
                                    'Phone': '',
                                    'Email': ''
                                   };
                //resetting the Values in the form
                component.set("v.LeadRec",newCandidate);
                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(saveLeadAction);
        
    }
})

LeadApp Code:
<aura:application extends="force:slds" >
  <lightning:layout>
        <lightning:layoutItem padding="around-large">
            <h1 class="slds-text-heading_large">Lead Information</h1>
        </lightning:layoutItem>
    </lightning:layout>
    
    <aura:dependency resource="c:Lead" />
    <c:Lead />
</aura:application>
 

 
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

In doInit method, you are calling helper method getAllRecords, but you didn't create a method in helper. 
 
doInit:function(component,event,helper){
        helper.getAllRecords(component);
    },

Create a getAllRecords method in helper.
getAllRecords:function(component){
        // Your code
    },

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Raj VakatiRaj Vakati
Change your code as below 

Controller.js
 
({
    doInit:function(component,event,helper){
        helper.getAllRecords(component);
    },
    
   })

Helper.js
 
({
    getAllRecords : function(component, event, helper) {
        console.log('Create record');
        var saveLeadAction=component.get("c.LeadRec");
       
        
        //Validation
        if($A.util.isEmpty(LeadRec.FirstName) || $A.util.isUndefined(LeadRec.FirstName)){
            // alert('First Name is Required');
            return;
        }            
        if($A.util.isEmpty(LeadRec.LastName) || $A.util.isUndefined(LeadRec.LastName)){
            // alert('Last Name is Required');
            return;
        }
        if($A.util.isEmpty(LeadRec.Email) || $A.util.isUndefined(LeadRec.Email)){
            // alert('Email is Required');
            return;
        }
        if($A.util.isEmpty(LeadRec.Title) || $A.util.isUndefined(LeadRec.Title)){
            // alert('Email is Required');
            return;
        }
        
        
        
        if($A.util.isEmpty(LeadRec.Company) || $A.util.isUndefined(LeadRec.Company)){
            
            return;
        }
        //Calling the Apex Function
        var saveLeadAction = component.set("c.LeadRec");
        
        //Setting the Apex Parameter
        saveLeadAction.setParams({
            "Lead":component.get("v.LeadRec")
        });
        
        //Setting the Callback
        saveLeadAction.setCallback(this,function(a){
            //get the response state
            var state = a.getState();
            
            //check if result is successfull
            if(state == "SUCCESS"){
                //Reset Form
                var newCandidate = {'sobjectType': 'Lead',
                                    'FirstName': '',
                                    'LastName': '',
                                    'Company': '',
                                    'Title': '',
                                    'Phone': '',
                                    'Email': ''
                                   };
                //resetting the Values in the form
                component.set("v.LeadRec",newCandidate);
                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(saveLeadAction);
        
    }
})

 
Ash asmanAsh asman
Thank you so much its Working
Ronaldo Costa 8Ronaldo Costa 8
It is saying:

Unable to find action 'LeadRec' on the controller of c:LeadRecord

Can you please help?

Thanks