3

I am using third party service to get data using rest API. But sometimes it return more than 6 MB data. They don't have API to provide data in chunks. I am already using future method to increase heap size limit to 12 MB.

With below code, I am facing heap size error at the time of de-serialization of data. I have also emphasised that line where I am getting error.

@future (callout=true)
public static void CreateProjects(set<ID> setOfProjectID)
{
    HttpRequest request;
    HttpResponse response = new HttpResponse();
    Map<Id,Opportunity> mapOfIdAndProjectToCreate = new Map<Id,Opportunity>([SELECT Id, Name, projectId__c FROM Opportunity 
                                                    WHERE ID IN :setOfProjectID]);
    Integer customerId;
    List<Opportunity> lstOppToUpdate = new List<Opportunity>();
    if(!mapOfIdAndProjectToCreate.isEmpty())
    {
        for(Id OpportunityID : mapOfIdAndProjectToCreate.keySet())
        {
            Opportunity oppCreated = new Opportunity();
            oppCreated = mapOfIdAndProjectToCreate.get(OpportunityID);
            try
            {
                String projectId;
                String requestBody = createRequestBodyForProject(mapOfIdAndProjectToCreate.get(OpportunityID));
                HttpRequest req = CreateHttpRequest(ConstantCls.CreateProjectURL,requestBody,'POST');
                Http http = new Http();
                HTTPResponse resp = new HTTPResponse();
                if(!Test.IsRunningTest())
                {
                    resp = http.send(req);
                    System.debug('heap size after api call '+Limits.getHeapSize());
                }
                if(resp.getStatusCode() == 200)
                {
                    System.debug('heap size before deserializtion '+Limits.getHeapSize());
// Till now we have 6 MB data in HTTPResponse resp variable, but when we go for deserializing by fetching its body, it gives Heap size limit error. 
// It is giving error when we try to fetch its body, because it has already size of response of 6 MB + little bytes are occupied by others variable. and when we fetch its body, it exceeds to more than 12 MB.
// In execution of below line, I am getting Heap size limit error.
                    *Map<String, Object> mapKeyVal = (Map<String, Object>)System.JSON.deserializeUntyped(resp.getBody());*
System.debug('heap size after deserialization '+Limits.getHeapSize());
                    Map<String, Object> data = (Map<String, Object>)mapKeyVal.get('data');
                    projectId = String.valueOf(data.get('projectId'));
                    oppCreated.projectId__c = projectId;
                    lstOppToUpdate.add(oppCreated);
                }
            }
            Catch(Exception ex)
            {
                System.debug('Error In Sync - CreateProjects method Updated - '+ ex.getMessage() + ex.getStackTraceString());              
            }
        }
    }
    UPDATE lstOppToUpdate;
}
public static HttpRequest CreateHttpRequest(String apiUrl, String requestBody, String method)
{
    Map<String,String> apiConfigs = GetAllConfigSettings();   
    HttpRequest req = new HttpRequest();
    req.setEndpoint(apiUrl);
    req.setMethod(method);  
    req.setTimeout(120000);
    req.setHeader('Content-Type', 'application/json');  
    req.setHeader('Authorization', apiConfigs.get('Access Token'));  
    if(String.isNotEmpty(requestBody))
    {
        req.setBody(requestBody);    
    }
    return req;
}

0 Answers0