在SuiteScript中提取JSON数据的问题 - NetSuite

时间:2018-03-22 21:34:56

标签: javascript netsuite suitescript suitescript2.0

我试图查看"中的值是否适用"客户付款数据的子列表已更改,并根据它执行某些操作。

我的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吗?

提前致谢!

1 个答案:

答案 0 :(得分:1)

Part of what is going on there is that it looks like you are trying to spelunk the NS data structure by what you see in the print statement. You are not using the NS api at all.

When you send the NS object to the log function I believe it goes through a custom JSON.stringify process so if you want to just inspect values you can do:

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

now oldRecObj can be inspected as though it were a simple object. But you won't be able to manipulate it at all.

You should be using the NS schema browser

and referring to the help docs for operations on N/record

A snippet I often use for dealing with sublists is:

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);
    }
}

and then

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