Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi Guys,

Below are some Basic scripting questions and their answers that every beginner must go through. Hit helpful if you get benefited from it and save it in the bookmark for further usage.

 Edit 1:- Check out my new Article for real-life use cases:-

Create Custom Document : Attached it to the current record and Send Over the email to assignment Gro...

 

Question 1:- Create an alert message using client script:-

Answer:- Navigate to System Definition- Client Script - New

find_real_file.png

Question 2 :- Create a business rule to print Hello:-

Answer:-   

Write it before the update.

find_real_file.pngfind_real_file.png

 

Question 3:- Remove UI policy for Problem field and add same validation using client script.

Answer:- 

Navigate to Client Script-New

Write Script as:-

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if(newValue.toString() == 'true'){
g_form.setReadOnly('problem_id', true);
}
else{
g_form.setReadOnly('problem_id', false);
}
}  

 

Question 4:- When the category is Software, the assignment group will be Software. Use business rule.

Answer:-

create before insert business rule on the incident table.

Condition: category == 'software'

Script:

(function executeRule(current, previous /*null when async*/) {
// Add your code here
current.assignment_group.setDisplayValue('Software');
})(current, previous);    

 

Question 5:- Create a common script for emails that will contain INC number, short description, Priority and configuration item:-

Answer:- 

 

Create Email Script.

Create Notification.

Add mail script template in notification body by using:-     

${mail_script:script name}   

 

Script:-

find_real_file.png

 

Question 6:-Auto-Populate user's email and user id when the user changes.

Answer:- 

 

Client Script:-   

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if(newValue == ''){
g_form.clearValue('u_vip_email');
g_form.clearValue('u_user_id');
}
var ga = new GlideAjax('checkRecords');
ga.addParam('sysparm_name', "getUserDetails");
ga.addParam('sysparm_userID', g_form.getValue('u_reference_1'));
ga.getXMLAnswer(function(answer){
var parser = JSON.parse(answer);
alert(answer);
g_form.setValue('u_vip_email', parser.email);
g_form.setValue('u_user_id', parser.user_name);
});
//Type appropriate comment here and begin script below
}   

 

Script Include:-   

var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDetails: function(){
var id = this.getParameter('sysparm_userID');
var obj = {};
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', id);

gr.query();
if(gr.next()){
obj["user_name"] = gr.user_name.toString();
obj["email"] = gr.email.toString();
}
return JSON.stringify(obj);
},
type: 'checkRecords'
});   

 

 

Question 7:-Create a new field Date and add validation for the date field so that it takes only future date.

Answer:- 

Config-Form Design-Add date field

Navigate to UI policy:-

find_real_file.png

 

Script Section:-  

Execute If true:-  

function onCondition(){
g_form.setValue('date',' ');
alert('Enter Valid Date, It cannot be past date');
}   

 

Question 8:- Problem should get created when a 'is problem required?' checkbox is checked and when incident gets resolved and problem id will be stored in problem field. The incident should not get resolved until all incident tasks associated with it gets closed.

Answer:-

Navigate to Business Rules

find_real_file.png

 

Script:-   

 

(function executeRule(current, previous /*null when async*/) {
//check if there are any incident tasks open
var task = new GlideRecord("incident_task");
task.addEncodedQuery("stateIN-5,1,2");
task.query();
if(task.next()) {
gs.addErrorMessage("You cannot close incident if incident tasks are still open.");
current.setAbortAction(true);
}
else
{
var gr = new GlideRecord("problem");
gr.initialize();
gr.short_description=current.short_description;
var prob_id = gr.insert();
current.problem_id=prob_id;
}
})(current, previous);    

 

 

Question 9:-Problem related to the incident should get displayed in a related list on the incident.

Answer:-

Need to create defined relationships
Navigate to Relationship
Applies to Table - incident
Queries from the table - problem
Script:-   

 

(function refineQuery(current, parent) {
// Add your code here, such as current.addQuery(field, value);
current.addQuery('sys_id', parent.problem_id);
})(current, parent);   

 

find_real_file.png

 

Question 10:-When assigned to is filled, populate assignment groups in which assigned to is a member.

Answer:-

Navigate to Script Include-New

 

find_real_file.png

 

Script:-  

 

