I need to make 2 calls per day to API Rest and process the data. I have used “apex scheduler” to automate the 2 calls a day, then call to my apex class where I have all the logic to call the webservice and the object field mapping I store in a list, then I validate if there are records, if there is, I use the “apex queueable” in which it calls a batch apex to do the upsert with an external id. Sometimes I get these 2 errors:

  1. Apex CPU Time Limit Exceeded
  2. Apex heap size too large

The webservice returns around 100K records per day, that is, 50k records per call, and the custom object has approximately 30 fields.

The code will be somethig like that:

//execute Apex scheduler

//call future method.

    public static void testCallout(){
        //logic callout and validation
        List<customObject__c>cobj = new List<customObject__c>();
        for(Object res : response ){
            //more logic..,.
            new customObject__c(
            field1__c = mappingfield...,
            field2__c = mappingfield...
        //call queueable apex and after that, call batch apex inside queueable apex to upsert records

1 Answer 1


The best way to insert bulk data into a custom object is to use Batch Apex.

To use batch Apex, write an Apex class that implements the Salesforce-provided interface Database. Batchable and then invoke the class programmatically.

To monitor or stop the execution of the batch Apex job, from Setup, enter Apex Jobs in the Quick Find box, then select Apex Jobs.

The following blog would be really helpful to implement Batch Apex.



  • 1
    And to avoid running into governor limits, pay special attention to the optional parameter scope. Feb 14, 2022 at 9:06
  • Thanks for upvoting, please let know for further assistance
    – Amit Kumar
    Feb 14, 2022 at 12:47

You must log in to answer this question.

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