user1698662
user1698662

Reputation: 31

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?

Upvotes: 3

Views: 8771

Answers (6)

Maira S
Maira S

Reputation: 47

We can do this with scheduled or map reduce script .First load file. which you can set to run according to your preferred schedule. Retrieve data of the existing file. Then you can create a new file with identical attributes, expect for the data field, which you can modify as needed. Finally, you can delete the old file to complete the process.

Below is sample code:

function onRequest(context)
{
    var oldfile=file.load({id:"12345});  //Replace with your file id
    var getData= oldfile.getcontents();
    var filename=oldfile.name;
    var filetype=oldfile.filType;
    file.delete({id:"12345"})//Replace with your file id.

    getdata+="this is changed data";

    var newdatafile=file.create({
    name:filename,
    fileType:filetype,
    contents:data
    });

newdatafile.folder="1111"   //Replace with your folder id
newdatafile.save();  //Save the new file
}

Upvotes: 0

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.

Upvotes: 0

Uma Kanth
Uma Kanth

Reputation: 5629

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
}

Upvotes: 3

Rohit Gupta
Rohit Gupta

Reputation: 593

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);

Upvotes: 0

thegeek
thegeek

Reputation: 93

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.

Upvotes: 3

user1864740
user1864740

Reputation: 31

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.

Upvotes: 1

Related Questions