0

I'm working on a integration project where I'm supposed to get millions of data by doing a callout to a REST API which I then need to insert in a custom object.

I'm having issues understanding 2 points

  • Will I be able to parse the JSON with JSON.createParser() and parser.readValueAs() normally as I do with other response data?
  • Most importantly, if I'm successful doing the above point, how should I approach inserting those data into Custom Object??

What I have already checked

  • I'm aware of Batch Job, but I'm unable to figure out how can I call it with a List (after parsing JSON response)

  • I did a bit of googling about Bulk API, however it seems I cannot call it from Apex Code.

Any pointers to the right direction is really appreciated.

0

You can parse the JSON using JSON.createParser() and parser.readValueAs() or you can also use JSON2Apex to create Apex class from your JSON. Based on your syntax it will give you a list in your code.

Then you can use Batch Iterator in Apex to pass this list in batch job and then Insert the data. Although if JSON size is very large you will still face other limits but once you reduce the JSON size this approach will work for you.

Here is a sample code to give you idea to use custom iterable.

public class MyBatchable implements Database.Batchable<Execution> {

    private class json2Apex {
        // Various data fields go here
    }

    public json2Apex[] start(Database.BatchableContext bc) {
        json2Apex[] js= new json2Apex[] {};
        // Logic that creates and adds to the list
        // Subject to normal governor limits
        return js;
    }

    public void execute(Database.BatchableContext bc, json2Apex[] jsapex) {
        for (json2Apex js : jspaex) {
            // Do something
        }
    }

    public void finish(Database.BatchableContext bc) {
    }
}
  • Thanks a lot. So this way I have to call Batch Class on a chunk of 200 Records manually? Isn't there any other automated way to do this? – Krishnandu Sarkar Apr 12 '18 at 11:51
  • @KrishnanduSarkar You need to pass the total list and execute the batch. Batch will divide them into chunk and will process them. – Tushar Sharma Apr 12 '18 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.