How do I pass a value from one method to another. I am using a public list with a {get; set;} but this seems not to be working. What I want to achieve is to loop trough the insert from the first method in the second method.

Code sample

public without sharing class addController {

    public List<Contact> contactList {get; set;}

public static void saveContacts(List<Contact> listContact){
    Insert listContact;
    contactList= listContact;

public static void createUser(){

    for(Contact con : contactList){
  ..   } 


1 Answer 1


The trick you're trying to use would work, except that a future method, being called in a separate transaction, won't remember the contactList from the first transaction. This is because the data is not serialized as part of calling the future method.

Instead, you have to use a parameter to pass the data in to the method:

// Note: this won't compile, demonstrative purposes only
@future public static void createUser(Contact[] contactList) {

However, as you've probably realized by now, future methods don't support this syntax (only primitives and collections of primitives are allowed), so you need to pass in the ID values you got from the first step:

@future public static void createUser(Set<Id> contactIds) {
  Contact[] contactList = [SELECT ... FROM Contact WHERE Id = :contactIds];
  for(Contact con: contactList) {

To get this collection efficiently, just create a Map from the results of your insert:

Insert listContact;
Map<Id, Contact> contactMap = new Map<Id, Contact>(listContact);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .