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
VSK98VSK98 

Getting Error in $A.getCallback() [Cannot read property 'Name' of null] Failing descriptor: {lightning:input}

Hello All,

I have just tried my first lightning page, the page contains two text fields and one button. Once click on button account record should be create. After click on preview i can able to see the form but when i entered the data in the text box i am getting below error. Could you please help me on this.

User-added image

Please find the lightning code below:
 
<aura:component >
    <aura:attribute name="NewAccount" type="Account"/>
	<lightning:input label="AccountName" name="myname" required="true" value="{!v.NewAccount.Name}" />
    <lightning:input label="Fax" name="fax" required="true" value="{!v.NewAccount.fax}"/>
    <lightning:button variant="brand" label="Submit" onclick="{!c.createAccount}"/> 
</aura:component>


({
	myAction : function(component, event) {
	var newAcc = component.get("v.NewAccount");
    var action = component.get("c.createAccount");
    action.setParams({ 
        "acc": newAcc
    });	
      action.setCallback(this, function(a) {
           var state = a.getState();
            if (state === "SUCCESS") {
                var name = a.getReturnValue();
               alert("hello from here"+name);
            }
        });
    $A.enqueueAction(action)   
        
	}
})

Advance thanks,
VSK98​
Best Answer chosen by VSK98
Ashif KhanAshif Khan
Hi VSK98,

As per Gauravendra it is right you are calling wrong controller action.
But problem [Cannot read property 'Name' of null]   which shows in pop while you type in input box is due to required lightning:input to overcome this issue you need to set default values of NewAccount Attribute.
<aura:attribute name="NewAccount" type="Account" default="{ 'sobjectType': 'Account',
                        'Name': '',
                        'Fax':'' }"/>

also am sharing working code of yours which I tried in my dev org
<!--myApp.app__>

<aura:application extends="force:slds">
    <c:createAccountComp />
</aura:application>
 
<!--createAccountComp.cmp-->

<aura:component controller='myNewAccController'>
    <aura:attribute name="NewAccount" type="Account" default="{ 'sobjectType': 'Account',
                        'Name': '',
                        'Fax':'' }"/>
	<lightning:input label="AccountName" name="myname" required="true" value="{!v.NewAccount.Name}" />
    <lightning:input label="Fax" name="fax" required="true" value="{!v.NewAccount.Fax}"/>
    <lightning:button variant="brand" label="Submit" onclick="{!c.createAccountInfo}"/> 
</aura:component>



 
<!--createAccountCompController.js-->


({
	createAccountInfo : function(component, event) {
	var newAcc = component.get("v.NewAccount");
    var action = component.get("c.createAccount");
    action.setParams({ 
        "acc": newAcc
    });	
      action.setCallback(this, function(a) {
           var state = a.getState();
            if (state === "SUCCESS") {
                var name = a.getReturnValue();
               alert("hello from here"+ JSON.stringify(name));
            }
          else{
              alert(state);
          }
        });
    $A.enqueueAction(action)   
        
	}
})

 

 
<!--myNewAccController.apxc-->

public class myNewAccController {
    
    @AuraEnabled 
    public static Account createAccount(Account acc){
       insert acc;
        return acc;
    }
}

Let me know , Is it work?

All Answers

GauravendraGauravendra
Hi VSK98,

From component button onclick it should call the javascript handler. You are directly calling the apex method. 
Change the onclick to call myAction method of javascript.
<lightning:button variant="brand" label="Submit" onclick="{!c.myAction}"/>
And add semicolon after $A.enqueueAction(action);

Let me know, if this all it needed.

Hope this helps.
Ashif KhanAshif Khan
Hi VSK98,

As per Gauravendra it is right you are calling wrong controller action.
But problem [Cannot read property 'Name' of null]   which shows in pop while you type in input box is due to required lightning:input to overcome this issue you need to set default values of NewAccount Attribute.
<aura:attribute name="NewAccount" type="Account" default="{ 'sobjectType': 'Account',
                        'Name': '',
                        'Fax':'' }"/>

also am sharing working code of yours which I tried in my dev org
<!--myApp.app__>

<aura:application extends="force:slds">
    <c:createAccountComp />
</aura:application>
 
<!--createAccountComp.cmp-->

<aura:component controller='myNewAccController'>
    <aura:attribute name="NewAccount" type="Account" default="{ 'sobjectType': 'Account',
                        'Name': '',
                        'Fax':'' }"/>
	<lightning:input label="AccountName" name="myname" required="true" value="{!v.NewAccount.Name}" />
    <lightning:input label="Fax" name="fax" required="true" value="{!v.NewAccount.Fax}"/>
    <lightning:button variant="brand" label="Submit" onclick="{!c.createAccountInfo}"/> 
</aura:component>



 
<!--createAccountCompController.js-->


({
	createAccountInfo : function(component, event) {
	var newAcc = component.get("v.NewAccount");
    var action = component.get("c.createAccount");
    action.setParams({ 
        "acc": newAcc
    });	
      action.setCallback(this, function(a) {
           var state = a.getState();
            if (state === "SUCCESS") {
                var name = a.getReturnValue();
               alert("hello from here"+ JSON.stringify(name));
            }
          else{
              alert(state);
          }
        });
    $A.enqueueAction(action)   
        
	}
})

 

 
<!--myNewAccController.apxc-->

public class myNewAccController {
    
    @AuraEnabled 
    public static Account createAccount(Account acc){
       insert acc;
        return acc;
    }
}

Let me know , Is it work?
This was selected as the best answer
VSK98VSK98
Hi Ashif,

