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
Aleksandar AleksandarAleksandar Aleksandar 

Accessing Apex properties in Lightning Components

Hi all,
I am trying to build lightning components and I want to access the properties of an Apex controller in the components to display certain data. When try to display the values like in Visualforce (with added @AuraEnabled), it does not work. When I place the properties inside an inner class and create a method that has a return type as that inner class, then when I return a list of that class I can see the data. Bellow is the code which displays data, but I want to use it without an inner class and extra method to get that data. If anybody can help please let me know.

Thanks!

Apex:
public with sharing class EmployeeAvailability {
public static empAvailableInnerclass innerCList;
 
public class empAvailableInnerClass {
        @AuraEnabled
        public Decimal aetPerc {get;set;}
        @AuraEnabled
        public Integer totalEmployeesToday {get;set;}
        @AuraEnabled
        public Decimal budgetThisMonth {get;set;}
        @AuraEnabled
        public Decimal averageMargin {get;set;}
        @AuraEnabled
        public Object averageRate {get;set;}
        @AuraEnabled
        public Integer illnessToday {get;set;}
        @AuraEnabled
        public List<availableNextSixty> AvailableSixty{get;set;}
        @AuraEnabled
        public Date DateAfterTwoMonths {get;set;}

        public empAvailableInnerclass() {
            DateAfterTwoMonths=Date.today().addmonths(2);
        }
   }

@AuraEnabled
    public static empAvailableInnerclass getEmployeesDailyStatus(){
        runRetrieve();
        return innerCList;
    }
}

JavaScript controller:
getFixedTable : function(cmp, event, helper) {
        //debugger;
        var table = cmp.get("c.getEmployeesDailyStatus");
        table.setCallback(this,function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                cmp.set("v.tabTable",response.getReturnValue());
            } else if (state === "ERROR") {
                alert('Error : ' + JSON.stringify(errors));
            }
        });
        $A.enqueueAction(table);
    }


Lightning Component:
<aura:handler name="init" value="{!this}" action="{!c.getFixedTable}"/>
<aura:attribute name="tabTable" type="EmployeeAvailabilityLightning"/>

<table id="fixedTab">
            <thead>
                <tr>
                    <th>Total employees today : {!v.tabTable.totalEmployeesToday}</th>
                    <th>Budget this month : {!v.tabTable.budgetThisMonth}</th>
                    <th>Average Margin : {!v.tabTable.averageMargin} %</th>
                    <th>Available next 60 days : {!v.tabTable.AvailableSixty.size}</th>
                    <th>Employee hours available today : {!v.tabTable.aetPerc} %</th>
                    <th>Illness Today: {!v.tabTable.illnessToday}</th>
                </tr>
            </thead>
        </table>
sachin kadian 5sachin kadian 5
are you declaring your variables static while using without inner class?
Aleksandar AleksandarAleksandar Aleksandar
Yes but it still does not show anything
bakul.patelbakul.patel
I may be wrong, but I think using apex controller variable is against core design rules of Lightning. I believe lightning framework is supposed to maintain state on client side instead of on server. The server side should be used only for CRUD operations and business logic.

May be what you can try is, create a new method in apex controller that returns instance of a wrapper apex class which ecompases all the data you need in individual variables.
Aleksandar AleksandarAleksandar Aleksandar
I believe this is what I am already doing or I don't know the difference between a wrapper class and an inner class. But what I was wondering is how to access the Apex properties or variables without the need of an inner/wrapper classes. I thought there must be a way but I was not able to find any so far. I need to know it because I am migrating Visualforce pages to Lightning Components. The Visualforce pages use properties to access the data so if there is a way to keep the logic and just add @AuraEnabled would save me time.
Akhil KulkarniAkhil Kulkarni
http://bobbuzzard.blogspot.in/2015/07/lightning-components-and-custom-apex.html
sfdcMonkey.comsfdcMonkey.com
check it for access apex class property in salesforce lightning component
http://www.sfdcmonkey.com/2016/11/17/access-apex-class-property-lightning-component/
Prasad N RPrasad N R
@{!Piyush_soni__c} Blantant self promotion. Coding conventions have not been followed. Explanations are not there. It works for copy paste. But, it is not easy for learners. Not sure why getters, setters, aura iterators etc. are in one page. If I were to have known this, I would have written codes to pass simple text from component to apex controller and vice versa.
Prasad N RPrasad N R
https://force-base.com/2016/12/12/learn-how-to-call-an-apex-class-from-a-lightning-component/ and https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_actions_call.htm turned out to be really helpful to me! :)