Basic Scripting Questions and Answers.

 


 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);  

 

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


 

App link : ServiceNow Interview Buddy 

 

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

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); 

Snow PDF

 ServiceNow PDF Try it Yourself » < iframe  src ="https://drive.google.com/drive/folders/1MHcrQb2TJTQtU8zyYZN1koJn5mD3r6YZ?usp=shari...