Phone
#NOWTips


5 ServiceNow Tips and Tricks

Have you ever randomly landed upon a ServiceNow blog or community post and thought, wow, I wish I would’ve known these tips earlier? If you answered yes, you aren’t alone. One of my favorite things about the ServiceNow ecosystem is its active online community.  

Turning to your peers and experts online is a great way to share knowledge and stay up-to-date on the platform’s latest features, especially as ServiceNow increasingly rolls out more frequent improvements. As someone who loves sharing and learning new things within ServiceNow, I wanted to use today’s blog as a space to share five platform tips and tricks for you to try.  

Copying Comments from Approval Records to a Related Record 

Recently, a client noticed on a Change request record that they didn’t have visibility to reject comments from the approver. After some digging in my personal dev instance, I found that this wasn’t an out of box functionality. The easiest way to solve this issue is to create a before business rule on the sysapprovals_approval table to copy the comments from the approval record to the change request. The only condition I added to this business rule was to run exclusively when the source table starts with change_request.  Below is the script to include in the advanced tab of the business rule.

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

approvalcomments(); 

function approvalcomments() 

{ 

var grr = new GlideRecord('change_request'); 

grr.get(current.sysapproval); 

grr.comments = 'Approval comment(s):' + '\n' + '\n' + current.comments; 

grr.update(); 

} 

})(current, previous); 

Navigating to a List View

This trick is simple, but when I learned about this tip, it literally blew my mind! Have you ever needed to navigate to a list view of a table, but you didn’t want to navigate away from your current page nor open a new tab? I sure have! Let me share with you one of the simplest tricks.  On the current page, in the navigator, start typing out the table name (example: sc_req_item) then add LIST to the end. Before you press ENTER, make sure the word “list” is in ALL CAPS.  Once you hit enter, the table list view should open up in a new tab. Boom! Next time, you can eliminate the multiple clicks of navigating to a table.

ATF – How to Search for a Member of a Group 

ATF has been around for a little bit now, but I haven’t had time to really dig into it until recently for a client who wanted to build out some tests for some of their catalog items. After a few hours learning the ins and outs of the automated test framework, I landed upon something that was very useful when I created my test. The catalog item I was building the test for consisted of 4+ approval steps. I needed an easier way to find a member of the approval group and then being able to impersonate them.

After some research, I was able to locate the code below. This script also helps to eliminate having to manually locate a user who is a member of the approval group.  What I did was created a server-side script test step copy, pasted the code below, and added the group’s sys id to the second line of the script. Since this script grabs the first user of the approval group you list, the next test step I added was the ‘impersonate a user’, and then, in the picker, I selected the value returned from the server-side script step.  Use the full code snippet below to streamline the search and impersonation steps for your automated testing framework. 
 

(function(outputs, steps, stepResultassertEqual) { 

var group = ''; // sys_id of group inside the quotes 

 

var grMember = new GlideRecord('sys_user_grmember'); 

grMember.addQuery('group', group); 

grMember.query(); 

 
if (grMember.next()) { 
//this will get the first group member record that is returned 

outputs.table = 'sys_user'; 

outputs.record_id = grMember.user; 

 

if (outputs.record_id) { 

stepResult.setOutputMessage("Successfully collected user sys_id"); 

return true; 

} else { 

stepResult.setOutputMessage("Failed to collect user sys_id"); 
//This likely means that the group member record is bad 

return false; 

} 

} else { 

stepResult.setOutputMessage("Failed to find group members"); 
//This likely means that there are no group members 

return false; 

} 

})(outputs, steps, stepResultassertEqual); 

Copy URL UI Action

As a first-timer in the consulting world, there are sometimes requests from clients that leave me scratching my head frustrated and confused thinking “this should be easier, but I’m struggling, why?!” Recently, one of those requests was to create a UI Action to copy the Service Portal URL of a record. I knew I had to create a UI Action, but I was missing one step that was crucial to making the UI Action functional. The missing piece was creating a business rule on the record to push the instance URI to a scratchpad so that the UI Action could retrieve the instance URI. The first script below is the copy UI action, then the second blurb of code is the script for the display business rule that adds the instance URI to a scratchpad.

function copySPLinkToClipboard() { 

var base= g_scratchpad.uri; 

var url = base + "sp?id=ticket&table=task&sys_id=" + g_form.getUniqueValue(); 

copyToClipboard(url); 

} 

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

g_scratchpad.uri = gs.getProperty('glide.servlet.uri'); 

})(current, previous); 

 

Skipping the First Record in a GlideRecord Query 

Now, for our last tip! While creating a mail script to display all of the variables of a record producer, my client wanted to display all of the variables, except the first variable. After some experimenting, I learned that if you add .next(); after your query, it jumps to the next variable in the queue. Below is the script I use to skip the first variable in a record producer and only display the remaining variables. If you want to skip the first two variables, all you need to do is add another .next(); before the while loop. 

(function runMailScript(current, template, email, email_action, event) { 

var gr = new GlideRecord('question_answer'); 

gr.addQuery('table_name',current.getTableName()); 

gr.addQuery('table_sys_id',current.sys_id); 

gr.query(); 

gr.next(); 

 

while(gr.next()) { 

if(gr.value == ''){ 

gr.next(); 

} 

template.print(gr.question.getDisplayValue() + "<br>" + gr.value + "<br><br>"); 

} 

})(current, template, email, email_action, event); 

I hope you enjoyed these five ServiceNow hacks, and have some new takeaways to try out on your own.  If you’re looking for more platform tips and tricks, keep an eye out for our Tips & Hacks blog series, where we compile our favorite platform tricks for your convenience. 

Continue Reading