Venkataraman
Venkataraman

Reputation: 151

Issue with pulling JSON data in SuiteScript - NetSuite

I am trying to see if a value in the "apply" sublist for customer payment data has changed and do some action based on it.

My SuiteScript is as follows:

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

What am I doing wrong here?

Basically what I am trying to achieve is compare the context.oldRecord.sublists.apply and context.newRecord.sublists.apply to find if the amt has changed or not.

Is there a better way to do this is SuiteScript 2.0?

Thanks in advance!

Upvotes: 0

Views: 4397

Answers (1)

bknights
bknights

Reputation: 15377

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

Upvotes: 3

Related Questions