Im currently using XmlNode and Document classes in apex to generate XML from sObject records. It works great for low volumes (up to 1000 records). However after that, I get CPU timeout. I can't see a way to process this data natively without hitting these limits. I have tried moving to Future and also standard best practise such as removing unnecessary fields from queries etc.. but no joy. Can anyone confirm if these classes scale well with CPU limits in mind? If not, do you know a better way to generate the XML? Perhaps off platform or using another class. Thanks

**Update - I was able to get batch apex working natively using string serialization, example here: How to build large single XML Document over multiple batches

enter image description here

enter image description here

  • 3
    Are you not able to employ middleware to perform the necessary data transformation?
    – Phil W
    Feb 4, 2022 at 10:51
  • 1
    Hi Phil, thanks for the reply. I had not considered middleware to be completely honest. I will do some research and see what comes back. I was hoping to do it natively but it seems like this will not be possible beyond a small amount of records.
    – Jay
    Feb 4, 2022 at 18:52
  • It would be helpful to understand your goals / real-world problem you're trying to solve, perhaps there's another way. (Please update your question rather than reply in comments)
    – identigral
    Feb 7, 2022 at 19:35

1 Answer 1


Any Apex code will fail because of governor limits if the volume of data is open ended.

Some problems can be solved in Apex if the data can be broken up into determinate sized chunks and worked on in those chunks. For internal processing asynchronous mechanisms such as Batch Apex can then be used. Or for APIs, the API can be designed to work in chunks.

Salesforce Functions might help you here where the processing logic is written in e.g. JavaScipt and runs in a less limited environment but still has good access to the Salesforce data. But note there are extra costs involved to use this; one figure I found Googling is $2k per month - no idea how correct that is.

But best you review why you are doing this first, and consider a broad range of options to solve the business problem before getting trapped in the technology problem.

  • Thanks Keith, very helpful. However, I rewrote it as a batch and it still gets CPU timeout :/
    – Jay
    Feb 4, 2022 at 16:19
  • @Jay Even with a small batch size? The word determinate is also important: limiting to say 100 parent records is no help in making the logic determinate if those records pull in a variable number of child records taht could be e.g. 10,000.
    – Keith C
    Feb 4, 2022 at 20:26
  • **update - Hi @Keith C. I have created a simple for loop with the XML creation. I have removed the need for all sObjects and process builders etc.. This is still failing. If I call addChildElement() once in the loop its fine, but when I add a few more it fails. To answer your question, one parent record would have 4 child nodes. There are 2000 parent records in this scenario. To me this seems like a fairly modest XML file but SF seems to struggle. Is there anything obvious in the code sample above that seems incorrect? Thanks
    – Jay
    Feb 7, 2022 at 11:14
  • I have also found that its not possible to build a single xml file over multiple batches. Only one xml per batch. The DOM document cannot be used as a stateful variable. I have tried to convert and parse via a stateful string and this has also failed. I may move this to a separate question since its getting further away from the original goal. @Keith C thanks again for your input.
    – Jay
    Feb 7, 2022 at 18:06
  • Note the example above can be solved using @future. However this does not solve my real world problem.
    – Jay
    Feb 7, 2022 at 18:07

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