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
ashwini gattuashwini gattu 

Action failed: c:TeacherAppCompo$controller$createClient [helper is not defined]

Hi ,

I tried to create my first lightning application but i get the folowing error.

User-added image

Below is my code

Application
<aura:application extends="force:slds" >
    <c:TeacherAppCompo/>
</aura:application>

Component
<aura:component controller='TeacherCreateRecord' >

    <aura:attribute name="client" type="Teacher__c" default="{'sObjectType':'Teacher__c','Name' : '' }" />
    <ui:inputtext label="Enter Teacher Name"   value="{!v.client.Name}" />
    <ui:button label="Submit" press="{!c.createClient}"/>
</aura:component>

Client side contoller
({
    createClient : function(component, event) {
        
        
       //for logs
        console.log("In Client Controller");

        //getting the record info from component to js
        var varClient=component.get("v.client");
       
        //validation to  check of field is empty or not
        if($A.util.isEmpty(varClient.Name) || $A.util.isUndefined(varClient.Name) )
        {
           alert('First name is required');
            //return the value and function will stop
            return;
        }
         
         helper.createRecord(varClient);
    }
})

Helper
({
    createRecord : function() {
         
       
       console.log("In helper");
        
        //calling apex function
        alert('Before action');
        var action = component.get("c.createTeacherRecord");
         
        alert('Before set params function');
        //set the parameters
        action.setParams({
            client : varClient
        });
        
        alert('Before callback function');
        //set callback function
        action.setCallback(this,function(a){
            
            var response=a.getState();
            if(response == "SUCCESS")
            {
                //Reset form and this is optional
                var newrecord= {'sObjectType' : 'Teacher__c','Name':''};
                component.set("v.client",newrecord);
                alert('Record is Created');
            }else if(response == "ERROR")
            {
                alert('Record is not Created');
            }
        });
        //To enqueue all actions and let all happen one by one 
        $A.enqueueAction(action);
    }
    
})

ApexClass
public with sharing class TeacherCreateRecord {
    
    
    @AuraEnabled
    public static void createTeacherRecord(Teacher__c  client)
    {
        try{
            System.debug('Teacher Create Record class'+ client) ;
            if(client != null)
            {
                insert client;
            }
        }
    
       catch(Exception e) 
       {
            System.debug('Inside Apex class') ;   
       }
    }
 }

The object i have used is Teacher__c and i am trying to fetch the name which is a standard field .
Best Answer chosen by ashwini gattu
sachinarorasfsachinarorasf
Hi Ashwini,

I have gone through your problem.Please try the below code.
 
Component:
<aura:component controller='TeacherCreateRecord' >

    <aura:attribute name="client" type="Teacher__c" default="{'sObjectType':'Teacher__c','Name' : '' }" />
    <ui:inputtext label="Enter Teacher Name"   value="{!v.client.Name}" />
    <ui:button label="Submit" press="{!c.createClient}"/>
</aura:component>

Controller:
({
    createClient : function(component, event,helper) {
        
        
       //for logs
        console.log("In Client Controller");

        //getting the record info from component to js
        var varClient=component.get("v.client");
       
        //validation to  check of field is empty or not
        if($A.util.isEmpty(varClient.Name) || $A.util.isUndefined(varClient.Name) )
        {
           alert('First name is required');
            //return the value and function will stop
            return;
        }
         
         helper.createRecord(component, event,helper,varClient);
    }
})

Helper:
({
    createRecord : function(component, event,helper,varClient) {
         
       
       console.log("In helper");
        
        //calling apex function
        alert('Before action');
        var action = component.get("c.createTeacherRecord");
         
        alert('Before set params function');
        //set the parameters
        action.setParams({
            client : varClient
        });
        
        alert('Before callback function');
        //set callback function
        action.setCallback(this,function(a){
            
            var response=a.getState();
            if(response == "SUCCESS")
            {
                //Reset form and this is optional
                var newrecord= {'sObjectType' : 'Teacher__c','Name':''};
                component.set("v.client",newrecord);
                alert('Record is Created');
            }else if(response == "ERROR")
            {
                alert('Record is not Created');
            }
        });
        //To enqueue all actions and let all happen one by one 
        $A.enqueueAction(action);
    }
    
})

