Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Background Script Scenario based interview questions.

Community Alums
Not applicable

6)Use a background script to create 100 incidents with different short descriptions such as (test1, test2, …, test100).

var gr= new GlideRecord('incident');

for(i=1; i<=100;i++){

    gr.initialize();

    gr.short_description= "shortDescription" + i;

    gr.insert();

}

gs.print("The new 100 record is crated");

 

7) Print the incident numbers updated last month using an array.

var Inc = [];

var gr = new GlideRecord("incident");

// Properly format the encoded query

gr.addEncodedQuery('sys_updated_onONLast month@javascript&colon;gs.beginningOfLastMonth()@javascript&colon;gs.endOfLastMonth()');

gr.query();

// Use a while loop to iterate over results

while (gr.next()) {

    Inc.push(gr.getValue('number'));

}

// Print the collected incident numbers

gs.print("Incidents updated last month: " + Inc.join(', '));

 

😎 Print the incident numbers where the caller is marked as VIP or NULL VIP, without using an encoded query.

var gr = new GlideRecord("incident");

gr.addQuery('caller_id.vip','=', true);

gr.addQuery('caller_id.vip','!=',"");

gr.query();

gs.print(gr.getRowCount());

while(gr.next())

{

    gs.print(gr.number);

}

9) Using a background script, how can you print duplicate records in the sys_user table?

var gaDupCheck1 = new GlideAggregate('sys_user');

     gaDupCheck1.addQuery('active','true');

     gaDupCheck1.addAggregate('COUNT', 'email');

     gaDupCheck1.groupBy('email');

     gaDupCheck1.addHaving('COUNT', '>', 1);

     gaDupCheck1.query();

     while (gaDupCheck1.next()) {

            gs.print(gaDupCheck1.email);

     }

 

10) Write a background script to resolve all low-priority incidents that are in the InProgress state,

// Create a new GlideRecord object for the incident table

var gr = new GlideRecord('incident');

// Add a query to find incidents that are In Progress and have low priority

gr.addEncodedQuery('state=2^priority=4'); // 'state=2' typically represents 'In Progress' and 'priority=3' typically represents 'Low'

// Query the records

gr.query();

// Loop through each record and update its state to Resolved

while (gr.next()) {

    // Set the incident state to Resolved (assuming 'Resolved' has a value of 3)

    gr.state = 4; // Update this value if your 'Resolved' state has a different value

    // Optionally, set the resolution code and comments

    gr.resolution_code = 'Resolved by Background Script'; // You can modify this as needed

    // Update the record in the database

    gr.update();

    // Print the incident number for reference (optional)

    gs.print('Resolved incident: ' + gr.number);

}

gs.print('All low-priority In Progress incidents have been resolved.');

 

0 REPLIES 0