Scripting Interview Questions

What is the use of get method in Glide record?

The ‘get’ method is used to return first record in the result set.
The ‘get’ method can also be used when you know the sys_id of that record. It returns Boolean values.

var grinc = new GlideRecord('incident');
var Value = grinc.get('881bb4156se831005be8883e6b7w2h77'); //any Sys ID
gs.info(Value); // logs true or false
gs.info(grinc.number); // logs Incident Number

What is the use of initialize() ?

It creates an empty record suitable for population before an insert.

var gr = new GlideRecord('incident');
gr.initialize();
gr.short_description = 'servicenowgyan.com';
gr.category = 'software';
gr.insert();

What’s the difference between initialize() and newRecord()?

initialize(): creates an empty record suitable for population before an insert.

var inc = new GlideRecord('incident');
inc.initialize();
gs.print(inc.opened_at.getDisplayValue());

Conclusion: initialize gives no output.

newRecord(): creates a GlideRecord, set the default values for the fields and assign a unique id to the record.

var inc = new GlideRecord('incident');
inc.newRecord();
gs.print(inc.opened_at.getDisplayValue());

Conclusion: newRecord() gives output

How can you fetch the last 5 incidents created?

We can achieve it by using orderByDesc() and setLimit().

var gr = new GlideRecord('incident');
gr.orderByDesc('sys_created_on');
gr.setLimit(5);
gr.query();

How can you get the number of results count in client side?

rows.length is used to get the number of results count in client side.

var gr = new GlideRecord('incident');
gr.addQuery('category', 'hardware');
gr.query();
alert('count of incident: ' + gr.rows.length);

How can you update records without updating “sys” fields like (Updated, Created, etc.) for a particular update?

autoSysFields is used to disable the update of ‘sys’ fields (Updated, Created, etc.).
For Example: when you need to perform a mass update of records to true up some of the data but want to retain the original update timestamps.

var gr = new GlideRecord('incident');
gr.addQuery('category', hardware);
gr.query();
while(gr.next()){  
 gr.autoSysFields(false); // / Do not update sys_updated_by, sys_updated_on, sys_mod_count,   sys_created_by, and sys_created_on
 gr.setWorkflow(false); // do not run any business rule
 gr.category = 'software';
 gr.update();
}

What is the use of setWorkflow() ?

setWorkflow is used to enable/disable the running of any business rules that may be triggered by a particular update.

var gr = new GlideRecord('incident');
gr.addQuery('category', 'hardware');
gr.query();
while(gr.next()){
   gr.category = 'software';
   gr.setWorkflow(false);
   gr.update();
}

How can you fetch list of records where a field value should not be empty?

addNotNullQuery can be used to fetch list of records where a field value should not be empty.

var gr = new GlideRecord('problem');
gr.addNotNullQuery('<Field Name>');
gr.query();

How can you fetch list of records where a field value is empty?

addNullQuery can be used to fetch list of records where a field value is empty.

var gr = new GlideRecord('incident');
gr.addNullQuery('<Field Name>');
gr.query();

How can you stop form submission on Client and Server side?

In client side, simply you will have to use return false.
In server side use the function current.setAbortAction(true);

What is the use of gs.include(‘validators’) ?

Validators is basically a script include that allows you to validate the data coming in when you script is looking for input.
Things like isNumeric, or isInteger or containsOnlyChars
Example is on Priority check:

if (email.body.priority != undefined && isNumeric(email.body.priority))

isNumeric is checked in the script includes – validators – to make sure it’s a number.

Can we call Script includes from Business Rule. If Yes, how?

var ClientsideCall = Class.create();
ClientsideCall.prototype = Object.extendsObject(AbstractAjaxProcessor, {
function sayHello(){
gs.addInfoMessage(current.number);
}
});
//call that script include function from an incident onBefore insert/update Business Rule with the below script:
function onBefore(current, previous) {
     sayHello();
}

And upon an insert or update of an incident it will display the incident number as an Info Message in the screen.

How to copy attachments from RITM to incident?

GlideSysAttachment.copy("sc_req_item", current.parent, "incident", current.sys_id);

How can you call a UI macro through Client Script?

//Macro

<script>
Function abc()
{
	//your code
}
</script>
//Client script:
//You have to call that a function from CS

abc();

How can you call a UI macro through Script Include?

var runner = new GlideJellyRunner();  
var result =  runner.runMacro('UI Macro Name');  
gs.print(result);

How to call script include from the “condition” section of a UI Action?

new Script Include Name().function name();

What is the condition to check which records will be captured within an Update Set?

The Condition is “update_synch=true

Navigate to the sys_dictionary.list.
Personalize the list to include the Attributes column.
Filter on Attributes should be  update_synch=true

How to move customizations to another update set without merging them?

To move the customization, open “sys_update_xml” table and update the “update_set” field with correct update set.

Illustrate g_scratchpad with example.

The g_scratchpad object passes information from the server to the client, such as when the client requires information not available on the form. For example, if you have a client script that needs to access the field u_retrieve, and the field is not on the form, the data is not available to the client script. A typical solution to this situation is to place the field on the form and then always hide it with a client script or UI policy. While this solution may be faster to configure, it is slower to execute.
If you know what information the client needs from the server before the form is loaded, a Display Business Rule can create g_scratchpad properties to hold this information. The g_scratchpad is sent to the client when the form is requested, making it available to all client-side scripting methods. This is a very efficient means of sending information from the server to the client.
For example, assume you open an incident and need to pass this information to the client:
The value of the system property css.base.color
Whether or not the current record has attachments
The name of the caller’s manager

A display business rule sends this information to the client using the following script:

g_scratchpad.css  = gs.getProperty('css.base.color');
g_scratchpad.hasAttachments = current.hasAttachments();
g_scratchpad.managerName  = current.caller_id.manager.getDisplayValue();

To access scratchpad data using a client script:

// Check if the form has attachments
if (g_scratchpad.hasAttachments)
    // do something interesting here
else
    alert('You need to attach a form signed by ' + g_scratchpad.managerName);

How can we declare Workflow scratchpad?

The scratchpad itself is automatically available to an executing workflow and requires no specific declaration. Variables are declared and stored in the scratchpad simultaneously by referencing it.

workflow.scratchpad.<variableName> = <variableValue>;
Or
var myValue = workflow.scratchpad.<variableName>;

What is the use of sysparam_name in GlideAjax?

GlideAjax uses sysparm_name to find which function to use.

What do you mean by Function names starting with “_”?

Function names starting with “_” are considered private and are not callable from the client.

What is the difference between getXML() and getXMLWait() in GlideAjax?

getXML()getXMLWait()
getXML is AsynchronousgetXMLWait() is Synchronous
getXML() is used when you want processing to continue, even if the results have not been returned.getXMLWait() is used when you want to halt processing until the results are returned. This will halt everything and wait for it to finish and return the results
If you are retrieving some data from server and next tasks does not depend on what you are retrieving from server. Then will use getXML().When you are trying to retrieve value of the variable. After getting value then only we can precede next step like comparing it with user input. In such scenarios will use getXMLWait().

What is the use of gsftSubmit()?

gsftSubmit(null, g_form.getFormElement(), “UI Action Id”) triggers the UI Action which is specified in the 3rd parameter, which is the action name/element id.
It is mostly used in UI actions that have a client side and a server side script.
At the end of the client side script, you call gsftSubmit in order to trigger the UI Action again – this time running only the server side code.