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
badri nath 9badri nath 9 

how can i load the data into lightining page?

Component:
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" controller="countRecords" >
    <aura:attribute name="contactcount" type="integer" />
     <aura:attribute name="Accountcount" type="integer" />
    <aura:attribute name="oppcount" type="integer" />
    <div style = "slds">
        <lightning:button onclick="{!c.count}" title="to get the total numbe of records" class="slds-button-sucess"  label="count" variant="brand" iconName="utility:people" iconPosition="left" /><br/>

        Totalnumber of accounts:<ui:outputText value="{!v.contactcount}" title="Totalnumber of accounts" class="slds-output"/><br/>
        Totalnumber of contacts:<ui:outputText value="{!v.Accountcount}" title="Totalnumber of contacts" class="slds-output"/><br/>
        Totalnumber of opprts:<ui:outputText value="{!v.oppcount}" title="Totalnumber of opprts"  class="slds-output"/><br/>
     </div>
</aura:component>
Controller:
({
    count : function(cpnt, event, helper) {
        var action = cpnt.get("c.getrecords");
        var accounts = cpnt.get("v.contactcount");
        var contacts = cpnt.get("v.Accountcount"); 
        var opprts = cpnt.get("v.oppcount");
        action.setParams({ 
                            "accounts": accounts,
                             "contacts": contacts ,
                              "opprts": opprts ,
                         });
        
    action.setCallback(this, function(a) {
           var state = a.getState();
            if (state === "SUCCESS") {
 
    alert("Records are counted sucessfully");
                           
            }
        });
    $A.enqueueAction(action);  

      
    }

})
Apex controller:
public class countRecords {
   /* @AuraEnabled public   integer accounts {get;set;} 
        @AuraEnabled public   integer contacts {get;set;}
        @AuraEnabled public  integer opprts {get;set;}*/
     @AuraEnabled
    Public static void getrecords(integer accounts,integer contacts, integer opprts){
        
        accounts = [select count() from Account];
        contacts = [select count() from contact];
        opprts = [select count() from Opportunitie__c];  
        system.debug('>>>>>>>>>>>'+accounts);
    }
    
}
Thanks in advance
Best Answer chosen by badri nath 9
Sandeep WaliaSandeep Walia
Hi Badree,

Here is the updated code:
Component
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,
                            force:lightningQuickAction" access="global" controller="countRecords" >
    <aura:attribute name="contactcount" type="integer" />
    <aura:attribute name="Accountcount" type="integer" />
    <aura:attribute name="oppcount" type="integer" />
    <div style = "slds">
        <lightning:button onclick="{!c.count}" title="to get the total number of records" class="slds-button-sucess"  label="count" variant="brand" iconName="utility:people" iconPosition="left" /><br/>
        
        Totalnumber of accounts:<ui:outputText value="{!v.Accountcount}" title="Totalnumber of accounts" class="slds-output"/><br/>
        Totalnumber of contacts:<ui:outputText value="{!v.contactcount}" title="Totalnumber of contacts" class="slds-output"/><br/>
        Totalnumber of opprts:<ui:outputText value="{!v.oppcount}" title="Totalnumber of opprts"  class="slds-output"/><br/>
    </div>
</aura:component>

JS Controller:
({
    count : function(cpnt, event, helper) {
        action = cpnt.get("c.getrecords");
        debugger;
        action.setCallback(this, function(a) {
            var state = a.getState();
            if (state === "SUCCESS") {
                var counts = a.getReturnValue();
                debugger;
                cpnt.set("v.Accountcount",counts[0]);  
                cpnt.set("v.contactcount",counts[1]);  
                cpnt.set("v.oppcount",counts[2]);  
            }
        });
        $A.enqueueAction(action);
    },
})

Apex Class
public class countRecords {
    @AuraEnabled
    Public static List<Integer> getrecords(){
        List<Integer> Counts = new List<Integer>();
        Counts.add([select count() from Account]);
        Counts.add([select count() from contact]);
        Counts.add([select count() from Opportunity]);  //Change to your custom opportunity object if needed
        return Counts;
    }
}

And here is the output after click of the count button:
User-added image

I advise you to use wrapper instead of a list for more complex return values​.

Hope this would have helped you,
Sandeep

All Answers

Sandeep WaliaSandeep Walia
Hi Badri,

