Posts

Featured Post

Delete v/s Splice for JavaScript Array

Use "splice" instead of "delete" to delete an item from an array because "Delete" replaces the item with undefined instead of the removing it from the array.


var fruits = ["apple", "banana", "grapes"];
fruits.length; // return 3
delete fruits[1]; // return true
fruits.length; // return 3


Instead

var fruits = ["apple", "banana", "grapes"];
fruits.length; // return 3
fruits.splice(2,1) ;
fruits.length; // return 2

Beautify JSON String

var myObj = {hello: "World"};

JSON.stringify(myObj, null, 4);
// '{
// "hello": "world"
// }'

String and Number Conversion in JavaScript

var myString = "999";

// Convert String to a number
var myNumber = +myString; // → 999
// OR
var myNumber = Number(myString) // → 999

// Convert String to negative number
var myNegativeNumber = -myString; // → -999
// OR
var myNegativeNumber = -Number(myString) // → -999

Show hidden variables on Service Portal using browser console

Since Service Portal uses Bootstrap, You can use those bootstrap specific classes to perform the required action

$('.form-group.ng-hide').removeClass('ng-hide');

Generate random numbers in JS

function generateRandom(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result; }

Clear ServiceNow cache using scirpt

gs.cacheFlush();

Jumbled up JavaScript

Image

ServiceNow - Widget Editor shortcuts (MAC)

Image

Copy JS path from browser devtools

Image
With DevTools (Chrome 72) you can copy JS path from your browser


What is the use of document ID field in ServiceNow

Image
There are multiple ways to implement one-to-many relationship in ServiceNow
Reference FieldsGlideListDocument ID fields ServiceNow has provided "Document ID" field to hold a reference of any table because at some times we have a requirement to hold the reference irrespective of the table.

Document ID lets the user choose the table and the associated record



Use splice() instead of delete for arrays in JS

var words = ["Hello", "World", "and", "some", "random", "text"]
console.log(words.length); // returns 6

delete words[2]
console.log(words.length); // returns 6


Delete replaces the item with "undefined" instead of the removing it from the array

The correct approach to remove an item from an array is to use "splice", The splice() method adds/removes items to/from an array, and returns the removed item(s)

Syntax
array.splice(index, howmany, item1, ....., itemX)


var words = ["Hello", "World", "and", "some", "random", "text"]
console.log(words.length); // returns 6

words.splice(2, 1)
console.log(words.length); // returns 5

JQuery in ServiceNow

Image
In order to use jQuery in ServiceNow you need to use $j or $j_glide



Note: You should avoid manipulating DOM in ServiceNow, Instead use GlideForm API

Find duplicate records in ServiceNow

Find duplicate records using GlideAggregate and groupBy

var importGR = new GlideAggregate('u_import');
importGR.addAggregate('COUNT', 'u_number');
importGR.addNotNullQuery('u_number');
importGR.groupBy('u_number');
importGR.addHaving('COUNT', '>', 1);
importGR.query();
while (importGR.next()) {
  gs.log(importGR.u_number);
}

Why to use get(key) instead of [key] for Python Dictionary?

Accessing value from a dictionary using square brackets will raise an exception if there is no value mapped to that key, here is an example
>>> dict = {'key1':1,'key2':2} >>> dict['key1'] 1 >>> dict['key3'] Traceback (most recent call last): File "", line 1, in dict['key3'] KeyError: 'key3' On the other hand, 'get' will return the default value if there is no value mapped to that key, the default value is 'None' by default
>>> dict = {'key1':1,'key2':2} >>> dict['key1'] 1 >>> a = dict.get('key3) >>> print a 0 To set a custom default value
>>> a = dict.get('key3', 'MyCustomValue')

Checkout specific folder in git

git checkout -- path/to/folder