Friday, October 22, 2021

Queueable class implementation code

Queueable Class: 

public class ExecuteBatchQueueableJobUpdate implements Queueable {

    List<task> lstTasksToInsertAsynchronously = new List<task>();

   public boolean isTrue{set;get;}

    //Constructor

    public ExecuteBatchQueueableJobUpdate(List<task> lstTasks, boolean isInsert){

        lstTasksToInsertAsynchronously = lstTasks;

        isTrue = isInsert;

    }


    public void execute(QueueableContext context) {

        try{

            //Invoking batch job

            if(isInsert){

                BatchDMLClassForTasks.batchUpdateSObjects(lstTasksToInsertAsynchronously);

            }else{

                BatchDMLClassForTasks.batchUpdateSObjects(lstTasksToInsertAsynchronously);

            }

        }

        catch(Exception objEx){

            System.debug(LoggingLevel.ERROR,objEx.getStackTraceString() +'\n'+ objEx.getMessage());

        }

    }

}

Helper class to insert or update from Queueable:

public class BatchDMLClassForTasks {

    // insert sobjects

    public static void batchInsertSObjects(List<SObject> sObjs) {  

        if(!sObjs.isEmpty()) { 

            try {

                BatchInsertTasks batch = new BatchInsertTasks(sObjs, true);

                Database.executeBatch(batch, 150);

            } catch (Exception e) {

                System.debug('Error inserting SObjects');

            }

        } 

    }

    // update sobjects

    public static void batchUpdateSObjects(List<SObject> sObjs) {

        if(!sObjs.isEmpty()) {

            try {

                BatchInsertTasks batch = new BatchInsertTasks(sObjs, false);

                Database.executeBatch(batch, 130);

            } catch (Exception e) {

                System.debug('Error inserting SObjects');

            }

        } 

    }

}

Batch class:

public class BatchInsertTasks implements Database.Batchable<sObject> {

    private List<sObject> objectsToInsert = new List<sObject>();

    private boolean isInsertTasks;  

    public BatchInsertTasks(List<sObject> objects, boolean isInsert){

        this.objectsToInsert = objects;

        this.isInsertTasks = isInsert; 

    }

    public Iterable<sObject> start(Database.BatchableContext BC) {

        return this.objectsToInsert;

    }

    public void execute(Database.BatchableContext BC, List<task> scope){

        if(this.isInsertTasks){

            Database.insert(scope);

        }else{

            Database.update(scope);

        }

    }

    public void finish(Database.BatchableContext BC) {}

}


Reuse of Queueable class in another classes


 if(!lstTaks.isEmpty()){

     System.enqueueJob(new ExecuteBatchQueueableJob(lstTasksToInsertAsynchronously, true));

  }

Thursday, October 21, 2021

JSON Parsing and Rest Api Pagination

 public integer multifetchapi(integer totalcount){

        integer defCount = 50;

        integer pageNumber = 0;

        Test__c obj= [SELECT Access_Token__c,Refresh_Token__c from Test__c  where Name = 'testName'];

        string strAuth=obj.Access_Token__c;

        Http http= new Http();

        HttpRequest req= new HttpRequest();

        req.setMethod('GET'); 

        req.setEndpoint('https://test.com/companies');

        req.setHeader('Authorization', 'Bearer '+strAuth);

        HttpResponse strResp=http.send(req);

        system.debug('--strResp--->'+strResp.getBody());

         if(strResp.getStatusCode()==200){

            system.debug('--strResp1--->'+strResp.getBody());

             Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(strResp.getBody());             

             Map<String, Object> statusResults = (Map<String, Object>)(results.get('filtered_count_map'));

             system.debug('statusResults==>'+statusResults);

             totalcount = integer.valueOf(statusResults.get('all'));

             if(totalcount > defCount){

                 pageNumber = defCount/totalcount; 

             }

             system.debug('pageNumber==>'+pageNumber);

             

         }

        return pageNumber;

    }

for(integer i=0; i<filterCount; i++){

            string strEndPoint=zic.Endpoint_Url__c+'?page='+i;//+'?filters=updated:gt:'+updated

}


CSS Styling

Styling: 1. Inline Styling: < div style = "font-size: 2rem;font-weight: 400;" > Good Evening </ div > 2. Usi...