Practice Examples in Service-now
Practice Examples from Shala Master
Scenario1:-Cancel the incident form submission if user has typed “Credit Card” in short description.
(User OnSubmit Client Script)
function onSubmit() {
//Type appropriate comment here, and begin script below
var desc = g_form.getValue(‘short_description’).indexOf(“Credit Card”) > -1;
if ((desc == true)) {
alert(“Please remove the word ‘Credit Card’ from your short description to Submit the form”);
return false;
} }
Scenario2:-User should not insert or update the incident record if user has entered “password” in
work notes or comments.
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var worknote = current.work_notes.getJournalEntry(1);
var worknoteContent = worknote.split(“(Work notes)\n”);
var lastWorknote = worknoteContent[1];
var comment = current.comments.getJournalEntry(1);
var commentContent = comment.split(“(Additional comments)\n”);
var lastComment = commentContent[1];
if(lastWorknote.indexOf(“password”)>-1){
current.setAbortAction(true);
}
if(lastComment.indexOf(“password”)>-1){
current.setAbortAction(true);
}
})(current, previous);
Scenario3:-Show AddINFO message “Caller and Assigned to are same” if caller and assinged to
users are same.
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
if (current.assigned_to.name == current.caller_id.name) {
gs.addInfoMessage(“The caller and assigned to should not be same”);
}
})(current, previous);
Scenario4:-Show alert “Manager are same for Caller and Assinged to User” if the caller’s manager
and assigned to user’s manager are same for incident.
function onSubmit() {
//Type appropriate comment here, and begin script below
var cman = g_form.getReference(“caller_id”).manager;
var aman = g_form.getReference(“assigned_to”).manager;
if (cman == aman) {
alert(“Manager are same for caller and assigned to user”);
}
}
Scenario 5:-User should not select a user for assinged to field for incident record if user does not
have email id.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === ”) {
return;
}
var email = g_form.getReference(“assigned_to”).email;
if (email == “”) {
alert(“You cannot select user that does not have an email id”);
g_form.clearValue(“assigned_to”);
}
//Type appropriate comment here, and begin script below
}
Scenario6:-Make state readonly if loggedin user and caller are same on incident form.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === ”) {
return;
}
var user = g_user.firstName + ” ” + g_user.lastName;
var caller = g_form.getReference(“caller_id”).name;
if (user == caller) {
g_form.setReadOnly(“state”, true);
}
//Type appropriate comment here, and begin script below
}
Scenario7:-Make state readonly if state=New on incident form.
Using UI Policy
Scenario8:-Set Assinged to= Pavan if category=Network.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === ”) {
return;
}
if (newValue==”network”) {
g_form.setValue(“assigned_to”,”Pavan”);
}
//Type appropriate comment here, and begin script below
}
Scenario 9:-Show alert if the state changed from Resolved to In Progress.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === ”) {
return;
}
if (oldValue == 6 && newValue == 2) {
alert(“The state has now changed from Resolved to In Progress.”);
}
//Type appropriate comment here, and begin script below
}
Scenario10:-Whenever caller changed the comment field for incident set state=In Porogress.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === ”) {
return;
}
var user = (g_user.firstName);
var caller = g_form.getReference(“caller_id”).first_name;
if (user == caller) {
g_form.setValue(“state”,2);
}
//Type appropriate comment here, and begin script below
}
Scenario 11:User should not change the state from Resolved to In Progress without putting comment
on incident form.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === ”) {
return;
}
if (oldValue != 6 ;& newValue != 2)
return;
g_form.setMandatory(‘comments’, true);
}
Scenario 12:- Show Comment and worknote value in AddINFO message after insert or update of
incident record.
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var worknote = current.work_notes.getJournalEntry(1);
var worknoteContent = worknote.split(“(Work notes)\n”);
var lastWorknote = worknoteContent[1];
var comment = current.comments.getJournalEntry(1);
var commentContent = comment.split(“(Additional comments)\n”);
var lastComment = commentContent[1];
gs.addInfoMessage(“The additional comments are: ” + lastComment + ” <br/><br/>The Work Notes are: ” + lastWorknote);
})(current, previous);
Scenario13:-Caller user should not have access to Assignment group and Assigned to field on
incident form.
If user is caller is logged in user he will have assignment group and assigned to as read only
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === ”) {
return;
}
var user = (g_user.firstName);
var caller = g_form.getReference(“caller_id”).first_name;
if (user == caller) {
g_form.setReadOnly(“assigned_to”, true);
g_form.setReadOnly(“assignment_group”, true);
}
}
Scenario 14:- User should see “xxx-xxx-xxxx” in short description on form whenever short description
contains mobile number.
function onSubmit() {
//Type appropriate comment here, and begin script below
var value = g_form.getValue(“short_description”);
var re = /^[0-9]{10}$/;
if (!re.test(value)) {
g_form.setValue(‘short_description’, ‘ xxx-xxx-xxxx’);
// var res = value.replace(re, “xxx-xxx-xxxx”);
// g_form.setValue(“short_description”, res);
}
Scenario 15:- User should not submit the incident form without setting short description and
description field values.
Set the short description and description as mandatory fields.
Scenario16:-Show alert if current loggedin user is member of particular group (Database).
Display Business Rule
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
// var groupID = current.assignment_group;
var groupName = “Database”;
var userID = gs.getUserID();
var gr = new GlideRecord(“sys_user_grmember”);
gr.addQuery(“group.name”, groupName);
gr.addQuery(“user”, userID);
gr.query();
if (gr.next()) {
g_scratchpad.grp = true}
})(current, previous);
On load Client Script
function onLoad() {
//Type appropriate comment here, and begin script below
if (g_scratchpad.grp == true) {
alert(“The user is part of the Database group”);
} else {
alert(“The user is not part of the Database group”);
}
}
And since the System administrator i.e. the current logged in user is part of the database group.
We will get an alert saying “The user is part of the Database group”
