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
Sagar PatilSagar Patil 

Error: Compile Error: Variable does not exist: cont

Hello Developers,

I am writing small method in apex class which will use in lightning component. I am following lightning syntax still I am getting compile error. PFB the code snippet which throwing the error.
 
@AuraEnabled public Contact cont{get;set;} 
​@AuraEnabled
 public static id getDetails(){
     Id aa='sss';
     System.debug('caseObjh.Order:::>'+cont.Order_Num__c);
     return aa;
  }
I have referred below link still don't have any clue on this error.
https://salesforce.stackexchange.com/questions/172265/can-we-access-auraenabled-variables-in-auraenabled-methods?rq=1
 
Can you please help me.  Thanks

Regards,
Sagar
 
Best Answer chosen by Sagar Patil
Nayana KNayana K
Only way is to pass the attributes into the method. If you have 5 variables to maintain a state, then create a wrapper classe instance to bind them and pass it was one attibute.

https://salesforce.stackexchange.com/questions/172265/can-we-access-auraenabled-variables-in-auraenabled-methods

As per thin link, 
public class Payload { @AuraEnabled public Integer counter; }

is the wrapper class. If you want one more like contact, add it inside wrapper. 

I am not sure how best to expain this from my side. But this link has an explanation already.

All Answers

Nayana KNayana K
If you declare the variable as static like @AuraEnabled public static Contact cont{get;set;}, it will save without error. But, it is of no use.

Static variables will be reset between each transaction. So, you will get cont as null in the method.

Pass the contact instance every time in the method and perform necessary logic:
 
@AuraEnabled
 public static id getDetails(Contact cont){
     Id aa='sss';
     System.debug('caseObjh.Order:::>'+cont.Order_Num__c);
     return aa;
  }
 
doSomething: function(component, event, helper) {
    var action = component.get("c.getDetails");
    action.setParams({ cont: component.get("v.contact"); });
    action.setCallback(function(result) {
        // Not included, but remember to check isValid/result status
        component.set("v.conId", result.getReturnValue());
    });
    $A.enqueueAction(action);
}




 
Sagar PatilSagar Patil
Hi Nayana,

Thanks for your solution.

If there are multiple variables like @AuraEnabled public Contact cont{get;set;} present in apex class then how we can deal with them. As it won't be good practice to declare all of them within method parameters like public static id getDetails(Contact cont). Can you please let me know if you have any workaround for such scenario. 

Regards,
Sagar
Nayana KNayana K
Only way is to pass the attributes into the method. If you have 5 variables to maintain a state, then create a wrapper classe instance to bind them and pass it was one attibute.

https://salesforce.stackexchange.com/questions/172265/can-we-access-auraenabled-variables-in-auraenabled-methods

As per thin link, 
public class Payload { @AuraEnabled public Integer counter; }

is the wrapper class. If you want one more like contact, add it inside wrapper. 

I am not sure how best to expain this from my side. But this link has an explanation already.
This was selected as the best answer