var BackfillAssignmentGroups = Class.create();
BackfillAssignmentGroups.prototype =
Object.extendsObject(AbstractAjaxProcessor, {
BackfillAssignmentGroup: function(getag) {
var gp = ' ';
var a = getag;
//return all groups if the assigned_to value is empty
if (!a)
return;
//sys_user_grmember has the user to group relationship
var grp = new GlideRecord('sys_user_grmember');
grp.addQuery('user', a);
grp.query();
while (grp.next()) {
gp = grp.group + ',' + gp;
}
// return Groups where assigned to is in those groups we use IN
for lists
return 'sys_idIN' + gp;
},
type: 'BackfillAssignmentGroups'
});  

 

Do a dictionary override for the Assignment Group field & create a new entry for the required table as below.

find_real_file.png

Qualifier script:-   

 

javascript: new BackfillAssignmentGroups().BackfillAssignmentGroup(current.assigned_to)   

 

Question 10:- Make assignment group and assigned to fields editable only to admin and incident manager. For others, these fields should be read-only

Answer:- 

Write a Client Script:-   

 

function onLoad(){
if(!g_user.hasRoleExactly('admin') || !g_user.hasRoleExactly('incident_manager')){
g_form.setReadOnly('assignment_group', true);
g_form.setReadOnly('assigned_to', true);
}
}   

 

Question 11:- There should be at least one incident, task associate, with an incident when state becomes work in progress and resolved.

Answer:-

Write BR for that:-

Condition: State [Changes To] Work In Progress or Resolved

Script:-    

 

(function executeRule(current, previous /*null when async*/) {
// Add your code here
var incTask = new GlideRecord('incident_task');
incTask.addQuery('parent', current.sys_id);
incTask.query();
if(!incTask.next()){
gs.addErrorMessage('Not allowed');
current.setAbortAction(true);
}
})(current, previous);  

 

Regards,

Gunjan

Comments
Gunjan Kiratkar
Kilo Patron
Kilo Patron

If you guys find out it really useful for basics then please hit the Helpful button and bookmark the article.

 

Thanks,

Gunjan

Vrushali6
Mega Contributor

HI @Gunjan Kiratkar  for Question 10:-When assigned to is filled, populate assignment groups in which assigned to is a member.

Iam facing issue can you please help me with this

find_real_file.png

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi,

 

Check if there is any already dictionary override created or not for incident.

It might happen because of that.

 

Regards,

Gunjan

Mohammadi
Mega Explorer

Hi Gunjan,

 

Thank you for sharing this article.

This is very helpful.

I will recommend to share the scenario based question and answers as well both conceptual and scripting details for experience between 4 to 8 years.

Thanks once again.

Regards,

Mohammadi

 

Arya9
Tera Contributor
Hi , This article is very helpful for me. Thanks
rohan raj2
Tera Contributor

Great compilation Gunjan!

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Mohammadi ,

I will definitely create more such articles. till then check out my new article which covers 2 real-life use cases.

Article Link:- Create Custom Document : Attached it to the current record and Send Over the email to assignment Gro...

 

 

Please mark my answer as helpful/correct if it resolves your query.

 

Regards,

Gunjan Kiratkar

Consultant - ServiceNow, Cloudaction

Rising Star 2022

PK16
Tera Expert
It helps me to clear the basic understanding of scripting in ServiceNow. Thanks.
Abhijit4
Mega Sage

Great Article Gunjan, this is really helpful for candidates to prepare for an interview.

 

In case if anyone needs more question and answers, below is the link for android app which provides variety of question and answers which will help you boost your interview preparation.

 

App link : ServiceNow Interview Buddy

 

Website link : servicenowinterviewbuddy.com 

Shinchan_VK
Tera Contributor

Hi Gunjan,

 

I am facing issue in Question 11. The BR is not working in aborting the action. Not sure why. I did exactly what is told in the solution. Can you help here.

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Shinchan_VK ,

Use Below Code :


(function executeRule(current, previous /*null when async*/) {
// Add your code here
var incTask = new GlideRecord('incident_task');
incTask.addQuery('task_effective_number', current.number);
incTask.query();
if(!incTask.next()){
gs.addErrorMessage('Not allowed');
current.setAbortAction(true);
}
})(current, previous);  
keerthana
Tera Contributor

i have doubt in question 6?

 

which table select on client script ?

 

user or incident?

kalaiselvi Ravi
Tera Contributor

Hi Keerthana,

                                 You can select Incident table on Client script.Bcz you are going to make changes with the incident form only.

 

keerthana
Tera Contributor

i will try that code didn't get answer....

kalaiselvi Ravi
Tera Contributor

Hi Keerthana,

 

Can u Please  share the screenshots of  your Client Script & Script Include code

Version history
Last update:
‎02-28-2021 04:19 AM
Updated by: