1

Problem: I am trying to see if a file already exists in the file cabinet. If it does, the user gets a warning. If it does not, then the file proceeds to save. All components of these work except when I try to load the file I get the following error.

org.mozilla.javascript.EcmaError: TypeError: Cannot call method "load" of undefined (/SuiteScripts/suitelet_CheckIfFileExist.js#23)

I am currently passing a file id of a file I know exist for testing so I know that I am passing a valid file id.

ClientScript:

function checkIfFileExists(){
    CallforSuitelet_CheckIfFileExist();
    var result
    if(result == true){
        Ext.Msg.show({
        title:'Overwrite File?',
        msg: 'This existing file will be<br>overwritten:<br>' + msgBoxValue + '<br><br>Continue?',
                width: 300,
        icon: Ext.MessageBox.QUESTION,
        buttons: Ext.MessageBox.YESNO,
        fn: function(buttonId, value) {
            if (buttonId == 'yes') {
                CallforSuitelet();
                console.log('Yes pressed');
                } else {
                    Ext.Msg.show({
                    title:'Information',
                    msg: 'No files were copied.',
                    icon: Ext.MessageBox.INFO,
                    buttons: Ext.MessageBox.OK
                });
                    console.log('No pressed');
                    }
                    }
            });
        }
    }

 }
    
function CallforSuitelet_CheckIfFileExist(){
    console.log("CallforSuitelet_CheckIfFileExist function Entered")

var suiteletURL = url.resolveScript({
    scriptId:'customscript_suitelet_checkiffileexist',// script ID of your Suitelet 
    deploymentId: 'customdeploy_suitelet_checkiffileexist',//deployment ID of your Suitelet
    returnExternalURL: false,
    params: {
           'msgBoxValue':msgBoxValue
        }
    });
        console.log("suiteletURL = " + suiteletURL);

    https.get.promise({
            url: suiteletURL
            });
}

Suitelet:

   /**
    * @NApiVersion 2.x
    * @NScriptType Suitelet
    * @NModuleScope SameAccount
    */
   define(['N/file', 'N/log'],

   function(file, log) {

/**
 * Definition of the Suitelet script trigger point.
 *
 * @param {Object} context
 * @param {ServerRequest} context.request - Encapsulation of the incoming request
 * @param {ServerResponse} context.response - Encapsulation of the Suitelet response
 * @Since 2015.2
 */

function onRequest(context) {

    if (context.request.method === 'GET') {
        
        var requestParam = context.request.parameters;
        
        var fileName = 'Drag and Drop/Sales Order/' + requestParam.msgBoxValue + '.pdf';
        
        var contextResponse = 'true';
        
  
    function fileHasNoValue(fileId){
        if (typeof fileId == 'undefined'){
        return true;
        }
        if (fileId === null){
        return true;
        }
        if (fileId === ''){
        return true;
        }
    return false;
    }
    log.debug({details:'line beforefileOBj '})
    var fileObj = file.load(288784)
    //file.load({ <======= I have tried loading both ways
    //id: 288784
    //});

    try{
        var file = file.load({id: fileName});
        contextResponse = fileHasNoValue(file);
    } catch (e) {
        log.error({
        title: e.name,
        details: e.message});
        }
    };

  context.response.write(contextResponse)
  return;
}

return {
    onRequest: onRequest
};

});
adn
  • 49
  • 9

1 Answers1

1

Been there, done that.

By using the var keyword you are overshadowing the variable that references the file module. In Javascript the var keyword has function scope so what you are doing is equivalent to:

function(onContext){
    var file;
    if (context.request.method === 'GET') {
    ...
    file = file.load... // if you look at it this way it's obvious that file no longer has a reference to the file module. 
}

Either of these should work:

file = file.load({id: fileName}); // file refers to the file module until it gets reassigned. 
contextResponse = fileHasNoValue(file);

or

var myFile = file.load({id: fileId});
contextResponse = fileHasNoValue(myFile);
bknights
  • 14,408
  • 2
  • 18
  • 31