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

Declared variable not working in @AuraEnabled controller

Hi All ,
Declared variable not working in @AuraEnabled controller 

below my sample code , how can we reuse declared variable "strusrfield "in mutiple methods 
getting  Variable does not exist: strusrfield   error 

Public class myusercls {
 @AuraEnabled public string strusrfield ; 
    public static list<String> getuserdetails(){ // this method calling from doinit 
          for( user objusr : [select id , Primary_Sales_Org__c from user where id=:UserInfo.getUserId() ]) {
              strusrfield = objusr.unicode__c;
        list<customobj__c> objcus =[select id unicode__c from customobj__c where unicode__c =:strusrfield ];
        return objcus ;

  public static list<String> getcustomretunx(){
        list<customobj__c> objcus =[select id unicode__c from customobj__c where unicode__c =:strusrfield ];
        return objcus ;


Sohaib Farooq22Sohaib Farooq22
You cannot reference your strusrfield variable as you do because it's a static context.
I managed to resolve this issue by declaring a class object and then setting its property : 
myusercls myObject = new myusercls();
myObject.strusrfield = 'whatever';
I hope this helps you.
Thanks @Sohaib Farooq22