0

I'm creating a REST endpoint in Salesforce in order to import a big set of contacts (2 million) and apply some custom logic to the insert (which I don't want in a trigger because it's a one-time thing).

So, the issue I'm concerned with is that heap size might be surpassed. Obviously, we're not going to send the 2 million contacts in the same JSON, we will chunk them down in batches of 200 so the endpoint can process them comfortably, but I would still like to add some validation.

I would like to basically be able to check the JSON's size before starting the deserialization, so if it's too big, I can return a relevant response warning the user, or simply denying the batch.

Is this possible? I've checked out the JSONParser class, but I haven't seen anything that might be of use, so that would leave me to the JSON.deserializeUntyped(jsonString) option.

Any help is appreciated.

1

You can check the answer in this post Calculate the data size of a given object for information about estimating object size. TLDR, you never know for sure what object size is going to be after deserialization. Counting the HttpRequest body size can be an estimate, but for sure not an exact value you can count on.

What you could do is to make sure you have enough space for storage of JSON in the HEAP, which is 6MB for synchronous and 12MB for asynchronous APEX.

Then use JSONParser iterate trough the JSON and deserialize step by step instead of deserializing whole JSON at the same time using JSON.deserialize(), which would effectively double the heap consumption.

You can also use Limits.getHeapSize() and Limits.getLimitHeapSize() to calculate free heap space while parsing.

0

You can check the response size before processing the response. You can do something like

HttpResponse res = http.send(req);
Integer Size = res.getBodyAsBlob().size();

And if this size is bigger you can simply send a email or like that to inform user to reduce chunk size.

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.