Problem with FDF save automation

I am working in Adobe Acrobat Pro DC on a Windows 10 PC.below is my code, the problem is that instead of automatically saving the FDF file to the same directory folder as the PDF Form, it opens the ‘save As’ dialog box for the user to navigate to the directory and save it manually.
Here is my code:
function exportFormDataToFDFFunction() {
try {
// Create a new FDF object
var fdf = this.exportAsFDF();

    // Retrieve form field values
    var fieldValues = {};
    var fields = this.numFields;
    for (var i = 0; i < fields; i++) {
        var field = this.getField(this.getNthFieldName(i));
        fieldValues[field.name] = field.value;
    }

    // Convert field values to FDF format
    var fdfContent = '';
    for (var fieldName in fieldValues) {
        fdfContent += '<< /T (' + fieldName + ') /V (' + fieldValues[fieldName] + ') >>\n';
    }
    fdfContent = '%FDF-1.2\n1 0 obj<</FDF<</Fields[' + fdfContent + ']>>/Type/Catalog>>\nendobj\ntrailer\n<< /Root 1 0 R>>\n%%EOF';

    // Get the path of the currently opened PDF form
    var pdfPath = this.path;
    console.println("PDF Path: " + pdfPath); // Debugging statement

    if (pdfPath) {
        // Extract the directory path
        var directoryPath = pdfPath.replace(/[^\/\\]*$/, ''); // Removes the file name from the path
        console.println("Directory Path: " + directoryPath); // Debugging statement
        
        // Specify the output file path and name
        var outputPath = directoryPath + "CheetSheet_Data.fdf";
        console.println("Output Path: " + outputPath); // Debugging statement

        // Write the FDF content using util object
        if (typeof util !== "undefined") {
            util.writeFile(outputPath, fdfContent);
            console.println("FDF File successfully saved."); // Debugging statement
        }
    } else {
        // If the PDF form is not saved yet, you can handle this case accordingly
        console.println("PDF form needs to be saved before exporting data.");
    }
} catch (error) {
    console.println("Error occurred: " + error); // Debugging statement
}

}

exportFormDataToFDFFunction(); // Call the function when the button is clicked

Hello, one possible problem i see is this:
} catch (error) {
console.println("Error occurred: " + error); // Debugging statement
} where does the curly brace come from?

What is util.writeFile I don’t seem to be able to find it. All I find are print methods on util.

Both exportAsFDF and saveAs takes a path parameter.

https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/doc.html?highlight=exportasfdf#exportasfdf

https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/doc.html?highlight=exportasfdf#saveas

I assume there are some security restrictions in place as well.

Thank you robheyays and lasjorg,
I’m a 60-year-old man trying to learn new Trix (ie. programming). I’m finding JavaScript to be quite messy and frustrating, it just doesn’t seem to make a lot of sense to me.
I think I’m going to look for an alternative solution for this problem, maybe I’ll try to tackle this with Python. either way, thanks to both of you.