apex Class:
public class TeacherCreateRecord {
    @AuraEnabled
    public static void createTeacherRecord(Teacher__c  client)
    {
        try{
            System.debug('Teacher Create Record class'+ client) ;
            if(client != null)
            {
                insert client;
            }
        }
        
        catch(Exception e) 
        {
            System.debug('Inside Apex class') ;   
        }
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora

All Answers

ayu sharma devayu sharma dev
Hello Ashwini

It looks like the problem is that you are passing an argument when calling the Helper : 
 
helper.createRecord(varClient);

But Helper createRecord method that does not seem to accepting one. Try replacing the helper method line as follow:
 
createRecord : function( varClient ){

Let me know if the problem still persists.

Regards
Ayush
ashwini gattuashwini gattu
I still get the error.
The code works only if i dont call the helper and add all the code in the client side controller itself.
sachinarorasfsachinarorasf
Hi Ashwini,

I have gone through your problem.Please try the below code.
 
Component:
<aura:component controller='TeacherCreateRecord' >

    <aura:attribute name="client" type="Teacher__c" default="{'sObjectType':'Teacher__c','Name' : '' }" />
    <ui:inputtext label="Enter Teacher Name"   value="{!v.client.Name}" />
    <ui:button label="Submit" press="{!c.createClient}"/>
</aura:component>

Controller:
({
    createClient : function(component, event,helper) {
        
        
       //for logs
        console.log("In Client Controller");

        //getting the record info from component to js
        var varClient=component.get("v.client");
       
        //validation to  check of field is empty or not
        if($A.util.isEmpty(varClient.Name) || $A.util.isUndefined(varClient.Name) )
        {
           alert('First name is required');
            //return the value and function will stop
            return;
        }
         
         helper.createRecord(component, event,helper,varClient);
    }
})

Helper:
({
    createRecord : function(component, event,helper,varClient) {
         
       
       console.log("In helper");
        
        //calling apex function
        alert('Before action');
        var action = component.get("c.createTeacherRecord");
         
        alert('Before set params function');
        //set the parameters
        action.setParams({
            client : varClient
        });
        
        alert('Before callback function');
        //set callback function
        action.setCallback(this,function(a){
            
            var response=a.getState();
            if(response == "SUCCESS")
            {
                //Reset form and this is optional
                var newrecord= {'sObjectType' : 'Teacher__c','Name':''};
                component.set("v.client",newrecord);
                alert('Record is Created');
            }else if(response == "ERROR")
            {
                alert('Record is not Created');
            }
        });
        //To enqueue all actions and let all happen one by one 
        $A.enqueueAction(action);
    }
    
})

apex Class:
public class TeacherCreateRecord {
    @AuraEnabled
    public static void createTeacherRecord(Teacher__c  client)
    {
        try{
            System.debug('Teacher Create Record class'+ client) ;
            if(client != null)
            {
                insert client;
            }
        }
        
        catch(Exception e) 
        {
            System.debug('Inside Apex class') ;   
        }
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
This was selected as the best answer
ashwini gattuashwini gattu
Hey Sachin It worked.

I kept using helper.createRecord(component, event,helper) all the  while  , i dint even know that we can pass the variable seperately as well.

Thankyou !!
Saroj SahooSaroj Sahoo
I tried all the suggestions you people have shared, however still I am getting the same error. Ashwini - could you please share your code which is working fine for you?
Saroj SahooSaroj Sahoo
After entering data on UI, when I am clicking on submit button, I am getting this error
 
Naresh V 19Naresh V 19
Hi Sachin Arora,
your answer to Ashwini gattu also helped in to resolve the issue of same problem, by using helper.createRecord(component, event,helper,varClient); and createRecord : function(component, event,helper,varClient). Thank you Sachin