- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
09-19-2023 09:59 PM - edited 09-20-2023 11:08 PM
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(<PARAMS HERE>);
2nd way:
var testUtil = new TestUtil();
testUtil["testFunction"](<PARAMS HERE>);
If the function name is stored in some property for example then
var callbackFunction = gs.getProperty("<PROPERTY NAME>");
var testUtil = new TestUtil();
testUtil[callbackFunction](<PARAMS HERE>);
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:
- 343 Views