Different ways of accessing the Script Include and GlideRecord functions
Lets discuss how can we access the script include or GlideRecord function when the function name is dynamic.
Before that lets understand how can we access the JavaScript object in different ways.
var obj = {
"key1": "value1"
}
If we want to access the value of the key then we can do it in 2 ways like below
1st way:
gs.info(obj.key1);
2nd way:
gs.info(obj["key1"]);
If we have a space in the key like below
var obj = {
"key 1": "value1"
}
Then the 1st way (with . notation) discussed above will give error. But we can use the 2nd way like below
gs.info(obj["key 1"]);
Now the same goes with Script Include and GlideRecord objects
Lets consider
Script Include name: TestUtil
Function name: testFunction
If we want to access the function of the Script Include then we can do it in 2 ways like below
1st way:
var testUtil = new TestUtil();
testUtil.testFunction();
2nd way:
var testUtil = new TestUtil();
testUtil["testFunction"]();
If the function name is stored in some property for example then
var callbackFunction = gs.getProperty("");
var testUtil = new TestUtil();
testUtil[callbackFunction]();
Note: Callback function can be stored anywhere in the system.
Now lets see how can we apply this to the GlideRecord object.
Lets consider we want to query the incident table and get the short description of the first record.
If we want to access the value of short description from the GlideRecord object then we can do it in different ways like below
var grInc = new GlideRecord("incident");
grInc.setlimit(1);
grInc.query();
if(grInc.next()){
//1st way:
gs.info(grInc.short_description);
//2nd way:
gs.info(grInc.getValue("short_description"));
//3rd way:
gs.info(grInc["short_description"]);
}
For more info on this you can watch the video in the link below:
https://www.servicenow.com/community/developer-articles/different-ways-of-accessing-the-script-include-and-gliderecord/ta-p/2676411