0

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 ?

1 Answer 1

2

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.

9
  • 2
    +1, but the normal acronym for Object-Oriented Programming is "OOP", not "OOPS".
    – sfdcfox
    Commented Aug 2, 2019 at 13:39
  • As I understood your answer, I could transfer some sobjects from one Method to another one by doing something like Class testMap { var A; var B; 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 ? EDIT: whaaaa, ok I'll edit my question because it's not formatted there Commented Aug 7, 2019 at 15:53
  • 1
    Understood your confusion. Primitive data types are passed by value, so it will not reflect. but non-primitive types are passed by reference - in your link observe System.debug(c.price); Commented Aug 7, 2019 at 16:18
  • 1
    Yes I understood your code, and this link :) I think my code should work ... I'm passing 3 objects in my first method, I'm treating the three of them and only returning one, the debug says the list only on param, has been changed as I want it to ; I don't return it ; I pass it on the second method as a parameter, and its size is 0. It's a Map<Id, Account>... For me, it looks quite as you're doing in your exemple !! I'm lost there (but your code helped for concepts ;) ) Commented Aug 7, 2019 at 16:26
  • 1
    It's the new that was killing everything ! I still have to learn how to be in full controle on those points. But with your help and my brainstorming / searching, I learnt a lot :) Commented Aug 8, 2019 at 9:13

You must log in to answer this question.

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