Background Script Scenario based interview questions.

Community Alums
Not applicable

1.Write down the script to find out the Count of Active incidents & group them by category.

// Create a new GlideAggregate object for the 'incident' table

var count = new GlideAggregate('incident');

// Add an active query to include only active records

count.addActiveQuery();

// Add an aggregate function to count the number of records

count.addAggregate('COUNT');

// Group the results by the 'category' field

count.groupBy('category');

//Execute the query

count.query();

// Print the total number of rows returned by the query

gs.print(count.getRowCount());

// Loop through each grouped result

while (count.next()) {

    // Print the category and the count of incidents in that category

    gs.info(count.getDisplayValue('category') + ' - ' + count.getAggregate('COUNT'));

}

2.how to get count of total incidents in system logged

var count = new GlideAggregate('incident');
count.addAggregate('COUNT');
count.query();    
if(count.next()) {
    gs.print(count.getAggregate('COUNT'));
}
3.Write a script to print last 5 incidents created yesterday?
gs.print('Last 5 incidents which are created yesterday are as below:');  // Corrected the escape sequence for tab
var gr = new GlideAggregate('incident');
gr.addEncodedQuery('active=true^sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
gr.setLimit(5);
gr.query();
while (gr.next()) {
    gs.print(gr.number);
}
4. Write a script to show incident count for each state which are updated today?
gs.print('State\tIncident Count');  // Corrected the escape sequence for tab
var grIncident = new GlideAggregate('incident');
grIncident.addEncodedQuery('active=true^sys_updated_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
grIncident.addAggregate('COUNT', 'state');
grIncident.groupBy('state');  // Group by state to get counts for each state
grIncident.query();
while (grIncident.next()) {
    var state = grIncident.state.getDisplayValue();
    var count = grIncident.getAggregate('COUNT', 'state');
    gs.print(state + '\t' + count);
}
5.Write a code snippet to print the incident numbers where the caller's manager field is empty.
var myObj = new GlideRecord('incident');
myObj.addEncodedQuery('caller_id.managerISEMPTY');
myObj.query();
 gs.print(myObj.getRowCount());
while (myObj.next()) {
    gs.print(myObj.number);
}

2 REPLIES 2

Community Alums
Not applicable

can you share more interview question based on Scripting!!
I face one interview question in interview like. 
How to get form server to client in Backgorung script ???

AbhishekG670936
Tera Contributor

you cannot directly call server to client in background script we can achieve this scenario by using glide ajax and script include