Home > Mobile >  NetSuite - Using SuiteScript to Create Button to Print Packing List on Transfer Order
NetSuite - Using SuiteScript to Create Button to Print Packing List on Transfer Order

Time:05-13

I'm trying to follow SuiteAnswers 41269 (which is for adding a Packing List to an Item Fulfillment) to add a button to print a packing list to a Transfer Order. This is essentially a test to try and be able to print more forms on more transactions that aren't native to NetSuite (VRMAs, etc.).

Note that the SuiteAnswers above seems to be out of date, because it references SS1.0 and SS2.0, but some of the code for SS2.0 also appears to use SS1.0 code. Since that SA was a little off, I've gone through a lot of the NetSuite help files on the different parts of the render and url modules to help build out these scripts.

My issue is that I keep getting thrown back an error when the "Print 1" button is clicked:

Uncaught ReferenceError: print1 is not defined
    at trnfrord.nl?id=658266&whence=&cmid=1652279684591_1868:1132:30
    at Object.execCb (NsRequire.js:2047:26)
    at Ma.check (NsRequire.js:1193:28)
    at Ma.enable (NsRequire.js:1475:10)
    at Ma.init (NsRequire.js:1106:11)
    at NsRequire.js:1771:18

I'm using a UserEvent Script, a Client Script, and a Suitelet Script. I'm currently using a static entity ID of the Transfer Order while I'm testing this, and will add a dynamic one once I get this working.

UPDATE: After implementing the first suggestion, I am now receiving this error:

Uncaught TypeError: Cannot read properties of undefined (reading 'resolveScript')
    at Object.print1 (scriptmodule.nl?_xt=.js&childPath=/SuiteScripts/buttonTest2/print_client_script&parentPath=/SuiteScripts/buttonTest2/print_user_event_script.js&assignDefineIfEmpty=T:22:26)
    at trnfrord.nl?id=658266&whence=&cmid=1652279684591_1868:1132:533
    at Object.execCb (NsRequire.js:2047:26)
    at Ma.check (NsRequire.js:1193:28)
    at Ma.enable (NsRequire.js:1475:10)
    at Ma.init (NsRequire.js:1106:11)
    at NsRequire.js:1771:18

Here are the the updated scripts as they stand now, which throw the above error:

User Event

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */

 define([], 
    
    function() {

function beforeLoad(scriptContext) {

    try {
         if (scriptContext.type == 'view') {
          var formObj = scriptContext.form;
              formObj.addButton({
                  id: 'custpage_print1',
                  label: 'Print 1',
                  functionName: 'print1'
              });

              formObj.clientScriptModulePath = 'SuiteScripts/buttonTest2/print_client_script.js';

            }
        } catch(error) {
              log.debug('ERROR', error);
        }
    }
    
           return {
        beforeLoad: beforeLoad
    };

});

Client

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 */

 define([
    'N/url',
],

function() {

    function pageInit() {}

    function print1(url) {
        var output = url.resolveScript({
            scriptId: 698,
            deploymentId: 1695,
            returnExternalUrl: true
        });
       
        window.open(output);
   }
   
   return {
        print1: print1,
        pageInit: pageInit
   };
})

Suitelet

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

 define(['N/render'],

 function(render) {

     function onRequest(context) {
     
       var total = 0;

          if (context.request.method == 'GET'){

         var ifid = context.request.parameters.custparam_ifid;

           var fileObj = render.packingSlip({
                    entityId: 658266,
                    printMode: render.PrintMode.PDF,
                    formId: 129
             });

                 fileObj.save()
          }
     }

     return {

         onRequest: onRequest
     };

  });

UPDATED UE SCRIPT:

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */

define([
    'N/url',
],

    function () {

        function beforeLoad(scriptContext) {

            try {
                if (scriptContext.type == 'view') {

                    const href = url.resolveScript({
                        scriptId: 699,
                        deploymentId: 1696,
                        returnExternalUrl: true
                    });
                    context.form.addButton({
                        id: 'custpage_print1',
                        label: 'Print 1',
                        functionName: "(window.location='"   href   "')"
                    });
                }
            } catch (error) {
                log.debug('ERROR', error);
            }
        }
        return {
            beforeLoad: beforeLoad
        };
    });

CodePudding user response:

You are not invoking the function or providing an eval argument (sort of). The functionName argument should just be the exported function name from the client script without parens. e.g.:

functionName: 'print1'

Also note that the client script doesn't have to be a ClientScript and that (IMO) your code is more portable if you use

 formObj.clientScriptModulePath = './custom_print_CS.js'; // or whatever you name it and save it besid your User Event script code. 

and your client script could look like:

/**
 * @NApiVersion 2.x
 */

define([
    'N/url',
],

function(url) {


    function print1() {
        var output = url.resolveScript({
            scriptId: 698,
            deploymentId: 1695,
            returnExternalUrl: true
        });
       
        window.open(output);
   }
   
   return {
        print1: print1
   };
})

CodePudding user response:

I know Suite Answer suggests this but there is an alternate solution that lets you skip the Client Script , by directly redirecting from the button to the Suitelet by directly putting the redirection on the button. The inside of your event script beforeLoad function would look like this

const href = url.resolveScript({
                    scriptId:698,
                    deploymentId:  1695,
                    returnExternalUrl: true
            });

context.form.addButton({
                    id:'custpage_print1',
                    label: 'Print 1',
                    functionName: "(window.location='" href  "')"
            });

Don't forget to load the N/url module

  • Related