Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi ServiceNow Community,

I have created this article for the people who are newly introduced to ServiceNow in order to practice some basic scripting questions.

If you miss part I of Basic Scripting Questions and Answers then below is the link for that article.

Basic Scripting Questions and Answers - Part I

 

1) How to access user-related fields without gliding the table.

Solution: -  

gs.getUser().getRecord().getDisplayValue("internal_integration_user")  

Note: - This code will work for the current session only. If you change the value of a field, then it will not give you the new value.

To get the new value you need to log out the user and log in again.

 

2) How to link attachments in the portal.

Solution: -

  • Create an SP widget for that as below.
  • Copy the below code and paste that into the ‘Body HTML template’.
  • Copy past your attachment sys_id in the below code.  
<div>

<!-- your widget template -->

  <p><a href="/sys_attachment.do?sys_id=530d1c44300ecd90b04a5fe3aef1f80b" title="Click Here to Download the Attachment">Click Here to Download the Template</a></p>

</div>  
  • Create a variable of type ‘Custom’. Before Quebec, the custom type was known as ‘Macro’.
  • Select that newly created widget in the widget field.
  • Now it will appear in the portal.
  • If you click on that then the document with that sys ID will download.

3) How to make catalog variables visible to specific catalog tasks using client script: -

Consider I have 2 catalog tasks as below: -

  1. Review
  2. Signoff

If I want 2 variables to be visible on the review task but hide that variable from the Signoff task. (I’m considering here that you have included all the variables from workflow task activity).

 

  • Write onLoad catalog client script
  • Applies on catalog task true.  
function onLoad() {

    if (g_form.getValue('short_description') == "Review") {

        g_form.setDisplay("capacity_planning", true); //Variables want to show at Review task level
        g_form.setDisplay('delivery_required', true);

    } else if (g_form.getValue("short_description") == "Signoff") {

        g_form.setDisplay("capacity_planning", false); //Variables want to show at SignOff task level
        g_form.setDisplay('delivery_required', false);

    }

}  

 

4) Make all the fields read-only on the incident form when the state changes to Closed.

  • Write onChange Client script
  • Field name: State 
if(newValue=='7'){  //Closed state value
var fields = g_form.getEditableFields();

for (var x = 0; x < fields.length; x++) {

    g_form.setReadOnly(fields[x], true);

}  
}

 

5) If you want to implement the same scenario for onLoad and onChange without using two scripts then follow the below steps. (How to use onChange client script as onLoad.)

  • Write onChange Client Script
  • Write-down your onload script inside if (isLoading || newValue === '').
  • Write-down your onchange code outside if (isLoading || newValue === '').  
function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

        if (g_form.getValue('state') == '7') { // Closed State Value

            var fields = g_form.getEditableFields();

            for (var x = 0; x < fields.length; x++) {

                g_form.setReadOnly(fields[x], true);

            }

        }

        return;

    } 

    //Type appropriate comment here, and begin script below

    fields = g_form.getEditableFields();

    for (x = 0; x < fields.length; x++) {

        g_form.setReadOnly(fields[x], true);

    }

 

}  

 

6) Change the number field background color of the incident if the priority is Critical

  • Write onChange Client Script.
  • OnChange of Priority
  • It will work both for onLoad and onChange.
  • If you don’t want it be run on onLoad then remove the code from ‘if (isLoading || newValue === '')’.

Script :-  

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

        var element = g_form.getElement('number');

        if (g_form.getValue('priority') == '1') {

            element.style.backgroundColor = "red";

        }

        return;

    }

 

    //Type appropriate comment here, and begin script below

    element = g_form.getElement('number');

    if (newValue == '1') {

        element.style.backgroundColor = "red";

 

    }else{

                        element.style.backgroundColor = "white";

        }

 

}

 

7) Check if the caller is VIP or not. If Caller is VIP show alert as ‘Caller ‘-Caller Email ID-’ is VIP ’.(Without using GlideAjax)

  • Write onLoad Client Script. (Using getReference in Client Script).

Script:-

function onLoad() {

    //Type appropriate comment here, and begin script below

    var caller = g_form.getReference('caller_id',doAlert);

 

    function doAlert(caller) { //reference is passed into callback as first arguments

                        if(caller.vip=='true'){

        alert('Caller ' + caller.email + ' is VIP');

                        }

    }

}

 

😎 Make the Edit/New button visible to specific Users only, on sys_user_group table without using ACL’s

Solution:-

  • Goto the sys_user_group table
  • Scroll down and click on any field from the related list of group members and click on the list control

find_real_file.png

  • Configure the 2 fields as shown below if the list control doesn't have these 2 fields.

find_real_file.png

  • Write down the below script on both the fields. 
if (gs.getUserID()== '62826bf03710200044e0bfc8bcbe5df1') {  //write sys_id of the users for those you want to give access to edit/add
 answer =false;
} else {
 answer =true;
}  
  • Make sure that 'Omit new button' and 'Omit edit button' checkbox is false.

 

 

9) Make Edit/New button visible to specific Users for Specifi Groups only on the sys_user_group table.

  • Goto the sys_user_group table
  • Scroll down and click on any field from the related list of group members and click on list control

find_real_file.png

  • Configure the 2 fields as shown below if the list control doesn't have these 2 fields.

find_real_file.png

  • Writedown below script on both the fields.
var answer;
if (parent.getUniqueValue() == '287ee6fea9fe198100ada7950d0b1b73') { //Group sys ID
 if (gs.getUserID() == '62826bf03710200044e0bfc8bcbe5df1') {  //User Sys _ID
 answer = false;
 }else
 {
 answer=true;
 }

 

 

 

Please mark my article as helpful and bookmark it for future use.

 

Regards,

Gunjan Kiratkar

Consultant - ServiceNow, Cloudaction

Rising Star 2022

Comments
Anirban11
Tera Explorer

Very Informative

Version history
Last update:
‎04-02-2022 12:07 AM
Updated by: