Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

JSON Parse not working

Community Alums
Not applicable

HI Team,

 

We have written a script include which is Global scope and also Before BR also in same scope

We are calling the script include as below

 

var parseValue = JSON.parse(new CustomerAccountUtil().getRootParent(current.sys_id));

 

But on doing gs.addInfoMessage(parseValue );

 

Its giving undefined message/error. However at script include side when we do gs.log()  we are getting the proper output.

Kindly help

 

8 REPLIES 8

Sandeep Rajput
Tera Patron
Tera Patron

@Community Alums Could you please check if new CustomerAccountUtil().getRootParent(current.sys_id)) a valid JSON string? If it isn't than you can expect to have errors while using JSON.parse method.

Community Alums
Not applicable

We are passing JSON object from script include and getting undefined error

Community Alums
Not applicable
Below us the function in script include:
  getRootParent: function(rec) {
        var obj = {};
        var objSysId = [];
        var retValue = '';
        var record = new GlideRecord('customer_account');
        record.addQuery('sys_id', rec);
        record.query();
        if (record.next()) {
            if (record.account_parent.toString() == '') {
                obj.sysId = record.getValue('sys_id');
                obj.custAcct = record.getValue('account_path');
                //objSysId.push(obj);
                gs.info("rootParentobj: " + JSON.stringify(obj));
                retValue = JSON.stringify(obj);
                return retValue;
            } else {
                this.getRootParent(record.account_parent.toString());
            }
        }
    },

@Community Alums try this

 

getRootParent: function(rec) {
        var obj = {};
        var objSysId = [];
        var retValue = '';
        var record = new GlideRecord('customer_account');
        record.addQuery('sys_id', rec);
        record.query();
        if (record.next()) {
            if (record.account_parent.toString() == '') {
                obj.sysId = record.getValue('sys_id');
                obj.custAcct = record.getValue('account_path');
                objSysId.push(obj);
                gs.log("My array objstysid: "+objSysId);
                return JSON.stringify(objSysId);
            } else {
                this.getRootParent(record.account_parent.toString());
            }
        }
    return "";
    },

 

Use this in BR:

 

var answer= new CustomerAccountUtil().getRootParent(current.sys_id);
gs.addInfoMessage(answer);
answer = JSON.parse(answer);
gs.addInfoMessage(answer);

 

Validate the log added in the script include if you still not getting the values on BR.

 

Thanks!

 

Please mark it as correct and helpful if it works✔️👍