You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SuiteScript视图模式下无法获取字段值的解决方案求助

视图模式下SuiteScript自定义打印按钮无法获取字段值的问题解决

我是SuiteScript新手,给库存物料(Inventory Item)记录添加了自定义打印按钮,该功能在编辑模式下可正常运行,但视图模式下客户端脚本无法获取custitem_acq_print_type字段的值,返回undefined,进而导致Suitelet触发MISSING_PDF_PARAMETERS错误,错误详情如下:

{"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}

现有脚本

1. 用户事件脚本(UserEventScript)

/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
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
    }; 
});

2. 客户端脚本(ClientScript)

/**
* @NApiVersion 2.1
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
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
    }
}); 

3. Suitelet脚本

/**
* @NApiVersion 2.x
* @NScriptType Suitelet
*/
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
    };
});

解决方法

方法1:Suitelet直接加载记录获取参数(最推荐)

完全避免客户端上下文限制,Suitelet仅接收记录ID,自行从服务器端加载记录获取printType:

  1. 修改客户端脚本的onClickPrint函数,只传递记录ID:
function onClickPrint(context) {
    var objRecord = currentRecord.get();
    var intRecId = objRecord.id;

    if(intRecId!= null && intRecId !="") {
        window.open('/app/site/hosting/scriptlet.nl?script=2553&deploy=1&recID=' + intRecId,'_blank');
    } else {
        alert('无法获取记录ID');
    }
}
  1. 修改Suitelet的onRequest函数,直接从加载的记录中读取printType:
function onRequest(context) {
    if (context.request.method === 'GET') {
        log.debug('phase', 'inizio');
        var intRecId = context.request.parameters.recID;
        if (intRecId) {
            log.debug('record id', intRecId);
            var renderer = render.create();
            var temp = record.load({
                id: intRecId,
                type: record.Type.INVENTORY_ITEM
            });
            // 直接从服务器端记录获取printType
            var printType = temp.getValue({fieldId: "custitem_acq_print_type"});
            log.debug('print type', printType);
            
            if (!printType) {
                context.response.write('缺少打印类型参数');
                return;
            }
            
            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");
            } else {
                context.response.write('无效的打印类型');
                return;
            }
            // render PDF
            var finalPDF = renderer.renderAsPdf();
            log.debug('render page', finalPDF);
            context.response.addHeader({
                name: 'Content-Type', value: 'application/pdf'
            });
            context.response.addHeader({
                name: 'Content-Disposition', value: 'inline; filename=itemLabel.pdf'
            });
            context.response.writePage(finalPDF);
        } else {
            context.response.write('缺少记录ID参数');
        }
    }
}

方法2:用户事件脚本添加隐藏字段传递参数

在表单加载时将printType存入隐藏字段,客户端直接读取该字段:

  1. 修改用户事件脚本的beforeLoad函数:
function beforeLoad(scriptContext) {
    var objForm = scriptContext.form;
    var currentRec = scriptContext.newRecord;
    var printType = currentRec.getValue({fieldId: "custitem_acq_print_type"});
    
    objForm.clientScriptModulePath = "SuiteScripts/rtt/add-print-button/acq_cs_get_record.js";
    // 添加隐藏字段存储printType
    var hiddenField = objForm.addField({
        id: 'custpage_hidden_printtype',
        type: 'text',
        label: 'Hidden Print Type'
    });
    hiddenField.isDisplay = false;
    hiddenField.defaultValue = printType;
    
    objForm.addButton({
        id: 'custpage_printbutton',
        label: 'Print Custom Label',
        functionName: 'onClickPrint'
    });
}
  1. 修改客户端脚本的onClickPrint函数:
function onClickPrint(context) {
    var objRecord = currentRecord.get();
    var intRecId = objRecord.id;
    // 读取隐藏字段的值
    var printType = objRecord.getValue({fieldId: "custpage_hidden_printtype"});

    console.log(printType);
    if(intRecId!= null && intRecId !="" && printType) {
        window.open('/app/site/hosting/scriptlet.nl?script=2553&deploy=1&printType=' + printType + '&recID=' + intRecId,'_blank');
    } else {
        alert('无法获取打印类型参数');
    }
}

方法3:客户端脚本从DOM获取字段值

视图模式下直接从页面DOM提取字段值作为兜底:

// 替换客户端脚本中的onClickPrint函数
function onClickPrint(context) {
    var objRecord = currentRecord.get();
    var intRecId = objRecord.id;
    // 尝试从DOM获取字段值
    var printTypeField = document.getElementById('custitem_acq_print_type');
    var printType = printTypeField ? printTypeField.value : null;
    
    // 兜底:DOM获取失败时尝试currentRecord
    if (!printType) {
        printType = objRecord.getValue({fieldId: "custitem_acq_print_type"});
    }

    console.log(printType);
    if(intRecId!= null && intRecId !="" && printType) {
        window.open('/app/site/hosting/scriptlet.nl?script=2553&deploy=1&printType=' + printType + '&recID=' + intRecId,'_blank');
    } else {
        alert('无法获取打印类型参数,请检查字段是否显示在表单上');
    }
}

内容的提问来源于stack exchange,提问作者Fil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 22:31:13