[SalesForce] How to get values from a global declared map, inside a @future method

I'm facing an issue of accessing objects,
That is the first time I'm trying to use Global variables on APEX ;

I got a @future main method that calls two methods,
In the first method I would like to fill values for a Map I will use on the second Method, without returning it (already returning another Map) ;

My idea was to use a variable declared as global on the Class level, but it seems impossible because of the future context…
How could I do ? Returning the two maps, from the first method ?

I found that topic that match the context of my issue, no real answering however..

Class and global declared Map :

global with sharing class AP01_CrecheSiteWeb {
    global Map<Id, List<Account>> mapIdContratToFinalListCreches {get; set;}
    ...

The @futur method :

    @future
    public static void updateCrecheSiteWeb(List<Id> listCZDTIds) {

    // Call first method - I want it to set another map in order to access it in the 2nd method !
    Map<Id, List<Account>> mapIdContratToListAssociatedCreches = getMapContractToCreches(listCZDTIds);

    try {
        // Call second method, that should use the map set in the 1st method, and of course the map returned by the 1st method
        getMapContractToCrechesSiteWeb(mapIdContratToListAssociatedCreches);
    } catch (Exception ex){
        System.debug('--AP01_CrecheSiteWeb EXCEPTION --' + ex.getMessage());
    }
}

First method (parts of) :

public Map<Id, List<Account>> getMapContractToCreches(List<Id> listCZDTIds) {

      [...] 
     // I want to do something like this :

    Map<Id, List<Account>> getGlobalMap = this.mapIdContratToFinalListCreches;

     [...]

     return anotherMap;
 }

And the 2nd method would do the same, get the global Map, use it, clear it.

EDIT : so now, maybe I should open a new topic because it has to do with it but not about the @future context specifically (I can do it if asked to)

As I understood your answer, I could transfer some sobjects from one Method to another one by doing something like

Class testMap {
var A;

meth1(param A) {
    // do something about A
    // don't return A, but i's affected because of referencing passage
}

meth2(paramA) {
    // use A with the value it has after metho 1 is Called
}

}

But I'm unable to do it … Am I missing something ?

Best Answer

The advantage with OOP languages is that, all the object assignments are references by nature. So even when they are passed via parameters, change in one method will reflect in another method when same reference is passed as parameter. To help explain this better, consider below example:

@future
public static void myFuture(List<Id> accIds){
    Map<Id,List<Contact>> accountsToConsMap = new Map<Id,List<Contact>>();
    met1(accIds, accountsToConsMap);
    met2(accountsToConsMap);
}

public static void met1(List<Id> ids, Map<Id,List<Contact>> accMap){
    List<Account> accs = [SELECT Id, Name, (SELECT Name FROM Contacts) FROM Account];
    for(Account acc: accs) {
        accMap.put(acc.Id, acc.Contacts);
    }
}

public static void met2(Map<Id,List<Contact>> accMap){
    System.debug('contacts map => '+accMap);
}

Here accMap acts as reference to accountsToConsMap, so although you modify it in met1 method, its value will reflect in met2 method also.

You can implement your logic in same way. There is no need to return anything in 1st or 2nd methods. Just pass parameters which acts as references and consume/process them in any method.