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

如何在Node.js中结合Lambda与SuiteScript 2.0获取NetSuite工单字段值?

解决NetSuite工单字段值获取与同步问题

一、把NetSuite工单页面字段值同步到本地控制台

如果只是想在浏览器控制台查看字段值,直接在客户端脚本的pageInit函数里输出即可:

function pageInit(context) {
    // 获取当前工单记录对象
    const currentRecord = context.currentRecord;
    // 读取xyz字段值(替换成实际字段ID)
    const xyzValue = currentRecord.getValue({ fieldId: 'xyz' });
    // 输出到浏览器控制台
    console.log('工单xyz字段值:', xyzValue);
}

打开NetSuite工单页面后,按F12调出浏览器开发者工具,切换到Console标签就能看到输出内容。

如果要把字段值传到本地Node服务的控制台,需要在客户端脚本里加请求逻辑,把数据发送到本地服务:

function pageInit(context) {
    const currentRecord = context.currentRecord;
    const xyzValue = currentRecord.getValue({ fieldId: 'xyz' });
    
    // 发送POST请求到本地Node服务(假设本地服务端口为3000)
    fetch('http://localhost:3000/receive-netsuite-data', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            workOrderId: currentRecord.id,
            xyzValue: xyzValue
        })
    })
    .then(res => res.json())
    .then(data => console.log('本地服务接收成功:', data))
    .catch(err => console.error('请求失败:', err));
}

同时在本地写个简单的Node服务接收数据:

const express = require('express');
const app = express();
app.use(express.json());

app.post('/receive-netsuite-data', (req, res) => {
    console.log('收到NetSuite工单数据:', req.body);
    res.json({ status: 'success' });
});

app.listen(3000, () => {
    console.log('本地服务运行在端口3000');
});

启动本地服务后,打开NetSuite工单页面,字段值就会同步到本地控制台。

二、用Lambda + Node.js获取NetSuite工单字段值

Lambda作为服务端,需要调用NetSuite的REST API拉取工单数据,步骤如下:

  1. 配置NetSuite REST API权限

    • 在NetSuite后台启用REST Web Services,创建集成记录,获取clientId、clientSecret、accountId、tokenId、tokenSecret这些认证参数。
    • 给集成记录分配工单记录的读取权限。
  2. 编写Lambda Node.js代码
    先获取OAuth2访问令牌,再调用工单API读取字段值:

const axios = require('axios');

exports.handler = async (event) => {
    // 从Lambda环境变量读取NetSuite认证信息(不要硬编码)
    const nsConfig = {
        accountId: process.env.NS_ACCOUNT_ID,
        clientId: process.env.NS_CLIENT_ID,
        clientSecret: process.env.NS_CLIENT_SECRET,
        tokenId: process.env.NS_TOKEN_ID,
        tokenSecret: process.env.NS_TOKEN_SECRET
    };

    // 获取访问令牌
    const tokenRes = await axios.post(
        `https://${nsConfig.accountId}.suitetalk.api.netsuite.com/services/rest/auth/oauth2/v1/token`,
        new URLSearchParams({ grant_type: 'client_credentials' }),
        {
            auth: { username: nsConfig.clientId, password: nsConfig.clientSecret },
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }
    );
    const accessToken = tokenRes.data.access_token;

    // 调用工单API获取数据(替换成目标工单ID)
    const workOrderId = '12345';
    const woRes = await axios.get(
        `https://${nsConfig.accountId}.suitetalk.api.netsuite.com/services/rest/record/v1/workorder/${workOrderId}`,
        { headers: { Authorization: `Bearer ${accessToken}`, Accept: 'application/json' } }
    );

    // 提取xyz字段值(替换成实际字段ID)
    const xyzValue = woRes.data.xyz;
    console.log('Lambda获取的工单xyz字段值:', xyzValue);

    return {
        statusCode: 200,
        body: JSON.stringify({ xyzValue: xyzValue })
    };
};
  1. 部署与测试
    • 把代码打包上传到Lambda,在环境变量中配置NetSuite的认证参数。
    • 触发Lambda执行后,可在Lambda日志里查看字段值,也可以通过返回值把数据传给其他服务。

注意:NetSuite REST API的字段ID可能和客户端脚本中的字段ID有差异,需要确认正确的字段标识;Lambda要能访问NetSuite的API域名,需确保VPC配置允许出站请求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 05:30:53