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
Phuc 2Phuc 2 

Share Map between classes

Hell all,
I am trying to figure out how to share a map between my main Class and a utility Class.

Main Class
global void Update( SObject oldSo, SObject newSo ){ 

public static Map<Id, SObject> updateMap = new Map<Id, SObject>();

record__c newRecord= ( record__c ) newSo;
newAccount = newRecord.Account

utilClass.updateRecord(newRecord, newAccount)
}

if( !updateMap.isEmpty() && Trigger.isUpdate && Trigger.isAfter ){
       List<SObject> updateList = updateMap.values();
       updateList.sort();
       List<Database.SaveResult> results = Database.update( updateList, false);
}


Util Class
public void updateRecord(record__c  newRecord, Account newAccount){
        if(newAccount != null && newRecord != null){
            newAccount.isAcount =  true;
            updateMap.put(newAccount.Id, newAccount);
        }
        return updateMap;
    }

What is the best option for passing the updateMap from the util class back to the main class?  Any help is appreciated.

Cheers,
P
Shiraz HodaShiraz Hoda
Hi Phuc,

Try below code. If it solves your issue kindly mark it as best answer.
 
global void Update( SObject oldSo, SObject newSo ){ 

public static Map<Id, SObject> updateMap = new Map<Id, SObject>();

record__c newRecord= ( record__c ) newSo;
newAccount = newRecord.Account

updateMap = utilClass.updateRecord(newRecord, newAccount)
}

if( !updateMap.isEmpty() && Trigger.isUpdate && Trigger.isAfter ){
       List<SObject> updateList = updateMap.values();
       updateList.sort();
       List<Database.SaveResult> results = Database.update( updateList, false);
}

=======================
Utility class
public Map<Id,Account> updateRecord(record__c  newRecord, Account newAccount){
Map<Id,Account> updateMap = new Map<Id,Account>();         
if(newAccount != null && newRecord != null){             
newAccount.isAcount =  true;             
updateMap.put(newAccount.Id, newAccount);         
}         
return updateMap;     
}