+ Start a Discussion
SFDC Forum 9SFDC Forum 9 

List value from another method

Here I am planning to use the list value from one method to another, but i am getting null value, how can i get it, please suggest ASAP

public class RPC 
{
@AuraEnabled public  static List<Contact> contlist {get;set;} 
@AuraEnabled 
    public static  getContact(Id firstcontId){
        
       
       List <Contact> contlist=[select Id, AccountID, Name, LastName, FirstName, Ind_ID__c,  from Contact LIMIT 5];
    
    }
    
    

@AuraEnabled public static String  getRefreshInfo()
{
   system.debug(contlist);     ---//It gives me null value, how can i get the contlist from getContact and use it here
}


}
Sachin HoodaSachin Hooda
Its because you haven't actually put anything in the list you declared globally, instead you created another list which will have scope within the inner method brackets.
Please try like this,
public class RPC 
{
@AuraEnabled public  static List<Contact> contlist {get;set;} 
@AuraEnabled 
    public static  getContact(Id firstcontId){
       contlist=[select Id, AccountID, Name, LastName, FirstName, Ind_ID__c  from Contact LIMIT 5];
    }
@AuraEnabled public static String  getRefreshInfo()
    {
   system.debug(contlist);  
   }
}
This way you'll be storing the list from the first method to the global list and it can be accessed in any other method.
 
SFDC Forum 9SFDC Forum 9
Hi Sachin, thaks for the respone.

I tred suggested way, it did not help.please sugest to analyse this further
Sachin HoodaSachin Hooda
How the other method is called?  From a component?
The global list is supposed to have the values until the class has executed successfully.
When you make a call to apex controller method from the component, they are being called from different instances. So the values you hold in the global list will be lost after the method have executed successfully. So you can't hold the values like that.
Instead you can return the list to your helper and store it in an attribute,
​​​next time when you need to get updated values from the database you make the call to the method again.
SFDC Forum 9SFDC Forum 9
Yes Sachin.
I am calling it from lighting js controller.

Whats happening is, as soon as component loads, through 'init', it will call 'someX'apex method and it will call some other methods internally and  in between this process, my list is used to query data.

My requirement is, when i click on lightnin button, will call some another js function which in turn call the another apex method, there I will have to use this same list.

I have used  some process as suggested  from the values of intial method (someX)(getting retun vaue and saving in attribute and stuff).


Sincce this conlist is used somewhere in between , i am not getting how to get it to helper.please suggest the syntax  or the way.


Thank you so much again
Sachin HoodaSachin Hooda
See, if refreshInfo method is called internally from getContact. And If there are records found based on your quyourand they will be stored in the global contlist variable. And this list will be accessible while the 2nd method is being executed and won't be null. 
And returning the list to your component is easy too.
You can refer here (https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_actions_call.htm) and if any problem occurrs post them here.