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

NetSuite:如何从客户端脚本向用户事件脚本传递模板ID?

解决方案

1. 客户端脚本修改:通过URL传递模板ID

修改按钮点击逻辑,刷新页面时将选中的模板ID作为URL查询参数传递,让服务器端的BeforeLoad事件能读取到该值:

//Client Script
function clientButtonClick() {
    try {
        var currentRec = currentRecord.get();
        var templateId = currentRec.getValue({fieldId:"template"});
        alert("templateId before refresh: " + templateId);
        
        // 构造带模板ID参数的刷新URL
        var currentUrl = new URL(window.location.href);
        currentUrl.searchParams.set('templateId', templateId);
        
        window.onbeforeunload = null;
        window.location.href = currentUrl.toString();
    } catch (e) {
        log.error("error in clientButtonClick()", e);
    }
}

function pageInit() {
    // Initialization code
}

2. 用户事件脚本(BeforeLoad)修改:读取参数并处理

修正原脚本中的错误(如未定义的rec变量、重复添加按钮),并通过请求对象获取URL参数,完成字段设置或子列表操作:

//User Event BeforeLoad
function beforeLoad(scriptContext) {
    try {
        if (scriptContext.type === 'create') {
            var form = scriptContext.form;
            var request = scriptContext.request;
            var rec = scriptContext.newRecord; // 获取当前新建的记录对象
            
            // 添加自定义临时字段(用于展示传递的模板ID)
            var field = form.addField({
                id : 'custpage_abc_text',
                type : serverWidget.FieldType.TEXT,
                label : 'Custom Template Id'
            });
            
            // 仅添加一次刷新按钮
            form.addButton({
                id: 'custpage_email_message_button',
                label: 'Refresh',
                functionName: 'clientButtonClick'
            });
            form.clientScriptModulePath = 'SuiteScripts/cs_get_template.js';
            
            // 从URL参数中读取模板ID
            var templateId = request.parameters.templateId;
            log.debug("templateId from URL", templateId);
            
            if (templateId) {
                // 将模板ID设置到临时字段
                field.defaultValue = templateId;
                // 如果需要保存到永久自定义字段,替换为以下代码(需替换实际字段ID)
                // rec.setValue({fieldId: 'custrecord_your_permanent_field', value: templateId});
                
                if(templateId == 272) {
                    // 执行子列表设置逻辑
                    log.debug("Processing sublists for template 272");
                    // 示例子列表操作
                    // rec.selectNewLine({sublistId: 'your_sublist_id'});
                    // rec.setCurrentSublistValue({sublistId: 'your_sublist_id', fieldId: 'target_field', value: 'desired_value'});
                    // rec.commitLine({sublistId: 'your_sublist_id'});
                }
            }
        }
    } catch (e) {
        log.error({ title: 'Error Details', details: e });
    }
}

关键说明

  • URL参数传递:这是NetSuite客户端向服务器端临时传递数据的常用方式,避免页面刷新后数据丢失。
  • 记录对象修正:原脚本中未定义rec变量,需通过scriptContext.newRecord获取当前操作的记录实例。
  • 重复按钮移除:删除原脚本中重复添加的按钮,避免界面异常。
  • 字段设置区分:临时字段(custpage_前缀)仅用于页面展示,若需永久保存值,需替换为实际的自定义实体字段ID并使用rec.setValue。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 02:09:55