Below code will work for your case:
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,
force:lightningQuickAction" access="global" controller="countRecords" >
    <aura:attribute name="contactcount" type="integer" />
     <aura:attribute name="Accountcount" type="integer" />
    <aura:attribute name="oppcount" type="integer" />
    <div style = "slds">
        <lightning:button onclick="{!c.count}" title="to get the total number of records" class="slds-button-sucess"  label="count" variant="brand" iconName="utility:people" iconPosition="left" /><br/>

        Totalnumber of accounts:<ui:outputText value="{!v.contactcount}" title="Totalnumber of accounts" class="slds-output"/><br/>
        Totalnumber of contacts:<ui:outputText value="{!v.Accountcount}" title="Totalnumber of contacts" class="slds-output"/><br/>
        Totalnumber of opprts:<ui:outputText value="{!v.oppcount}" title="Totalnumber of opprts"  class="slds-output"/><br/>
     </div>
</aura:component>

({
    count : function(cpnt, event, helper) {
        
    action.setCallback(this, function(a) {
           var state = a.getState();
            if (state === "SUCCESS") {
				var counts = a.getReturnValue();
				component.set("v.Accountcount",a[0]);  
				component.set("v.contactcount",a[1]);  
				component.set("v.oppcount",a[2]);  
            }
        });
    $A.enqueueAction(action);
    }

})

public class countRecords {
     @AuraEnabled
    Public static List<Integer> getrecords(){
        List<Integer> Counts = new List<Integer>();
        Counts[0] = [select count() from Account];
        Counts[1] = [select count() from contact];
        Counts[2] = [select count() from Opportunitie__c];  
		return Counts;
    }
}

I also suggest you to refer this link for lightning development in future:
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/intro_framework.htm  (Online)
https://resources.docs.salesforce.com/sfdc/pdf/lightning.pdf (Offline)

Hope this helps,
Sandeep
badri nath 9badri nath 9
Hi Sandeep Walia,

code you written above is not working fine , and you didn't call server side method(getrecords),the error shows below , 

c:cmpntcount$controller$count [action is not defined] Failing descriptor: {c:cmpntcount$controller$count}

hope you will get back with solution ,
Thanks
Sandeep WaliaSandeep Walia
Hi Badree,

Here is the updated code:
Component
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,
                            force:lightningQuickAction" access="global" controller="countRecords" >
    <aura:attribute name="contactcount" type="integer" />
    <aura:attribute name="Accountcount" type="integer" />
    <aura:attribute name="oppcount" type="integer" />
    <div style = "slds">
        <lightning:button onclick="{!c.count}" title="to get the total number of records" class="slds-button-sucess"  label="count" variant="brand" iconName="utility:people" iconPosition="left" /><br/>
        
        Totalnumber of accounts:<ui:outputText value="{!v.Accountcount}" title="Totalnumber of accounts" class="slds-output"/><br/>
        Totalnumber of contacts:<ui:outputText value="{!v.contactcount}" title="Totalnumber of contacts" class="slds-output"/><br/>
        Totalnumber of opprts:<ui:outputText value="{!v.oppcount}" title="Totalnumber of opprts"  class="slds-output"/><br/>
    </div>
</aura:component>

JS Controller:
({
    count : function(cpnt, event, helper) {
        action = cpnt.get("c.getrecords");
        debugger;
        action.setCallback(this, function(a) {
            var state = a.getState();
            if (state === "SUCCESS") {
                var counts = a.getReturnValue();
                debugger;
                cpnt.set("v.Accountcount",counts[0]);  
                cpnt.set("v.contactcount",counts[1]);  
                cpnt.set("v.oppcount",counts[2]);  
            }
        });
        $A.enqueueAction(action);
    },
})

Apex Class
public class countRecords {
    @AuraEnabled
    Public static List<Integer> getrecords(){
        List<Integer> Counts = new List<Integer>();
        Counts.add([select count() from Account]);
        Counts.add([select count() from contact]);
        Counts.add([select count() from Opportunity]);  //Change to your custom opportunity object if needed
        return Counts;
    }
}

And here is the output after click of the count button:
User-added image

I advise you to use wrapper instead of a list for more complex return values​.

Hope this would have helped you,
Sandeep
This was selected as the best answer
badri nath 9badri nath 9
hi Sandeep Walia,
above code working fine you forget to define variable for action(Line 19),

Thank you for your wonderfull Response,