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.

How to log an Object in ServiceNow?

Meloper
Kilo Sage
var Arrays = Class.create();
Arrays.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    diffArrays: function(a1, a2) {

        var a = [],
            diff = [];
        var oldBigger = false;

        for (var i = 0; i < a1.length; i++) {
            a[a1[i]] = true;
        }
        gs.log('1a ' + JSON.stringify(a));
        gs.log('2a ' + a);
        gs.print('3a' + JSUtil.logObject(a));
		        gs.print('4a' + JSUtil.describeObject(a, 'name'));

		
        gs.log(global.JSON().encode(a))

        for (var i = 0; i < a2.length; i++) {
            if (a[a2[i]]) {
                delete a[a2[i]];
            } else {
                a[a2[i]] = true;
            }
        }

        for (var k in a) {
            gs.log('5a ' + k);

            diff.push(k);
        }

        if (a1 < a2) {
            oldBigger = true;
        }

        return [diff, oldBigger];
    },
    type: "Arrays"

});
var test = new Arrays();
gs.log(test.diffArrays(["a", "b", "d"], ["a", "b", "c"]));
var array123 = [1, 2, true]

 

LOG:

32 REPLIES 32

Hi,

did you print the diff?

As per docs the function should work fine

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

No i need to write the Diff in the Work Notes...

I also don't understand how this method should work if I don't pass the parameters, especially since a2 is set to null.

so it look to me that i works with this code:

   var arrayUtil = new ArrayUtil();
        var a1 = oldarray;
        var a2 = newarray;
        var testar = arrayUtil.diff(a1, a2);
 new_work_notes += testar.join('\n');

but not like this
  var arrayUtil = new ArrayUtil();
        var a1 = oldarray;
        var a2 = newarray;
       arrayUtil.diff(a1, a2);
 new_work_notes += arrayUtil.join('\n');

 

 i guess this was my fault but i dont unterstand that scriptinclude....

Hi,

that's because diff() method returns an array and you need to hold that in some variable so 1st script is working

var arrayUtil = new ArrayUtil();
        var a1 = oldarray;
        var a2 = newarray;
        var testar = arrayUtil.diff(a1, a2);
 new_work_notes += testar.join('\n');

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

can you explain how this Method works.

We call this with (a1, a2)

but the Method of that Script include has no Parameters.

So how it handle our passed arrays?

Also in the Method, a2 is set to null!?