I'm trying to parse an XML response from a web service.

The XML contains the information of the line items added to a quote as well as document data with the description of all line items. This document data is later used to create an attachment in salesforce. However, if the number of line items is very large(say more than 50), I'm hitting a Apex Heap Size Limit Exceeded issue while parsing the XML.

From the logs, I could find that the property "Data" in XML file contained the document data which needs to be used to create an attachment file in Salesforce. As the size of this property get increased, cumulative HEAP size is also getting increased.

Below is my code

activateResult is the response obtained from Web service

streamReader = new XmlStreamReader(activateResult);
    streamReader = moveToElement(streamReader, START_TAG_ACTIVATE_RESULT);
    streamReader = moveToNextTag(streamReader);
    Map<String, SOAPDtos.Property> contextDataProps = parseContextData(streamReader, new Set<String> {CUSTOM_DATA_ELEMENT});
    streamReader = moveToElement(streamReader, TARGET_ENTITY_ELEMENT);
    Map<String, SOAPDtos.Property> targetEntity = parseEntity(streamReader, PRINTED_DOCUMENT_ELEMENT);
    Map<String, SOAPDtos.RelatedObject> relatedObjects = new Map<String, SOAPDtos.RelatedObject>();
    Boolean isRelatedObject = true;
    while (isRelatedObject) {
        Map<String, SOAPDtos.Property> relProps = parseEntity(streamReader, PRINTED_DOCUMENT_ELEMENT);
        if (relProps == null) {
            isRelatedObject = false;
        } else {
            Map<String, SOAPDtos.Property> relContextData = parseContextData(streamReader, new Set<String> {RELATED_ENTITY_ELEMENT, PRINTED_DOCUMENT_ELEMENT});
            SOAPDtos.RelatedObject relatedObject = new SOAPDtos.RelatedObject(readPropValue(relProps, 'positionnumber'), readPropValue(relContextData, 'id'), relProps);
            relatedObjects.put(relatedObject.name, relatedObject);
            // In case the parsing hits the documents all related entities have been parsed and the loop can stop --> If just for safety reason
            if (streamReader.getLocalName() == PRINTED_DOCUMENT_ELEMENT) {
                isRelatedObject = false;
    // Move to the first tag of the printed document
    streamReader = moveToNextTag(streamReader);
    // Parse document, stop at export format document
    Map<String, SOAPDtos.Property> printedDocument = parseContextData(streamReader, new Set<String> {EXPORT_FORMAT_ELEMENT});
    // Move to first tag of export format document
    streamReader = moveToNextTag(streamReader);
    // Parse export format
    Map<String, SOAPDtos.Property> exportFormat = parseContextData(streamReader, new Set<String>());
    SOAPDtos.ActivationAttachment printedDocumentAttachment = new SOAPDtos.ActivationAttachment(readPropValue(printedDocument, 'filename'), readPropValue(printedDocument, 'mimetype'), 
                                                                                                readPropValue(printedDocument, 'desc'), readPropValue(printedDocument, 'data'));
    SOAPDtos.ActivationAttachment exportDocumentAttachment = new SOAPDtos.ActivationAttachment(readPropValue(exportFormat, 'filename'), readPropValue(exportFormat, 'mimetype'), 
                                                                                                readPropValue(exportFormat, 'desc'), readPropValue(exportFormat, 'data'));
  • 1
    I don't thing, that someone will look into those numbers of custom classes. I have few suggestions how can you figure out with heap size limit. 1) separate concerns of parsing and attachment creation. 2) Do second in separate transaction. Queueable would be useful as async transaction has 2x heap size limit – Oleksandr Berehovskyi Nov 22 '17 at 10:24
  • @AlexanderBerehovskiy. Thanks for your input. However, the size of 'activateResult' which is the complete XML obtained from webservice becomes pretty huge when there are large number of line items. As this is stored as a single string for later parsing, the heap size gets affected a lot. – Vishnu J Menon Nov 23 '17 at 3:10

Your Answer

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

Browse other questions tagged or ask your own question.