Scenerios based 'Servicenow Developer' Interview Questions-Coding-1

 1) how to print active incident Records?

Answer:-USE Background scripts

Path:- system navigator>scripts-background

CODE:-

var gr=new GlideRecord("incident");

gr.addQuery("active","true");

gr.query();


while(gr.next()){

gs.print(gr.number);

}


2) print latest 10 incident records those are active and with priority P1?

Answer:-USE Background scripts

Path:- system navigator>scripts-background

CODE:-

var gr=new GlideRecord("incident");

gr.addQuery("active","true");

gr.addQuery("priority",1);

gr.orderByDesc("sys_created_on");

gr.setLimit(10)

gr.query();


while(gr.next()){

gs.print(gr.number);

}



3) find the count of active incidents?

Answer:-USE Background scripts

Path:- system navigator>scripts-background

CODE:-

var gr=new GlideRecord("incident");

gr.addQuery("active","true");

gr.query();


var count=gr.getRowCount();

gs.info("avtive incidents are:- "+count);


4) write code to find a count of active records for each category?

Answer:-USE Background scripts

Path:- system navigator>scripts-background

CODE:-

var gr=new GlideAggregate("incident");

gr.groupBy("category");

gr.addQuery("active",true);

gr.addAggregate("count");

gr.query();

while(gr.next()){

var count=gr.getAggregate();

var cat=gr.getValue("category");

gs.info("categgory of the incident:- "+cat+"  avtive incidents are:- "+count);

}


5) if customer wants to run scripts on every monday and friday?

Answer:-USE scheduled  scripts

Path:- system definaition>scheduled jobs->Automatically run scripts of your choosing

CODE:- conditional checkbox "true"
conditon:-

asnwer=checkcondition();

function checkcondition(){

var gdt=new GlideDateTime();
var dayOfWeek=gdt.DayOfWeekLocalTime();
if(dayOfWeek==1||dayOfWeek==5){
return true;
}
else{
return false;
}
}


6)  concat the value of 2 feilds and display them in one another feild?

Answer:-

Path:- dictionary> new> function feild

CODE:- in function feild
glidefunction:concat(feild1,,,feild2)


7) make the UI action button visible to only assignment group

Answer:-

Path:- ui action>condition

CODE:-gs.getUser().isMemberOf(current.assignment_group);

8) add role to group by using script?

Answer:-

Answer:-USE Background scripts

Path:- system navigator>scripts-background

CODE:-

var role=new GlideRecord("sys_user_has_role");

role.initialize();

role.group="sys_id of the group";

role.role="sys_id of the role";

role.inherits=true;

role.insert();


9) remove the incident records of specific date?

Answer:-USE Background scripts

Path:- system navigator>scripts-background

CODE:-


var gr=new GlideRecord("incident");

gr.addEncodedQuery("addquery here");

gr.query();

while(gr.next()){

gr.deleteRecord();

}

here:- go to incident list use filter and run query then copy that query




10) Disable attachment Icon form incident from using script?

Answer:-USE client script

Path:- system navigator>client script>ONOAD CLIENT SCIPT

CODE:-

g_form.disableAttachments();


11) when the incident is created then create same incident in custom table called u_incident. details of the incident should in ?

Answer:-USE Business rule

Path:- system navigator>Business rule>After Business rule

CODE:-on incident table create BR


var num=current.number;

var cat=current.category;

var short_d=current.short_description;

var d=current.description;

var GrInc=new GlideRecord("u_incident");

GrInc.number=num;

GrInc.category=cat;

GrInc.short_description=short_d;

GrInc.descripton=d;

GrInc.insert();

gs.addInfomassage("incident created ");


12) If the incident short description starts with the string 'Database', set database as category.

Answer:-USE client script

Path:- system navigator>client script>ONCHANGE CLIENT SCIPT

CODE:-on incident table create BR

function onChange(control, oldValue, newValue,
isLoading, isTemplate)
{
if (isLoading || newValue === '') 
{return;}
var short_desc= g_form.getValue('short_description');
var position = short_desc.search('Database');
if (position==0){
g_form.setValue('category','database');
}
}

14) For every hour automatically assign analytics_admin role for users who don't have analytics_admin or analytics_viewer role.

Answer:-USE Scheduled script

Path:- system navigator>Scheduled script

CODE:- 

giveRole();

