Search code examples
netsuitesuitescriptsuitescript2.0

Suitescript 2.0 get the contents of a file from a Suitelet file field and use the data in a scheduled script


I have created a button on the work order records that opens a suitelet form with a fieldType.FILE to allow the user to choose a file locally. When the Suitlet is submitted I want the script to take the file, take the contents, and then pass two arrays to a scheduled script. So far I have no errors and none of my log.debugs are showing up in Netsuite.

Here is the suitelet code for reference:

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 * 
 * @author MF
 */

define(['N/ui/serverWidget', 'N/file', 'N/search', 'N/email','N/ui/dialog'], function (serverWidget, file, search, email, dialog) {
    function onRequest(context) {
        //getWoData(context);
        buildPage(context);
    };
    function buildPage(context) {

        var woForm = serverWidget.createForm({
            title: "Mass Update Work Order Due Dates",
        })
        woForm.addField({
            id: 'custpage_wo_export',
            label: "Work Order Update File",
            type: serverWidget.FieldType.FILE
        });
        woForm.addSubmitButton({
            id: 'custpage_submitwo',
            label: 'Submit',
            functionName: 'SubmitWorkOrders'
        });
        woForm.clientScriptModulePath = "SuiteScripts/WoSubmitClient.js"
        context.response.writePage(woForm);
    }

return {
        onRequest: onRequest
    };
});

The issue starts at the client as far as I know. Here is the client script for the Suitelet above:

/**
 * @NApiVersion 2.X
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 * @author MF
 */
define(['N/url', 'N/https','N/currentRecord'], function (url, https, currentRecord) {
    function pageInit(context) { };

    function SubmitWorkOrders(context) {
       var objRecord = currentRecord.get();
        //Handle the save button
        log.debug({
            title: 'SubmitWorkOrders',
            details: "This function was called"
        })
        var uploadedFile = objRecord.getValue({
            fieldId: 'custpage_wo_export'
        })
        log.debug({
            title: 'File',
            details: uploadedFile.name
        })
        var taskCreate = url.resolveScript({
            scriptId: 'customscript_scheduledscripttaskagent',
            deploymentId: 'customdeploy_scheduledscripttaskagent',
            returnExternalUrl: false,
            params: {
                input_file: uploadedFile
            }

        });
    }


    return {
        pageInit: pageInit,
        saveRecord: SubmitWorkOrders
    };


});

This client script then calls another suitelet to create and run the task.

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 * 
 * @author MF
 */

define([
    'N/task',
    'N/redirect',
    'N/file'
], function (task, redirect, file) {
    function scriptTask(context) {

        //check file type
        var input_file = context.request.params.input_file;
        log.debug({
            title: 'File Name',
            details: input_file.name
        })
        var woUpdateScriptTask = task.create({
            taskType: task.taskType.SCHEDULED_SCRIPT,
            deploymentId: 'customscript_womassupdate',
            params: { uploadedFile: uploadedFile },
            scriptId: 715
        }).submit();
    }
    return {
        onRequest: scriptTask
    };
});

Any advice would be appreciated, I have only been in the Netsuite realm for a few weeks and javascript in general only a bit longer than that. Note that there is some residual modules in some of these scripts from testing different approaches.

Currently the entire process looks something like this -> (1)UserEventScript on Work Orders -> (2)ClientScript on Work Orders -> (3)First attached Suitelet -> (4)ClientScript for Suitelet -> (5)Suitelet to create the task to run the scheduled script -> (6)ScheduledScript to update work orders. Looking to get some advice on getting the file contents from step 3 to step 5 so the data could get parsed before they are passed to the scheduled script.

Thanks in advance


Solution

  • I think this is how you can get the file contents from your first Suitelet to your Scheduled script:

    First, you need to combine your 1st and 2nd Suitelet into one single Suitelet. This is required for getting selected file. You can simply do it like this in your first Suitelet > function onRequest():

    function onRequest(context) {
        if (request.method == 'GET') { //This block will execute when you open your Suitelet form
            buildPage(context);
        }else{ //This block will execute when Submit button is clicked
            scriptTask(context); // add scriptTask() function body in your 1st Suitelet from 2nd Suitelet
        }
    };
    

    Second, if your client script is attached with a Suitelet you can view it's logs using console.log instead of log.debug in web browser's console (open with Ctrl+Shift+i). BUT getting value of 'custpage_wo_export' in your CS will only return a string like 'C:\fakepath\your_file_name.file_extension'. You might need to first save selected file in NetSuite's File Cabinet to access it's contents. This can be done in function scriptTask() now in your 1st Suitelet.

    In Client Script > function SubmitWorkOrders(), use:

    function SubmitWorkOrders(context) {
        var objRecord = context.currentRecord;
        var uploadedFile = objRecord.getValue({
            fieldId: 'custpage_wo_export'
        })
        console.log('File', uploadedFile);
    
        return true; //use return 'false' to view the log in console first
    }
    

    Now Third, in function scriptTask(), now in 1st Suitelet, use:

    function scriptTask(context) {
        if (context.request.files.custpage_wo_export) {
            var fileObj = context.request.files.custpage_wo_export; //save this fileObj and then access it's contents.
            log.debug('fileObj', fileObj);
            fileObj.folder = 1630; //speficy the id of folder in File Cabinet
            var fileId = fileObj.save();
            log.debug("fileId", fileId);
    
            //Now, load newly saved file and access it's contents. You can also access file contents in your Scheduled script depending on what you prefer/require.
            if (fileId) {
                var fileObj = file.load({
                    id: fileId
                });
                fileContents = fileObj.getContents();
                log.debug("fileContents", fileContents);
    
                /*
                    Now, you have fileId and/or fileContents. You can execute your Scheduled script here using task.create() etc.
                */
            }
    
        }
    }
    

    Note: Selecting and uploading/saving the same file in File Cabinet will simply overwrite the existing file keeping file id same.