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

如何执行Suitelet后自动关闭窗口并阻止页面跳转至基础物品搜索页

解决方案

核心问题分析

  1. window.open会强制生成新窗口,且浏览器对非用户主动触发的window.close有安全限制,导致无法自动关闭空白页。
  2. 页面跳转是因为window.open的触发逻辑干扰了当前页面状态,需用异步请求替代同步跳转。

修改按钮代码(彻底解决窗口与跳转问题)

把原按钮的window.open替换为AJAX异步请求,无需打开新窗口,也不会触发页面跳转:

'<button onclick="handleApproval(\'' || {systemnotes.field} || '\', \'' || {internalid} || '\'); return false;">Approve</button>'

同时在已保存搜索的自定义HTML字段中添加对应JS函数:

function handleApproval(fieldId, recId) {
  const url = `/app/site/hosting/scriptlet.nl?script=602&deploy=1&compid=*******_SB2&field_id=${fieldId}&rec_id=${recId}`;
  
  fetch(url, { method: 'GET' })
    .then(response => {
      if (response.ok) {
        alert('审批完成');
        window.location.reload(); // 可选:刷新当前搜索页更新状态
      } else {
        alert('操作失败,请重试');
      }
    })
    .catch(error => {
      console.error('请求错误:', error);
      alert('操作出错,请查看控制台');
    });
}

修改Suitelet代码(移除无效客户端逻辑,优化业务处理)

服务器端Suitelet无需输出页面内容,仅完成记录更新即可,移除无效的closer函数(服务器端无法调用客户端window对象):

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

define(['N/record'], function(record) {
  function onRequest(scriptContext) {
    const rec_id = scriptContext.request.parameters.rec_id;
    const field_id = scriptContext.request.parameters.field_id;

    if (!rec_id || !field_id) {
      scriptContext.response.write('缺少必要参数');
      return;
    }

    try {
      const itemRecObj = record.load({
        type: record.Type.INVENTORY_ITEM,
        id: rec_id,
        isDynamic: true
      });

      switch(field_id) {
        case 'custom1':
          itemRecObj.setValue({ fieldId: 'checkbox1', value: true });
          break;
        case 'custom2':
          itemRecObj.setValue({ fieldId: 'checkbox2', value: true });
          break;
        case 'custom3':
          itemRecObj.setValue({ fieldId: 'checkbox3', value: true });
          break;
        default:
          scriptContext.response.write('无效的字段标识');
          return;
      }

      itemRecObj.save();
      scriptContext.response.write('操作成功');
    } catch (error) {
      scriptContext.response.write(`操作失败: ${error.message}`);
    }
  }

  return {
    onRequest: onRequest
  };
});

关键改进点

  • 用AJAX替代window.open,彻底消除新窗口和页面跳转问题。
  • 移除Suitelet中无效的客户端操作代码,专注服务器端业务逻辑。
  • 增加参数校验和错误捕获,提升代码稳定性。
  • 可选的页面刷新操作,让用户实时看到记录更新后的状态。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 05:10:55