[SalesForce] Limit for quota ‘api.dw.io.FileWriter(File)’ exceeded. Limit is 0, actual is 1

I have written the following function in the helper library:

exports.createXML = function(ExportFile, XMLObj){

    var writer = new FileWriter(ExportFile, 'UTF-8');

    var xsw = new XMLStreamWriter(filewriter);

    xsw.writeCharacters(XMLObj);

    xsw.close();

    fileWriter.close();

}

[Here, ExportFile is an object of the class dw.io.File and XMLObj is an object of the class XML]

And I have written another code using class FileWriter, but I always encounter the error saying Limit for quota 'api.dw.io.FileWriter(File)' exceeded. Limit is 0, actual is 1. We are currently using the SFRA architecture.

I have gone through many mail threads and XChange questions and discussions on this topic, nothing seems to solve this issue.

I am trying to convert an old custom Workflow to new custom Job component.

What should I do to solve this issue?

Thanks in advance.

Best Answer

You cannot access a file for writing in a Storefront context.

I'd likely ask you first, "Is what you're doing absolutely necessary in a storefront context?" Then secondly, "Can you break your functionality into a two-step process whereby the file writing occurs as an asynchronous job that polls for new work every few minutes?"

The answer to the first question in this case I'd assume is: "I'm trying to test this functionality using a storefront controller because testing jobs can be problematic." If that were true, I'd tell you to deal with it because this quota is going to prevent you from getting your task done. Just do it as a standalone job w/ component and feed it what it needs. Maybe you put a file on the WebDAV and ensure it's there before running your job component.

If the answer is not what I've assumed above: You'll need to find a way to either store that payload as a Custom Object for later writing in a Job context or use a third party to store it either temporarily or permanently.

See quota documentation: https://documentation.b2c.commercecloud.salesforce.com/DOC2/topic/com.demandware.dochelp/DWAPI/quota/html/API_Quotas.html

The reason for this quota is to prevent storefront threads being locked by system-level IO operations. It would be really easy to DDoS a site that had file uploads going right onto disk.

Note that you can upload files via HTTP Multipart MIME POST and they can be stored and you can operate on them. This mechanism should not be used to accept Import/Export XML as that is not secure.

Related Topic