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

Date需为DD/MM/YYYY格式报错及format方法调用失败求助

解决NetSuite中Advanced Intercompany Journal Entry日期设置及format模块调用错误

1. 修复format模块引入错误

你的代码中重复引入了N/file模块,导致真正的N/format模块被覆盖:

// 错误的define
define(['N/search', 'N/runtime', 'N/record', 'N/task', 'N/file', 'N/file', 'N/format'], function (search, runtime, record, task, file, format) {

这里第二个N/file会占用原本属于N/format的参数位置,导致format变量实际指向的是N/file模块,而非N/format。修正方法是移除重复的N/file:

// 正确的define
define(['N/search', 'N/runtime', 'N/record', 'N/task', 'N/file', 'N/format'], function (search, runtime, record, task, file, format) {

2. 正确解析DD/MM/YYYY格式的日期

JavaScript原生new Date()对DD/MM/YYYY格式的解析存在兼容性问题(默认优先识别为MM/DD/YYYY),当日期中的“日”大于12时(比如13/12/2024),会直接返回无效日期,导致设置trandate时触发错误。

使用NetSuite的format.parse方法可以指定格式解析日期,确保正确识别DD/MM/YYYY:

// 替换原有的new Date(invoiceDate)代码
var datenew = format.parse({
    value: invoiceDate,
    type: format.Type.DATE,
    format: format.DATETIMEFORMAT.DDMMYYYY // 指定输入格式为DD/MM/YYYY
});

如果环境中format.DATETIMEFORMAT.DDMMYYYY不可用,也可以直接使用格式字符串:

var datenew = format.parse({
    value: invoiceDate,
    type: format.Type.DATE,
    format: 'dd/MM/yyyy'
});

3. 完整修改后的代码片段

以下是修正后的关键部分代码:

var NS_MAX_SEARCH_RESULT = 1000;
var JEIDArray=[];
// 修复重复引入的N/file
define(['N/search', 'N/runtime', 'N/record', 'N/task', 'N/file', 'N/format'], function (search, runtime, record, task, file, format) {

    function execute(context) {
        // ... 原有execute代码不变
    }
    
    function createICJE(results,invoiceId) {
        log.debug("inside craeteje");
        var counter=0;
        results.forEach(function (result) {
            var invoiceNumber =  result.getValue({ name: "invoicenum", label: "Invoice Number" });
            var invoiceDate = result.getValue({ name: "trandate", label: "Date" });
            log.debug("invoice date",invoiceDate);
            
            // 替换原生Date解析,用NetSuite format模块
            var datenew = format.parse({
                value: invoiceDate,
                type: format.Type.DATE,
                format: format.DATETIMEFORMAT.DDMMYYYY
            });
            log.debug("datenew",datenew);

            var journal = record.create({
                type: 'advintercompanyjournalentry',
                isDynamic: true,
                defaultValues: {
                    customform: 136       
                }
            });
            journal.setValue('trandate', datenew);
            // ... 后续setValue代码不变
            var journalId = journal.save();
            log.debug('Journal Entry Created', 'Journal ID: ' + journalId);
            JEIDArray.push({ item: itemId, amount: ayukAllocationAmount, JE: journalId });
        }); // 补充原有代码缺失的forEach闭合括号
    }

    return {
        execute: execute
    }
});

额外注意事项

  • 确保脚本有权限访问N/format模块,在脚本部署的“可用功能”中勾选对应权限。
  • 检查搜索返回的trandate是否统一为DD/MM/YYYY格式,若部分发票格式不一致,需先做格式校验或转换。
  • 原有代码中createICJE函数内的postingId、subsidiary等变量未定义,需确保这些变量有正确赋值,否则会引发新错误。

内容的提问来源于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.15 21:13:16