+ Start a Discussion
Abi V 4Abi V 4 

How to pass the list from one method to another method in controller.

I have the different methods in controller and it is fetching the user.name based  login details.
Controller is having the different methods and each method want to check the user.name details in where clause of soql.
Please let me know how to write a method and to hold the user.name details and use the same in all the methos where clause.
Best Answer chosen by Abi V 4
mritzimritzi
If you need to access any field in multiple methods in a class, its better to define it in the class, outside any method.
That way one variable can be accessed by any number of methods.

Sample:
public class SampleClass{
	String usrNm;
	//constructor
	public SampleClass(){
		// initialize it once in the constructor, use it in any method
		usrNm = userinfo.getUserName();
	}
	public void method1(){
		if(usrNm=='sampleUser'){
			// do something
		}
	}
	public List<Contact> method2(){
		if(usrNm=='randomUser'){
			// do something
		}
	}
	// and so on...
}

Mark this as Best Answer, if this helps solve your problem.

All Answers

KeshabKeshab
Abi.

There are many ways to achive this. but i will sugeet you to add a Parameter in your controller method "String CurrentUserName".

When you call that method , pass Userinfo.getUserName()  for  paramter "CurrentUserName".


Example :
public with sharing class PassParameterClass {


 Public void callMethod() {
    String userName=userinfo.getUserName();
    QueryMethod(userName);
   QueryMethod2(userName);
  }

  public void QueryMethod(String  CurrentUserName) { 
    List<COntact> conList= new List<COntact> ();
    try {
      conList=[select id ,firstname from Contact where owner.username=:CurrentUserName];
    } catch (Exception e) {
      ApexPages.addMessages(e);
      message = 'Whoops! An error occurred -- ' + e.getMessage();      
    }
  }

public void QueryMethod2(String  CurrentUserName) { 
    List<Account> accList= new List<Account> ();
    try {
      accList=[select id ,name from Account where owner.username=:CurrentUserName];
    } catch (Exception e) {
      ApexPages.addMessages(e);
      message = 'Whoops! An error occurred -- ' + e.getMessage();      
    }
  }

}
 
Abi V 4Abi V 4
Thanks for your reply!!
But in my case QueryMethod and QueryMethod2 returing list values to vf page and diaplying in table .So is it the right way to do?
mritzimritzi
If you need to access any field in multiple methods in a class, its better to define it in the class, outside any method.
That way one variable can be accessed by any number of methods.

Sample:
public class SampleClass{
	String usrNm;
	//constructor
	public SampleClass(){
		// initialize it once in the constructor, use it in any method
		usrNm = userinfo.getUserName();
	}
	public void method1(){
		if(usrNm=='sampleUser'){
			// do something
		}
	}
	public List<Contact> method2(){
		if(usrNm=='randomUser'){
			// do something
		}
	}
	// and so on...
}

Mark this as Best Answer, if this helps solve your problem.
This was selected as the best answer
Abi V 4Abi V 4
Thnak you ,working fine