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
Abhishek Sharma 527Abhishek Sharma 527 

make the method Batchable

Hello Experts, I have a normal method which I want to make it Batchable as it should be executed on given time. It's the first time to work with Batch apex. Can anyone please guide how to implement this.
public static void firebaseDashboardData(){
        try {
           Map<Id,Integer> sessionCountMap= TBT_EventQuerySelector.getSessionCount();
           Map<Id,Integer> activeSessionCountMap=TBT_EventQuerySelector.getActiveSessionCount();
           List<DashboardWrapper> dashboardDataList=new  List<DashboardWrapper>();
           Set<Id> keySet = sessionCountMap.keySet();
           for(Id key : keySet){
               if(activeSessionCountMap.containsKey(key)) {
               DashboardWrapper data=new DashboardWrapper();
               data.userId=key ;
               data.sessionCount=sessionCountMap.get(key );
               data.activeSessionCount=activeSessionCountMap.get(key );
               dashboardDataList.add(data);
               }
               
           }
            
            String body='{';
            Http http = new Http();
            HttpRequest request = new HttpRequest();
            request.setEndpoint('https://test/UserDashboard.json');
            request.setMethod('PATCH');
            
            for(DashboardWrapper dW: dashboardDataList)
            {
                body=body+'"'+dw.userId+'" :'+JSON.serialize(dw)+',';
            }
            body=body.removeEnd(',');
            body=body+'}';
            request.setBody(body);
            HttpResponse response = http.send(request);
            // If the request is successful, parse the JSON response.
            if(response.getStatusCode() == 200) {
                // Deserialize the JSON string into collections of primitive data types.
                Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
                System.debug(results);
                JSON.deserializeUntyped(response.getBody());
                //     System.debug(results);
            }
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
    }

Any help would be highly appreciated.
Best Answer chosen by Abhishek Sharma 527
SubratSubrat (Salesforce Developers) 
Hello Abhishek ,

To make your method batchable, you need to implement the Database.Batchable interface and define the start, execute, and finish methods. Here's an example of how you can modify your code to make it batchable:
public class FirebaseDashboardBatch implements Database.Batchable<sObject>{
    
    public Database.QueryLocator start(Database.BatchableContext context) {
        // Use SOQL query to get the records to process
        return Database.getQueryLocator('SELECT Id FROM User');
    }
    
    public void execute(Database.BatchableContext context, List<sObject> scope) {
        // Convert the sObjects to User records
        List<User> users = (List<User>) scope;
        Map<Id,Integer> sessionCountMap= TBT_EventQuerySelector.getSessionCount(users);
        Map<Id,Integer> activeSessionCountMap=TBT_EventQuerySelector.getActiveSessionCount(users);
        List<DashboardWrapper> dashboardDataList=new  List<DashboardWrapper>();
        Set<Id> keySet = sessionCountMap.keySet();
        for(Id key : keySet){
            if(activeSessionCountMap.containsKey(key)) {
                DashboardWrapper data=new DashboardWrapper();
                data.userId=key ;
                data.sessionCount=sessionCountMap.get(key );
                data.activeSessionCount=activeSessionCountMap.get(key );
                dashboardDataList.add(data);
            }
        }
        
        String body='{';
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://test/UserDashboard.json');
        request.setMethod('PATCH');
        
        for(DashboardWrapper dW: dashboardDataList)
        {
            body=body+'"'+dW.userId+'" :'+JSON.serialize(dW)+',';
        }
        body=body.removeEnd(',');
        body=body+'}';
        request.setBody(body);
        HttpResponse response = http.send(request);
        // If the request is successful, parse the JSON response.
        if(response.getStatusCode() == 200) {
            // Deserialize the JSON string into collections of primitive data types.
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            System.debug(results);
            JSON.deserializeUntyped(response.getBody());
            //     System.debug(results);
        }
    }
    
    public void finish(Database.BatchableContext context) {
        // This method is called after all batches finish
    }
}
If the above information helps , please mark this as Best Answer.
Thank you.