'NetSuite SuiteScript to modify file in the file cabinet

We have files within the NetSuite file cabient which need to be updated (the url field has changed). I found the noted article on this site but there is no code example to perform the requested. It indicates to use the nlapiLoadFile and nlapiSubmitFile calls; would anyone be able to assist with a code example?

Link: Can Netsuite Suitescript modify a file in the file cabinet?



Solution 1:[1]

Ya, it seems a bit odd. The only way I found is:

  1. Load The File
  2. Create a file handle with:
    • Set the file name to one that you intended.
    • Set the content to intended one
  3. Set the folder and submit.

I have attached a code snippet


    var file = nlapiLoadFile(file_id);
    var content = file.getValue();
    content = '...put your content...';
    file = nlapiCreateFile(file.getName(), 'FILE TYPE', content);
    file.setFolder(required_folder_id);
    nlapiSubmitFile(file);

Hope this helps.

Solution 2:[2]

There is no special API function to edit an existing file, you could take the details of the existing file and create a new file with the same details but changing the data field only and deleting the old file.

var start = function(request, response)
{
    var fileId = "107524";//get the existing file id
    var file = nlapiLoadFile(fileId);
    var data = file.getValue();
    var name = file.getName();
    var folderId = file.getFolder();
    var fileType = file.getType();
    nlapiDeleteFile(fileId);//delete the older file

    data += ",this is the appended data";//change the data
    var newFile = nlapiCreateFile(name, fileType, data);//create a new file with the same details
    newFile.setFolder(folderId);
    nlapiSubmitFile(newFile);//submit it
}

Solution 3:[3]

Do you mean file instead of field? If you use nlapiLoadFile(/path/file), you can then use getURL() to provide a link to that file.

Solution 4:[4]

NetSuite does not have a edit file kind of an API. You will have to load the original file, modify the contents as per your needs and then submit that data by creating a new file with same file name and inside the same folder. This simply overrides the existing file.

Here's the code sample:

var original = nlapiLoadFile(FILE_ID_OR_FILE_PATH_IN_FILE_CABINET);
var originalContent = original.getValue(); //Return the value (base64 encoded for binary types) of the file

var updated = nlapiCreateFile(original.getName(), FILE_TYPE, UPDATED_FILE_CONTENTS);
updated.setFolder(original.getFolder());
nlapiSubmitFile(updated);

Solution 5:[5]

I experienced a similar error when trying to modify a file using SuiteScript in Netsuite at the server-side. I was using the way explained in the documentation, where they say copying a new file through file.copy() with conflictResolution: file.ConflictResolution.OVERWRITE. However, that way didn't work for me as it neither created the file nor overwrote it. Finally, I use the following form to get it working:

...            
let fileNew = file.create({
                name: 'same_name_of_the_original_file',
                fileType: file.Type.PLAINTEXT, // change it depending the file type you will create
                contents: credentials.body,
            });

fileNew.folder = folder_id;
let fileId = fileNew.save();
...

So, the key is to change the folder and saving it after the file is created. After that, saving the file would overwrite the original.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abdusalam Ben Haj
Solution 2 Uma Kanth
Solution 3 user1864740
Solution 4 Rohit Gupta
Solution 5 Roberto C. Rodriguez-Hidalgo