How to log an Object in ServiceNow?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-20-2021 06:04 AM
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-20-2021 10:05 PM
Hi,
did you print the diff?
As per docs the function should work fine
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-20-2021 10:09 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-20-2021 10:11 PM
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....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-20-2021 10:25 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-20-2021 10:28 PM
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!?
