Glide Record

DebiprasadP
ServiceNow Employee
ServiceNow Employee

Hello All,

I am new to javascript world ; started learning .

I have a task where i am fetching json data from one table and extracting few elements from that json data and storing into another table.

 

Table1 (result string) --> stores json data

Table2(Col1 string, Col2 String,)

 

Example- 

/*
var data =
[
{
"state": "success",
"results": "[{\"hash\":\"123\",\"count\":\"22\"},{\"hash\":\"456\",\"count\":\"211\"}]",
"host": "127.0.0.1"
}
];

var mydata = JSON.stringify(data[0].results);

//var results = data.results.toString();
gs.info(mydata);
*/

 

Output - [ { hash: '123', count: '22' }, { hash: '456', count: '211' } ]

 

But when i try to use below code gives me error:

var rec = new GlideRecord('table1');

rec.query();

while (rec.next())
{
//gs.info(1);
jsonresult = rec.result.toString();
gs.info(jsonresult);
//gs.info(JSON.parse(jsonresult.state));
mydata = JSON.stringify(jsonresult[0].results);
gs.info(mydata);

};

 

It gives me error like cannot fetch data from undefined.

 

Please guide me if anything i am missing.

 

Thanks in advance.

 

2 REPLIES 2

Sandeep Rajput
Tera Patron
Tera Patron

@DebiprasadP 

 

Please update the code as follows and see if it works for you.

 

var rec = new GlideRecord('table1');
rec.query();
while (rec.next())
{
//gs.info(1);
var jsonresult = rec.result.toString();
var mydata = JSON.parse(jsonresult);
if(mydata){
gs.info(jsonresult.state);
gs.info(jsonresult.results[0].hash);
gs.info(jsonresult.results[0].count);
}
}

Juhi Poddar
Kilo Patron

Hello @DebiprasadP 

As per my understanding table1 has field result that stores JSON data in the form of string. Your requirement is to get the value from result field and save the hash and count data in table2 in two different fields.

  • Function getValue('fieldName') helps to get the data from the field.
  • To read more about glide record API refer this: Glide Record 

Here is the updated script:

 

var rec = new GlideRecord('table1'); //modify te table1 name
rec.query();

while (rec.next()) {
    var jsonResultString = rec.getValue('result');  // Fetch the JSON string directly from the 'result' field
    gs.info("Original JSON String: " + jsonResultString);

    try {
        var jsonResult = JSON.parse(jsonResultString); // Parse the JSON string into a JavaScript object
        var resultsArray = JSON.parse(jsonResult[0].results); // Parse the JSON string into a JS object
        gs.info("Parsed Results Array: " + JSON.stringify(resultsArray));

        // Iterate through the array and process each object
        resultsArray.forEach(function(result) {
            // Create a new record in Table2
            var newRecord = new GlideRecord('table2'); //modify te table2 name
            newRecord.initialize();
            newRecord.col1 = result.hash; // Map 'hash' to 'Col1'
            newRecord.col2 = result.count; // Map 'count' to 'Col2'
            newRecord.insert();

            gs.info("Inserted record with Hash: " + result.hash + ", Count: " + result.count);
        });
    } catch (e) {
        gs.error("Error parsing JSON: " + e.message);
    }
}

 

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar