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

在NetSuite中添加CRM任务自定义按钮及相关技术咨询

NetSuite自定义按钮字段验证与任务完成触发方案

需求说明

  • 在NetSuite表单中添加自定义按钮,仅当指定自定义字段(ID:custeventcrm_sales_status)值为6时,允许触发CRM任务完成操作
  • 字段值不符合要求时,弹窗提示用户无法完成任务

用户提供的无效代码

define([], function() {
  function completeTaskIfCustomFieldIsTrue() {
    var customFieldId = 'custeventcrm_sales_status'; // Replace with the actual custom field ID
    var isCustomFieldTrue = document.getElementById(customFieldId).value;
    var isStatus = 'status'; //FieldID of status task

    if (isCustomFieldTrue == 6) {
      // Custom field is true, complete the CRM task
      // Add your code here to complete the CRM task
       button.onclick = function() {
        window.location.href = 'sb2.app.netsuite.com/app/crm/calendar/task.nl?id=|| {internal_id} ||&whence=&cmid=1692983702342_16261';
        var isStaus = 'COMPLETE'; // Replace with the desired status

        changeTaskStatus(taskId, newStatus);
  };

  // Append the button to the form
  var form = document.getElementById('custform_200_5210436_sb2_536'); // Replace with the actual form ID
  form.appendChild(button);
    } else {
      // Custom field is not true, do nothing
      window.alert('Custom field is not true. Skipping CRM task...');
    }
  }
  return {
    pageInit: function(context) {
      completeTaskIfCustomFieldIsTrue();
    }
  };
});

代码问题修复及优化方案

原代码存在多处问题:未定义button变量、直接操作原生DOM不符合NetSuite规范、变量拼写错误、未正确获取任务ID等。以下是修复后的SuiteScript 2.x客户端脚本:

define(['N/ui/message', 'N/ui/serverWidget', 'N/currentRecord'], function(message, serverWidget, currentRecord) {
    function initButtonLogic() {
        var currentRec = currentRecord.get();
        var form = currentRec.getForm();
        
        // 添加自定义按钮
        form.addButton({
            id: 'custpage_complete_crm_task',
            label: '完成CRM任务',
            functionName: 'handleTaskCompletion'
        });

        // 绑定按钮点击逻辑
        window.handleTaskCompletion = function() {
            var fieldValue = currentRec.getValue({
                fieldId: 'custeventcrm_sales_status'
            });

            if (fieldValue === 6) {
                // 更新任务状态为完成
                currentRec.setValue({
                    fieldId: 'status',
                    value: 'COMPLETE'
                });
                currentRec.save();

                // 显示成功提示
                message.create({
                    title: '操作成功',
                    message: 'CRM任务已完成',
                    type: message.Type.SUCCESS
                }).show();
            } else {
                // 显示错误提示
                message.create({
                    title: '操作失败',
                    message: '自定义字段值不为6,无法完成任务',
                    type: message.Type.ERROR
                }).show();
            }
        };
    }

    return {
        pageInit: function(context) {
            initButtonLogic();
        }
    };
});

修复要点

  • 使用NetSuite官方API替代原生DOM操作,避免环境兼容性问题
  • 实时获取字段值,确保判断逻辑与当前表单状态一致
  • 用NetSuite内置消息组件替代原生alert,保持界面风格统一
  • 直接通过API更新任务状态并保存,无需跳转页面

关于已保存搜索实现类似功能的说明

已保存搜索本身无法直接触发任务完成操作,它仅用于数据查询和报表展示。但可以结合**计划脚本(Scheduled Script)**实现自动化逻辑:

  1. 创建已保存搜索,筛选出custeventcrm_sales_status值为6且状态未完成的CRM任务
  2. 编写计划脚本,定期执行该搜索,对符合条件的任务批量更新状态为完成
  3. 配置脚本执行频率(如每日、每小时),实现自动化触发

如果需要实时触发,建议使用用户事件脚本(User Event Script),在custeventcrm_sales_status字段值变更为6时自动完成任务,无需手动点击按钮。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 13:50:20