netsuitesuitescriptsuitescript2.0clientscript

How can i get a field value in view mode using a client script?


Hi I'm new to suitescript I've added a custom print button to the inventory item record, it works totally fine in edit mode but in view mode the client script can't get the field value for my print type, it returns an undefined value. it gives this error:

{"type":"error.SuiteScriptError","name":"MISSING_PDF_PARAMETERS","message":"Missing parameters required to generate PDF","stack":["renderAsPdf(N/render)","onRequest(/SuiteScripts/rtt/add-print-button/acq_sl_form_print.js:66)"],"cause":{"type":"internal error","code":"MISSING_PDF_PARAMETERS","details":"Missing parameters required to generate PDF","userEvent":null,"stackTrace":["renderAsPdf(N/render)","onRequest(/SuiteScripts/rtt/add-print-button/acq_sl_form_print.js:66)"],"notifyOff":false},"id":"","notifyOff":false,"userFacing":false}

i'm using a user event script


/**

define(['N/runtime', 'N/log', 'N/currentRecord'], function (runtime, log, currentRecord) {

    /**
    * beforeLoad event handler
    *
    * @gov 0
    *
    * @param context
    *        {Object}
    * @param context.newRecord
    *        {record} The new record being loaded
    * @param context.type
    *        {UserEventType} The action type that triggered this event
    * @param context.form
    *        {form} The current UI form
    *
    * @return {void}
    * @static
    * @function beforeLoad
    */
    function beforeLoad(scriptContext) {
        var objForm = scriptContext.form;
        objForm.clientScriptModulePath = "SuiteScripts/rtt/add-print-button/acq_cs_get_record.js";
        objForm.addButton({
            id: 'custpage_printbutton',
            label: 'Print Custom Label',
            functionName: 'onClickPrint'
        });
    }

    return {
        beforeLoad : beforeLoad
    }; 
});

a client script (to get the record and calling the suitelet page). In edit mode it gets normally the printType but i want it to work even if it's view mode.


/**

define(['N/currentRecord'], function (currentRecord) {

    function pageInit(context) {

    }
    function onClickPrint(context) {
        var objRecord = currentRecord.get();
        var intRecId = objRecord.id;
        var printType = objRecord.getValue({
            fieldId: "custitem_acq_print_type"
        });
        console.log(printType);
        if(intRecId!= null && intRecId !="") {
            window.open('/app/site/hosting/scriptlet.nl?script=2553&deploy=1&printType=' + printType + '&recID=' + intRecId,'_blank');
        }
    }
    return {
        pageInit : pageInit,
        onClickPrint : onClickPrint
    }
}); 

and then a suitelet that renders my custom templates, based on which prinType is selected in the page


/**

define(['N/render', 'N/file', 'N/record'],

function (render, file, record) {

    function onRequest(context) {

        if (context.request.method === 'GET') {
            log.debug('phase', 'inizio');
            
            if (context.request.parameters.printType && context.request.parameters.recID) {
                var intRecId = context.request.parameters.recID;
                var printType = context.request.parameters.printType;
                log.debug('print type', printType);
                log.debug('record id', intRecId);
                var renderer = render.create();
            
                var temp = record.load({
                    id: intRecId,
                    type: record.Type.INVENTORY_ITEM
                });
                renderer.addRecord('record', temp);
    
                var linecount = temp.getLineCount({
                    sublistId: 'recmachcustrecord_acq_link'
                });
                log.debug(' righe ', linecount);
    
                var extraInformation = [];
                for (var i = 0; linecount > i; i++) {
                    extraInformation.push({
                        description: temp.getSublistValue({
                            fieldId: 'custrecord_acq_descriptions',
                            sublistId: 'recmachcustrecord_acq_link',
                            line: i
                        }),
                        detail: temp.getSublistValue({
                            fieldId: 'custrecord_acq_detail',
                            sublistId: 'recmachcustrecord_acq_link',
                            line: i
                        })
                    });
                }
            
                renderer.addCustomDataSource({
                    format: render.DataSource.OBJECT,
                    alias: "customData",
                    data: {extraInformation : extraInformation}
                });
                if(printType == 1){
                    renderer.setTemplateByScriptId("CUSTTMPL_ITEM_LABEL_A4");
                } else if(printType == 2){
                    renderer.setTemplateByScriptId("CUSTTMPL_ITEM_LABEL_A5");
                } else if(printType == 3){
                    renderer.setTemplateByScriptId("CUSTTMPL_ITEM_LABEL_A6");
                } else if(printType == 4){
                    renderer.setTemplateByScriptId("CUSTTMPL_ITEM_LABEL_6X3");
                }
                

                // render PDF
                var finalPDF = renderer.renderAsPdf();
                log.debug('render page', finalPDF);
                /* newfile.folder = -15; // ID of folder where file created
                newfile.name = "itemLabel.pdf";
                var fileId = newfile.save(); */
                context.response.addHeader({
                    name: 'Content-Type', value: 'application/pdf'
                });
                context.response.addHeader({
                    name: 'Content-Disposition', value: 'inline; filename=itemLabel.pdf'
                });
                context.response.writePage(finalPDF);
            }
        }
    }

    return {
        onRequest: onRequest
    };
});

Sorry fo my english, if anyone knows a workaround to make this work in view also, it will be a major uplift to my workday.


Solution

  • /**
     *@NApiVersion 2.1
     *@NScriptType UserEventScript
     */
    define(['N/record', 'N/search', 'N/runtime', 'N/url', 'N/ui/serverWidget'],
        function (record, search, runtime, url, serverWidget) {
            function beforeLoad(context) {
                if (context.type == context.UserEventType.VIEW) {
    
                    let newRecord = context.newRecord;
                    let recId = newRecord.id;
                    let rectype = newRecord.type;
    
                    let suiteletURL = url.resolveScript({
                        scriptId: 'your suitelet script id',
                        deploymentId: 'your suitelet deployment id'
                    });
    
                    suiteletURL = suiteletURL + '&recId=' + recId;
                    suiteletURL = suiteletURL + '&recType=' + rectype;
                    
                        log.debug('suiteletURL',suiteletURL)
    
                        let button = context.form.addButton({
                            id: 'custpage_click_button',
                            label: 'click me',
                            functionName: 'window.open("'+ suiteletURL +'", "_blank")'
    
                        });
                }
            }
    
    
            return {
                beforeLoad: beforeLoad
            };
        });