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

如何避免NetSuite中HTTPS请求的SSS_REQUEST_TIME_EXCEEDED错误

问题描述

我有一个销售订单创建时触发的User Event脚本,会向Mulesoft的集成URL发送HTTPS POST请求。虽然每次API调用都成功,但NetSuite仍会在45秒内抛出SSS_REQUEST_TIME_EXCEEDED错误。想请教:

  • 是否可以延长该超时时间?
  • 能否让请求在后台运行?
  • 是否应该创建Suitelet并在User Event中调用?

补充说明:每次POST调用均成功,但脚本仍报错。

原User Event代码

function afterSubmit(context) {
if (context.type !== context.UserEventType.CREATE)
return;
try{
    var envtype = JSON.stringify(runtime.envType)
    var accountId = runtime.accountId
    var isProduction = (accountId == '12345')
    log.debug({title: 'Before Post' }) 
    
    
    
    var tokenrecord =  record.load({
        type: 'customrecord_mulesoft_setup',
        id: 1,
        isDynamic: true,
    });
    
    var token = tokenrecord.getValue({
        fieldId: 'custrecord_sandbox_token_so_api'
    })
    log.debug({title : 'token' , details: token});
    
    var headers = {
        'Content-Type' : 'application/json',
        Authorization : isProduction ? 'Bearer ' + token : 'Bearer ' + token,
        'User-Agent': 'NetSuite/2022',
        Accept: '*/*',
        Host : isProduction ? 'test.com' : 'test.com',
        'Accept-Encoding': 'gzip, deflate, br',
        Connection :'keep-alive'            
    }
    
    var salesorderid = context.newRecord.id;
    if (isProduction)      {
        apiNum = 'xxxxxx';
        endPointUrl = 'https://test.com/' + salesorderid;               } 
        else {
            
            apiNum = 'yyyy';  // all updates
            endPointUrl = 'https://test.com/' + salesorderid;
        }
        
        
        var postedObj = { /*
        parameters : [
            {
                "Key": "ns_internal_id",
                "Value": v_intid
            } 
        ] */
    }                 
    
    var jsonify = JSON.stringify(postedObj);
    
    log.debug({title : 'url' , details: endPointUrl});
    log.debug({title : 'headers' , details: headers});
    log.debug({title : 'body' , details: jsonify});
    
    
    var resp=https.post({
        url: endPointUrl, 
        headers: headers,
        body: jsonify
    });
    
    var id = record.submitFields({
        type: record.Type.SALES_ORDER,
        id: salesorderid,
        values: {
            custbody_sent_to_mulesoft: true
        }
    });
}
catch(e){
    log.error({title : 'Error Message' , details: e.message});
    log.error({title : 'Error Code' , details: e.name});

log.debug({title: 'Headers' + 'resp.headers' })

    log.error({title : 'Could not send item to SF !!' , details: JSON.stringify(resp)});
    log.error({title : 'Could not send item to SF !!' , details: JSON.stringify(jsonify)})

  }
return;
}

错误信息

Message: 你尝试连接的主机超出了允许的最大响应时间。
Code: SSS_REQUEST_TIME_EXCEEDED


解决方案

1. 关于超时时间延长

NetSuite对同步HTTP请求的超时时间无法自定义延长,默认限制为45秒。即使请求最终执行成功,只要响应返回时间超过45秒,平台就会触发SSS_REQUEST_TIME_EXCEEDED错误,这是平台硬限制,没有绕过途径。

2. 让请求后台运行的正确方式

不要使用Suitelet,正确方案是用**计划脚本(Scheduled Script)或脚本任务(Script Task)**实现异步处理:

  • 在User Event的afterSubmit中,不直接执行HTTP请求,而是创建一个异步脚本任务,传递销售订单ID等必要参数。
  • 计划脚本负责实际调用Mulesoft API,该逻辑在NetSuite后台异步执行,不受45秒同步超时限制。

3. 代码调整示例

修改后的User Event脚本(仅触发异步任务)

function afterSubmit(context) {
    if (context.type !== context.UserEventType.CREATE) return;

    try {
        var salesorderid = context.newRecord.id;
        // 创建计划脚本任务
        var scriptTask = task.create({
            taskType: task.TaskType.SCHEDULED_SCRIPT,
            scriptId: 'customscript_mulesoft_salesorder_sync', // 替换为你的计划脚本ID
            deploymentId: 'customdeploy_mulesoft_salesorder_sync', // 替换为你的脚本部署ID
            params: {
                custscript_so_id: salesorderid // 传递销售订单ID参数
            }
        });
        var taskId = scriptTask.submit();
        log.debug({title: '异步任务已提交', details: taskId});
    } catch(e) {
        log.error({title: '任务创建失败', details: e.message});
    }
}

对应的计划脚本

function execute(context) {
    var salesorderid = context.getParameter({name: 'custscript_so_id'});
    if (!salesorderid) {
        log.error({title: '参数缺失', details: '未获取到销售订单ID'});
        return;
    }

    try {
        var accountId = runtime.accountId;
        var isProduction = (accountId == '12345');
        
        // 加载Token配置
        var tokenrecord = record.load({
            type: 'customrecord_mulesoft_setup',
            id: 1,
            isDynamic: true,
        });
        var token = tokenrecord.getValue({fieldId: 'custrecord_sandbox_token_so_api'});
        
        // 构建请求头
        var headers = {
            'Content-Type' : 'application/json',
            'Authorization' : 'Bearer ' + token,
            'User-Agent': 'NetSuite/2022',
            'Accept': '*/*',
            'Host' : 'test.com',
            'Accept-Encoding': 'gzip, deflate, br',
            'Connection' :'keep-alive'            
        };
        
        // 构建请求URL
        var endPointUrl = 'https://test.com/' + salesorderid;
        
        var postedObj = {}; // 补充实际需要传递的参数结构
        var jsonify = JSON.stringify(postedObj);
        
        log.debug({title : '请求URL' , details: endPointUrl});
        log.debug({title : '请求体' , details: jsonify});
        
        // 发送POST请求
        var resp = https.post({
            url: endPointUrl, 
            headers: headers,
            body: jsonify
        });
        
        // 更新销售订单同步状态
        record.submitFields({
            type: record.Type.SALES_ORDER,
            id: salesorderid,
            values: {custbody_sent_to_mulesoft: true}
        });
        
        log.debug({title: '同步成功', details: resp.body});
    } catch(e) {
        log.error({title : '同步失败' , details: e.message});
        log.error({title : '错误代码' , details: e.name});
    }
}

为什么不推荐Suitelet?

如果在User Event中调用Suitelet,本质还是同步执行逻辑,依然会受到45秒超时限制,无法解决当前问题。只有异步的计划脚本/脚本任务才能绕过同步超时约束。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 11:55:40