Thanks for your response!

I have tried with your code but i am getting another issue. please find the error below.
Undefined

Regards,
Siva
Ashif KhanAshif Khan
Hi Siva,
It's working fine on my side.

User-added image
Please share your code Am curious to know what you missing.
VSK98VSK98
Hi Ashif,

Really strange to me!!!!!
Please find the snippet below:

Component:
<aura:component controller="AccountController">
    <aura:attribute name="NewAccount" type="Account" default="{ 'sobjectType': 'Account',
                        'Name': '',
                        'Fax':'' }"/>
	<lightning:input label="Name" name="myname" required="true" value="{!v.NewAccount.Name}" />
    <lightning:input label="Fax" name="fax" required="true" value="{!v.NewAccount.Fax}"/>
    <lightning:button variant="brand" label="Submit" onclick="{!c.createAccount}"/> 
</aura:component>

Client Side Controller:
({
    createAccount : function(component, event) {
       // alert('dhdhhg');
	var newAcc = component.get("v.NewAccount");
     //   console.log("current text: " + newAcc);
       var action = component.get("c.createAccount");
  
       
   action.setParams({ 
       "acc": newAcc
                
   });	
   
      action.setCallback(this, function(response) {
           var state = response.getState();
            if (state === "SUCCESS") {
                alert("From server: " + response.getReturnValue());

                var name = response.getReturnValue();
              alert("hello from here"+ JSON.stringify(name));
            }
          else{
              alert(state);
          }
        });
    $A.enqueueAction(action);
        
	}
})

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

Server Side Controller:
public with sharing class AccountController{
@AuraEnabled

public static Account createAccount(Account acc) {


Insert acc;
return acc;
}
}

Adv Thanks,
VSK98​ 
​​
Ashif KhanAshif Khan
Hi VSK98,
you are facing the issue due to the same name of JS Action As createAccount and Apex Method as createAccount   
while we take same name of JS action and Apex Method it's lightning behavior of recurrsive call.

Change JS action name 
<lightning:button variant="brand" label="Submit" onclick="{!c.createAccountInfo}"/>

Same in JS controller Too
createAccountInfo : function(component, event) {

Keep All code same Just change JS action, Your Code will work perfectly.


 
VSK98VSK98
Hi Ashif, Thanks............I'ts working fine now :)....... Regards, VSK98
Radhashree YarashiRadhashree Yarashi
As per the new release notes of Winter'21, user should have access to aura enbled classes.
I had simillar issue, I have added the aura class access to user profile or you can add it in permission set to resolve issue
https://releasenotes.docs.salesforce.com/en-us/winter21/release-notes/salesforce_release_notes.htm#:~:text=Home%20%3E-,Salesforce%20Winter%20'21%20Release%20Notes,to%20your%20customers%2C%20from%20anywhere.&text=Our%20release%20notes%20offer%20brief,of%20enhancements%20and%20new%20features (https://apc01.safelinks.protection.outlook.com/?url=https:%2F%2Freleasenotes.docs.salesforce.com%2Fen-us%2Fwinter21%2Frelease-notes%2Fsalesforce_release_notes.htm%23:~:text%3DHome%2520%253E-%2CSalesforce%2520Winter%2520'21%2520Release%2520Notes%2Cto%2520your%2520customers%252C%2520from%2520anywhere.%26text%3DOur%2520release%2520notes%2520offer%2520brief%2Cof%2520enhancements%2520and%2520new%2520features&data=02%7C01%7CRadhashree_Yarashi%40infosys.com%7C847dd6b5ba334cb9f3b008d8639306de%7C63ce7d592f3e42cda8ccbe764cff5eb6%7C1%7C0%7C637368830407597564&sdata=qe5nCgUKHjh3P9leH3E5ZOCiV4MN1BoH1C7Jp8xHf3w%3D&reserved=0" style="color:#0563c1; text-decoration:underline)

https://admin.salesforce.com/blog/2020/critical-update-ensure-users-have-access-to-auraenabled-methods (https://apc01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fadmin.salesforce.com%2Fblog%2F2020%2Fcritical-update-ensure-users-have-access-to-auraenabled-methods&data=02%7C01%7CRadhashree_Yarashi%40infosys.com%7C847dd6b5ba334cb9f3b008d8639306de%7C63ce7d592f3e42cda8ccbe764cff5eb6%7C1%7C0%7C637368830407597564&sdata=0SDoj3elDJtlT9LZMxTXy%2FE24JRmi90JLEFQ207V4WI%3D&reserved=0" style="color:#0563c1; text-decoration:underline)


 
Beatrice DickBeatrice Dick
We had kind of a similar Error Message. In our Case it seems, that updating the Chrome Browser to the newest Version fixed the Issue.
Newest Chrome Version: 96.0.4664.45
Here our Message:
Error
Error in $A.getCallback() [Cannot read properties of undefined (reading 'set')]
Object._resetContentBodyAndFooter()@https://xxxx.lightning.force.com/components/flowruntime/flowRuntimeV2.js:27:277
Object.buildComponentTree()@https://xxxx.lightning.force.com/components/flowruntime/flowRuntimeV2.js:17:19
eval()@https://xxxxx.lightning.force.com/components/flowruntime/runtimeLib.js:1:24868
Object.eval()@https://xxxx.lightning.force.com/components/flowruntime/flowRuntimeV2.js:11:77
Q._updateState()@https://xxxxx.lightning.force.com/components/flowruntime/runtimeLib.js:1:26471
eval()@https://xxxxx.lightning.force.com/components/flowruntime/runtimeLib.js:1:26847