0

I have a JSON too large to be deserialize, and I'm getting the Exception System.LimitException: Apex heap size too large: 6127921.

I used groupeRame = (groupeRame) JSON.deserialize(jsonRequest, groupeRame.class), where groupeRame.class is a inner class wrapper.

I tried JSON2Apex but it did not work too.

Would anyone have an idea to correct this problem?

Thanks in advance.

2
  • defer the work to a queueable (limit will be 12MB and 60sec CPU)
    – cropredy
    Jul 23, 2019 at 23:28
  • Thanks how to process ? Jul 24, 2019 at 11:14

2 Answers 2

2

Transaction receiving the JSON (CPU limit = 10 sec, Heap = 6MB)

for example, an APEX REST service that has a method annotated @HttpPost

System.enqueueJob(new MyQueueable(jsonRequest));

Queueable outer (or inner) class (CPU limit = 60 sec, Heap = 12MB)

public class MyQueueable implements System.Queueable {

  String jsonBody;
  public MyQueueable(String jsonBody) {this.jsonBody = jsonBody;}

  public execute(QueueableContext qc) {

    MyApexType myType = (MyApexType) Json.deserialize(this.jsonBody,MyApexType.class);
    ... do something with deserialized myType
  }
}
4
  • Can use it in @HttpPost webservice static Retour importGestionPrioriteClient() { ? Jul 31, 2019 at 15:05
  • @SylvainCoppin - edited my answer
    – cropredy
    Jul 31, 2019 at 17:02
  • It's work but i can't have the return of the request, because execute is asynchronous. Aug 1, 2019 at 13:41
  • Well, you need to either get the poster to reduce the size of the Json or change the interface such that the reply is async. The SFDC limit is a hard limit.
    – cropredy
    Aug 1, 2019 at 13:59
1

I dont think its possible with apex as synchronous limit is 6mb. Instead can you try returning to front end (Lightning Aura component/LWC or page) and process it by using JSON.parse(jsonRequest)

However, if this is used just for database operations and not showing anything on UI, then put this logic in future or queueable methods. Async apex limits for heap is 12mb.

4
  • It is a webservice and I try to deserialyse the request body response : String jsonRequest = RestContext.request.requestBody.toString(); groupeRame = (groupeRame) JSON.deserialize(jsonRequest, groupeRame.class); Jul 23, 2019 at 14:06
  • are you saving anything to database in salesforce OR are you displaying in UI? Jul 23, 2019 at 14:09
  • After trying to deserialyze , yes I insert the data into an sObject Jul 23, 2019 at 14:12
  • And for information when I used JSON2Apex I have a CPU limit exceeded Exception Jul 23, 2019 at 14:42

You must log in to answer this question.

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