0

I have a normal apex class, but since the no of records returned from a certain query (highlighted in code) is exceeding governor limits, I wish to replace that with a batch job, which will calculate the results and store in a collection. I intent to use that collection for the remaining logic in my apex class. Is it possible?

Note: I also have a visualforce page which references this apex class to display data. The highlighted section is where my code says cpu timeout exceeded, but I believe the subsequent lines of code also need to be replaced and passed into the batch job.

global class Persona_Custom_Metadata_V4 {
    public Map<String, String> PersonaSetting {get;set;}
    public List<User> usrList {get;set;}
    public Map<ID, User> usrMap {get;set;}
    public List<PermissionSetAssignment> PermList {get;set;}
    public Map<Id,List<String>> mapPermList {get;set;}
    public List<string> StorePermSets {get;set;}
    public Set<String> PSSet {get;set;}
    public  List <integer> CountofPS {get;set;}
    public List <Persona_Test__mdt> PersonaMDT {get;set;}
    
    public Id[] userIdsMiss { get; set; }
    public Id[] userIdsAdd { get; set; }
    
    public Id[] userIdsMiss2 { get; set; }
    public Id[] userIdsAdd2 { get; set; }
    
    public Map<ID, User> usrMapPersona1 {get;set;}
    public Map<ID, User> usrMapPersona2 {get;set;}
    
    public Map<ID, Integer> CountofPSMap {get;set;}
    public Map<ID, List<String>> MissingUser {get;set;}
    public Map<ID, List<String>> AdditionalUser {get;set;}
    public List<String> MissingList {get;set;}
    public List<String> AdditionalList {get;set;}
    public List<String> CombinedList {get;set;}
    public Map<ID, List<String>> finalMissingMap {get;set;}
    public Map<ID, List<String>> finalAdditionalMap {get;set;}
    public Map<ID, List<String>> Persona1Missing {get;set;}
    public Map<ID, List<String>> Persona2Missing {get;set;}
    public Map<ID, List<String>> Persona1Additional {get;set;}
    public Map<ID, List<String>> Persona2Additional {get;set;}
    
    public Map<ID, List<String>> PersonaAdditionalTest {get;set;}
    public Map<ID, List<String>> PersonaMissingTest {get;set;}
    public String selectedRT{get;set;}
    public boolean pb2Rendered{get;set;}
    public boolean pb3Rendered{get;set;} 
    public boolean isExport{get; set;}
    
    public void getUserDetails()
    {
        usrMapPersona1 = new Map<ID, User>();
        usrMapPersona2 = new Map<ID, User>();
        
        userIdsMiss = new Id[0];
        userIdsAdd = new Id[0];
        
        userIdsMiss2 = new Id[0];
        userIdsAdd2 = new Id[0];

        Persona1Additional = new Map<ID, List<String>>();
        Persona2Additional = new Map<ID, List<String>>();
        Persona1Missing = new Map<ID, List<String>>();
        Persona2Missing = new Map<ID, List<String>>();
        PersonaSetting = new Map<String, String> ();
        usrList = new list<user>();
        usrMap = new Map<ID, User>();
        PermList = new list<PermissionSetAssignment>();
        mapPermList = new Map<Id,List<String>>();
        StorePermSets = new List<String>();
        PSSet = new Set<String>();
        CountofPS = new list<integer>();
        PersonaMDT = new list <Persona_Test__mdt>();
        CountofPSMap = new Map<ID, Integer>();
        MissingUser = new Map<ID, List<String>>();
        AdditionalUser = new Map<ID, List<String>>();
        MissingList = new List<String>();
        AdditionalList = new List<String>();
        CombinedList = new List<String>();
        finalMissingMap = new Map<ID, List<String>>();
        finalAdditionalMap = new Map<ID, List<String>>();
        
        for (Persona_Test__mdt PersonaMDTRecords : Persona_Test__mdt.getAll().values())
        {
            String PersonaPSset = PersonaMDTRecords.PS_Set__c.trim();
            PersonaSetting.put(PersonaMDTRecords.Identity_PS__c, PersonaPSset);
        }
        
        for (String str:PersonaSetting.keyset() )
        {
            PermList = new List<PermissionSetAssignment>();
            usrMap = new Map<ID, User>();
            usrList = new list<user>();
            mapPermList = new Map<Id,List<String>>();
            MissingUser = new Map<ID, List<String>>();
            AdditionalUser = new Map<ID, List<String>>();
            PersonaMDT = [SELECT Id, Count_of_Unique_PS__c FROM Persona_Test__mdt WHERE Identity_PS__c = :str ];
            Integer CountPS = Integer.valueOf(PersonaMDT.get(0).Count_of_Unique_PS__c);
            system.debug(CountPS);
            usrList = [SELECT Id, firstname, lastname, profile.name, division, department
                       FROM User
                       WHERE  Id IN (SELECT AssigneeId
                                     FROM PermissionSetAssignment
                                     WHERE PermissionSet.label = :str)] ;
            //system.debug(usrList);
            usrMap.putall(usrList);
            
            if (str == 'Test Chatter Free7')
            {
                usrMapPersona1 = usrMap;
            }
            
            if (str == 'Test Chatter Free8')
            {
                usrMapPersona2 = usrMap;
            }
            
            ****PermList =  [SELECT AssigneeId, PermissionSet.label
                         FROM PermissionSetAssignment WHERE AssigneeId = : usrMap.keySet()];****
            for(PermissionSetAssignment perm : PermList){
                if(mapPermList.containsKey(perm.AssigneeId)){
                    List<String> temp =  mapPermList.get(perm.AssigneeId);
                    temp.add(perm.PermissionSet.label);
                    
                    for (Integer i = (temp.size() - 1) ; i >= 0 ; i--){
                        String s = temp[i];
                        if(s.contains('00e1a000000MWaTAAW')){
                            temp.remove(i);
                        }
                    }
                    
                    mapPermList.put(perm.AssigneeId,temp);
                }
                else{
                    mapPermList.put(perm.AssigneeId,new List<String>{perm.PermissionSet.label});
                }
            }
            //system.debug(mapPermList);
            // to store the count of PS
            for (Id idvalue: mapPermList.keyset())
            {
                Integer count = 0;
                for (String s: mapPermList.get(idvalue))
                {
                    //system.debug(' Map keyset Id Value' + idValue );
                    //system.debug(' list values : '+ s);
                    count++;
                    CountofPSMap.put(idvalue, count);
                }

                if (count < CountPS)
                {
                    //String str1 = String.join( mapPermList.get(idvalue), ', ' );
                    
                    //string concatenate='User ID:'+ '' + idvalue + ' '+ '' + 'PS assigned:' + str + '\n';
                    MissingUser.put(idvalue, mapPermList.get(idvalue));
                    
                    //MissingList.add(concatenate);                    
                }
                else if (count > CountPS)
                {
                    String str2 = String.join( mapPermList.get(idvalue), ', ' );
                    
                    string concatenate2 = 'User ID:'+ '' + idvalue + ' '+ '' + 'PS assigned:' + str2 + '\n';
                    AdditionalUser.put(idvalue, mapPermList.get(idvalue));
                    //AdditionalList.add(concatenate2);
                }
            }
            
            if (str == 'Test Chatter Free7')
            {
                Persona1Missing = MissingUser;
                Persona1Additional = AdditionalUser;
                
                //PersonaAdditionalTest = Persona1Additional;
                //PersonaMissingTest = Persona1Missing;
                for (ID idmissing : Persona1Missing.keyset())
                {
                    userIdsMiss.add(idmissing);
                }
                
                for (ID idadd : Persona1Additional.keyset())
                {
                    userIdsAdd.add(idadd);
                }
            }
            else if (str == 'Test Chatter Free8')
            {
                Persona2Missing = MissingUser;
                Persona2Additional = AdditionalUser;
                for (ID idmissing2 : Persona2Missing.keyset())
                {
                    userIdsMiss2.add(idmissing2);
                }
                
                for (ID idadd2 : Persona2Additional.keyset())
                {
                    userIdsAdd2.add(idadd2);
                }
                
                Persona2Missing = MissingUser;
                Persona2Additional = AdditionalUser;
                
                // PersonaAdditionalTest = Persona2Additional;
                // PersonaMissingTest = Persona2Missing;
            }
            
            /*system.debug(Persona1Missing);
            system.debug(Persona2Missing);
            system.debug(Persona1Additional);
            system.debug(Persona2Additional);*/
            //system.debug(CountofPSMap);
            // system.debug('Missing PS User---->' + '\n' + MissingList);
            // system.debug('Additional PS User---->' + '\n' + AdditionalList);
            // CombinedList.addAll(MissingList);
            // CombinedList.addAll(AdditionalList);
            // return CombinedList;
            //finalMissingMap =  MissingUser;
            //finalAdditionalMap =  AdditionalUser;
            // finalList = CombinedList;
            // system.debug(finalMissingMap);
            //system.debug(finalAdditionalMap);
            
            //system.debug('Test---->' + '\n' + finalList);
            
            // MissingUser = new Map<ID, List<String>>();
            //AdditionalUser = new Map<ID, List<String>>();
            // system.debug(MissingUser);
        }
        
        if (selectedRT == 'Persona1')
        {
            pb2Rendered = true;
            pb3Rendered = false;
            //Persona1Missing = MissingUser;
            // Persona1Additional = AdditionalUser;
                        
            PersonaAdditionalTest = Persona1Additional;
            PersonaMissingTest = Persona1Missing;
        }
        else if (selectedRT == 'Persona2')
        {
            pb3Rendered = true;
            pb2Rendered = false;
            // Persona2Missing = MissingUser;
            // Persona2Additional = AdditionalUser;
            
            PersonaAdditionalTest = Persona2Additional;
            PersonaMissingTest = Persona2Missing;
        }
        else if (selectedRT == 'All')
        {
            pb3Rendered = true;
            pb2Rendered = true;
            // Persona2Missing = MissingUser;
            // Persona2Additional = AdditionalUser;
            
            PersonaAdditionalTest = Persona2Additional;
            PersonaMissingTest = Persona2Missing;
        }        
    }
    
    public void getExport() { isExport= true; }
}
2
  • 2
    just because you faced cpu limit is not a solution to go for batch. batch job will be asynchronous.if you want to use result in relatime in vf page its not possible. you should focus on working on cpu limit by optimizing code.if not possible there are various ways ways to reduce data manipulation in server like doing a pagination on vf page but it will be entirely depend on your case.only you have to check your use case and make correct decision. Commented Mar 4, 2023 at 16:38
  • 1
    Ignoring the actual question, but why is the class global and everything in it public? I suspect you do not need it global.
    – Phil W
    Commented Mar 4, 2023 at 17:27

1 Answer 1

0

The technical answer to your question is:

  • If the VF page launches a batch job, the job executes asynchronously
  • Thus, when the batch job finishes is indeterminate
  • As such, for the VF page to recognize the results of the batch job basically boil down to:
    • the batch job's finish() must save the results in a persistent store (i.e., the database as records)
    • the VF page must poll for the data (classically, done <apex:actionPoller>

The above is a complicated design and @gauravsharma comment is valid - look for ways to optimize the VF controller logic to not run into CPU limits

You must log in to answer this question.

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