function giveRole(){

try{

var gruser = new GlideRecord("sys_user");

gruser.query();

while(gruser.next()) {

var grurole = new GlideRecord("sys_user_has_role");

grurole.addQuery("user", gruser.sys_id);

grurole.addQuery("role.name",

"IN","analytics_admin,analytics_viewer");

grurole.setLimit(1);

grurole.query();

if(!grurole.next()){

grurole.initialize();

grurole.user = gruser.sys_id;

grurole.setDisplayValue("role", "analytics_admin");

grurole.insert();

}

}

}

catch(ex){ gs.info(ex);}

}


15) In a problem record, hide Notes section when problem state is equal to '102'.

Answer:-USE client script

Path:- system navigator>client script>ONCHANGE CLIENT SCIPT

CODE:- 

function onChange(control, oldValue, newValue, isLoading,

isTemplate) {

if (isLoading) {

if(g_form.getValue('state') == '102'){

g_form.setSectionDisplay('notes', false);

}

return;

}

if (newValue == "102") {

g_form.setSectionDisplay('notes', false);

} else {

g_form.setSectionDisplay('notes', true);

}

}


16) Calculate elapsed time from the Incident record creation and add the result to duration type field

Answer:-USE Business rule

Path:- system navigator>Business rule>Display Business rule

CODE:-on incident table create BR

(function executeRule(current, previous /*null when

async*/) {

var start = new GlideDateTime(current.sys_created_on);

var stop = new GlideDateTime();

var duration = GlideDateTime.subtract(start,stop);

current.u_duration=duration;

})(current, previous);


18) Update the incident priority to critical with an UI Action

Answer:-USE UI ACTION

Path:- system navigator>UI action

CODE:-function setPriority(){

g_form.setValue(’urgency’,1);

g_form.setValue(’impact’,1);

}

Create an ui action in the incident table, give it a name, choose type form button and select show insert and update also client, create function setPriority() ;add this function to

the Onclick field


17) Update the incident priority to critical with an UI Action and save the record ( client and server side script )

Answer:-USE UI ACTION

Path:- system navigator>UI action

CODE:-

Create an ui action in the incident table, give it a name, choose

type form button and select show insert and update also client,

create function updateToPriority() add this function to the Onclick

field.


function updateToPriority()

{

alert(’you are going to change the priority’);

gsftSubmit(null,g_form.getFormElement(),’update_to_priority’);

}

if ( typeof window==”undefined”)

setPriority();

function setPriority()

{

current.urgency=‘1’;

current.impact =’1’;

current.update();

action.setRedirectURL(current);

}



18) If a user in his profile has position equal to nonagent, the user has ITIL role, remove ITIL role for the user.

Answer:-USE After Business Rule

Path:- system navigator>Business Rule

CODE:-


CONDITION

Position is non-agent(custom feild)


(function executeRule(current, previous /*null when async*/) {

var grrole = new GlideRecord('sys_user_has_role');

grrole.addQuery('user', current.sys_id);

grrole.addQuery('role.name', 'itil');

grrole.query();

if(grrole.next()){

grrole.deleteRecord();

}

})(current, previous);


19) Send a notification to the assigned to of an incident , whenever a custom date field is superieur than the current date time.

Answer:-USE Scheduled script

Path:- system navigator>Scheduled script

CODE:-

Register an trigger.notification event for the incident table, create as well a beta notification triggerd when the event is fired, then write a scheduled script to query the incident table for meeting the condition above, if there are records overdue 30, 60, 90 days , generate overdue reminder from the script,     which will automaticatlly send the notifications.


var grInc = new GlideRecord('incident');

grInc.addActiveQuery();

grInc.query();

while(grInc.next()){

if(new GlideDateTime(gr.reached_date).getNumericValue()

>= new GlideDateTime().getNumericValue()){

gs.eventQueue('trigger.sendoverdueremindernotification'

,grInc,grInc.assigned_to.toString());

}}


20) In the Incident form make the contact type field editable only for Assignment group members.

Answer:-USE Display BR and client script

Path:- system navigator>

CODE:-

Display BR

(function executeRule(current, previous /*null when

async*/) {

g_scratchpad.isMember =

gs.getUser().isMemberOf(current.assignment_group.toString()

);

})(current, previous);


ONLOAD CLIENT SCRIPT

function onLoad(){

if(g_scratchpad.isMember.toString() == 'false'){

g_form.setReadOnly('contact_type', true);

// add few more fields as per your requirement

}

}



SET LOGGED IN USER AS CALLER OF INCIDENT

function onLoad(

{

  // Check if the Caller field is empty

if (!g_form.getValue('caller_id')) 

  // Set the Caller field to the logged-in user's name 

 g_form.setValue('caller_id', g_user.userID); 

 } }


Comments