在 SuiteScript 中提取 JSON 数据时出现问题 - NetSuite

Issue with pulling JSON data in SuiteScript - NetSuite

我正在尝试查看 "apply" 客户付款数据子列表中的值是否已更改并根据它执行一些操作。

我的SuiteScript如下:

define(['N/record', 'N/https'],
function(record,https)
{

function afterSubmit(context)
{
    var oldRec = context.oldRecord;
    log.debug({title: 'oldRec ', details: oldRec });
    // This log shows that the JSON has an 
    // attribute called sublists which contains "apply" which has all the applied payments
   // eg: {"id":"1234",  "type":"customerpayment",  "fields":{all the fields},
   // "sublists": {"apply" : {"line 1"...}}}

   var oldRecSublists = oldRec.sublists;
   log.debug({title: 'oldRecApply ', details: oldRecSublists });
   // This returns empty or null though there is data

我做错了什么?

基本上我想要实现的是比较 context.oldRecord.sublists.apply 和 context.newRecord.sublists.apply 以查看 amt 是否已更改。

SuiteScript 2.0 有更好的方法吗?

提前致谢!

发生的部分情况是,您似乎正试图通过在打印语句中看到的内容来探查 NS 数据结构。您根本没有使用 NS api。

当您将 NS 对象发送到日志函数时,我相信它会经过自定义 JSON.stringify 过程,因此如果您只想检查值,您可以执行以下操作:

var oldRecObj = JSON.parse(JSON.stringify(oldRec));

现在 oldRecObj 可以像检查一个简单的对象一样进行检查。但是你根本无法操纵它。

您应该使用 NS schema browser

并参考N/record

操作的帮助文档

我经常用来处理子列表的一个片段是:

function iter(rec, listName, cb){
    var lim = rec.getLineCount({sublistId:listName});
    var i = 0;
    var getV = function (fld){
        return rec.getSublistValue({sublistId:listName, fieldId:fld, line:i});
    };
    var setV = function(fld, val){
        rec.setSublistValue({sublistId:listName, fieldId:fld, line:i, value:val});
    };
    for(; i< lim; i++){
        cb(i, getV, setV);
    }
}

然后

iter(oldRec, 'apply', function(idx, getV, setV){
    var oldApplied = getV('applied');
});