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
Haneef SHaneef S 

Error while displaying list of accounts in lightning

Hi Team,

I need to Create an application which displays all the accounts in my org. But i am getting below error, can some one please help.
Here is my code:
DisplayAllaccounts.app
<aura:application >
    <c:DisplayAllAccountscomp />
</aura:application>
DisplayAllAccountsComp.cmp
<aura:component controller="displayAccounts">
    <aura:attribute name="accountrows" type="object[]"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit1}"/>
                  <div class="container">
    <p>
            <b> List of Contacts </b>
        </p>
                      <table class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th><strong> Id </strong></th>
                    <th> <strong>  Name </strong> </th>
                </tr>
            </thead>
            <tbody>
                <aura:iteration var="cell" items="{!v.accountrows}">
                    <tr>
                        <td> <a href="{! '/'+cell.Id}"> {!cell.Id} </a> </td>
                        <td> {!cell.Name}  </td>
                    </tr>
                </aura:iteration>
            </tbody>
        </table>
    </div>
</aura:component>
DisplayAllAccountshelper.js
({
    getallaccounts : function(component, event,helper) {
        var action1 = component.get("c.getaccounts");
        action1.setcallback(this,function(a){
                           component.set("v.accountrows",a.getReturnValue());
                           });
        $A.enqueueAction(action1);
     
        
    }
})
DisplayAllAccountsController.js

({
    doInit1 : function(component, event, helper) {
        helper.getallaccounts(component);
        
    }
})
I am getting below error:
error
Haneef SHaneef S
Forgot to add my apex controller
displayAccounts.apxc:

public class displayAccounts {
    @auraenabled
    public static list<Account> getaccounts()
    {
        return [select id, name from account];
    }

}
Ajay K DubediAjay K Dubedi
Hi Haneef,

Please change your Controller helper class which is given below copy and paste in helper class.
Your problem has solved.

({
    getallaccounts: function (component, event, helper) {
        console.log("called helper");
        var action = component.get("c.getaccounts");
        action.setCallback(this,function (a) {
            var rtnValue = a.getReturnValue();
            if (rtnValue !== null) {
                component.set("v.accountrows",a.getReturnValue());                
            } 
        });
        $A.enqueueAction(action);
    }
})

Please let me know if you have any query.
Please mark it as best Answer if you find it helpful.

Thank You
Ajay Dubedi
Haneef SHaneef S
Thanks for your response Ajay,
I have made changes suggested by you but still facing same error
Thanks,
Haneef
Ajay K DubediAjay K Dubedi
Hi Haneef,

This code is working fine in my org.
Please try this with the new component.

--DisplayAllAccountsComponent.cmp---

<aura:component controller="DisplayAccounts_ApexCont" access="global">
    <aura:attribute name="accountrows" type="object[]"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
                  <div class="container">
    <p>
            <b> List of Contacts </b>
        </p>
                      <table class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th><strong> Id </strong></th>
                    <th> <strong>  Name </strong> </th>
                </tr>
            </thead>
            <tbody>
                <aura:iteration var="cell" items="{!v.accountrows}">
                    <tr>
                        <td> <a href="{! '/'+cell.Id}"> {!cell.Id} </a> </td>
                        <td> {!cell.Name}  </td>
                    </tr>
                </aura:iteration>
            </tbody>
        </table>
    </div>
</aura:component>

----DisplayAllAccountsComponentController.js---
({
      doInit : function(component, event, helper) {
        helper.getallaccounts_Helper(component,event,helper);
        
    }
})

----DisplayAllAccountsComponentHelper.js---

({
    getallaccounts_Helper : function (c, e, h){
        try {
            var action = c.get("c.getaccounts");
            action.setCallback(this, function (r) {
                if(r.getState() === 'SUCCESS') {
                    var storedResponse = r.getReturnValue();
                    console.log('storedResponse:');
                    c.set('v.accountrows', storedResponse);
                } else {
                    console.log('ERROR');
                    console.log(r.getError());
                }
            });
            $A.enqueueAction(action);
        } catch (ex) {
            console.log(ex);
        }
    },
    
})

----DisplayAllAccountsApplication.app---

<aura:application extends ="force:slds" access="global">
    <c:DisplayAllAccountsComponent/>
</aura:application>

---DisplayAccounts_ApexCont---

public class DisplayAccounts_ApexCont {
 @auraenabled
    public static list<Account> getaccounts()
    {
        return [select id, name from account];
    }

}
Please let me know if you have any query.
Please mark it as best Answer if you find it helpful.

Thank You
Ajay Dubedi
Haneef SHaneef S
I am getting same error.
User-added image
Not sure, Why  I am getting same error.
Do I need to change any settings??
Please help
Thanks,
Haneef
Ajay K DubediAjay K Dubedi
Hi Haneef,

Above code is working fine in my Org.I have tested so many times.
Please check method name and doInit name also if same then copy and paste where is using.
1-go to developer console.
2-Create new Lightning component.
3-In right side of component click on controller and helper insert the above code very carefully.
there is no changes of code copy and run and name of class and component are same if you create new.

-- copy method name from apex class and paste in helper where the method is used--- 

    javaScript is case-sensitive
    in your class, method name is getaccounts but you use getAccounts so you got this error 
    use
    var action = c.get("c.getaccounts");
    in your  DisplayAllAccountsComponentHelper.js

Please let me know if you have any query.
Please mark it as best Answer if you find it helpful.

Thank You
Ajay Dubedi