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
srikanth mannesrikanth manne 

can i pass the variables or list from one aura enabled method to another aura enabled method in same class?

srikanth mannesrikanth manne
Amit, I want to accesss one method variable or list  in some other method in (server side). Second method i want to access first method query data instead of writing same query in second method. is it possible to access?
Raj VakatiRaj Vakati
You no need to use any events .. the lightning component attributes will work 

Here the complete code

Apex class
 
public class DemoClass {
    public static String strMessage ='In Constructot';
    public DemoClass(){
        strMessage ='In Constructot';
    }    
    @AuraEnabled 
    public static String method1(){
        return  strMessage ='In Constructot 1 ';
    }
    
    @AuraEnabled 
    public static String method2(){
        return  strMessage ='In Constructot 2';
    }
    
}


Component 
 
<aura:component controller="DemoClass" >
    
    <aura:attribute name="str" type="String"/>

    <lightning:button label="calling method1" title="calling method1" onclick="{! c.handleClick1 }"/>
    <lightning:button label="calling method2" title="calling method2" onclick="{! c.handleClick2 }"/>
    
    {!v.str}
    
</aura:component>
 
({
    handleClick1 : function (component, event, helper) {
        var action = component.get("c.method1");
        action.setCallback(this, function(a) {
            component.set("v.str", a.getReturnValue());
        });
        $A.enqueueAction(action);
        
    },
    handleClick2 : function (component, event, helper) {
        
        var action = component.get("c.method2");
        action.setCallback(this, function(a) {
            component.set("v.str", a.getReturnValue());
        });
        $A.enqueueAction(action);
        
    }
})





Another way you can do it .. move the entire logic into the service class .. in service class you can refer the same variable from the different methods .. you no need to use static methods in service class ( use singleton pattern or any best practices )

 
Deepak_KumarDeepak_Kumar
Hi Srikanth,
If you are asking in the Apex class, then You can call AuraEnabled methods same as normal methods.
eg.
Public class DemoClass{
  @AuraEnbled
	public static void method1(){
		list<Contact> conlist = [SELECT Id FROM CONATCT LIMIT 100];
		DemoClass.method2(conlist);
		String myStr = 'TEST';
		DemoClass.method3(myStr);
	}
	@AuraEnbled
	public static void method2(list<Contact> conlist){
		System.debug(conlist);	
	}
	@AuraEnbled
	public static void method3(String str){
		System.debug(str);	
	}
}
Please mark as solved and also best answer if it will solve your problem.
Thanks.
 
Timmy AhluwaliaTimmy Ahluwalia
Hi,
 I used the Raj and Srikanth method to share the list with other method but i have no success, is it possible can you please share the code.
My case is that i am creating the list in one method and then i want to use the same list with other method, how can i acheive that.
Please share your expertise.
Thanks