Client Script VS UI Policies

Client Scripts vs. UI Policies

Client Scripts and UI Policies both execute client-side logic and use the same API. Both are used to manage forms and their fields. When developing an application, how can you decide which client-side script type to use? Use this table to determine which type is best suited to your application’s needs:
Criteria Client Script UI Policy
Execute on form load Yes Yes
Execute on form save/submit/update Yes No
Execute on form field value change Yes Yes
Have access to field’s old value Yes No
Execute after Client Scripts No Yes
Set field attributes with no scripting No Yes
Require control over order of execution *Yes Yes
*Although the Order field is not on the Client Script form baseline you can customize the form to add it.
UI Policies execute after Client Scripts. If there is conflicting logic between a Client Script and a UI Policy, the UI Policy logic applies.

G_FORM Cheat Sheet

Client scripts can be called 

  1. alert('Message'); // To Debug the client script.
  2. g_form.addDecoration('field to add icon to','icon', 'icon label');
    // add an icon from the list of supported icons to the field urgency and the label for the icon.
  3. g_form.addErrorMessage('Something went wrong- Message');
    // add an error message in red with the preferred message.
  4. g_form.addInfoMessage('This is info messaage');
    // Add information message on the top with the message.
  5. g_form.addOption('urgency', '4','Very High Priority');
    // Add an option to the choice list with the value '4' and the display value 'Very High Priority'.
  6. g_form.clearMessages();
    // Clear all the messages that were displayed from addInfoMessage and addErrorMessage etc.,
  7. g_form.clearOptions('impact');
    // Clears all the choice values available, user will see only blank choice list.
  8. g_form.disableAttachments(); / g_form.enableAttachments();
    // enables or disables the attachment option.
  9. g_form.flash('urgency','red', '1');
    // flashes a color on the specified field, used to draw users attention.
  10. var x = g_form.getActionName();
    // returns the latest action name performed
  11. var x = g_form.getBooleanValue('fieldName');
    //Returns false if the field value is false or undefined, otherwise returns true.
  12.  var x = g_form.getControl('fieldName');
    //This is strictly used for mobile application only 

Browser tab Image and Title ServiceNow

If you want to change the default image on ServiceNow browser tab and also the image beside it, change the settings in System Properties - > System.

Change the Browser tab title field and the Banner image field the image saved in the images.

Display message after a record is inserted in ServiceNow

All the database level checks like insert, update, delete or query; on any of the operations if we want to perform something we will have to use business rules.

Mention the below for a business rule and for a script to execute choose check the advanced check box:

When to run : insert
under the actions we can specify a message with out checking the advanced or by writing a script under the advanced section

How to check email id format in ServiceNow

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   //Type appropriate comment here, and begin script below

    var fieldName = "u_email_id"; //Email address field name
    var email = newValue + ''; //Make sure this is a string by appending the "" to it
    var regEx = new RegExp(/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);

    g_form.hideFieldMsg(fieldName);

    if (!regEx.test(email)) {
        g_form.showFieldMsg(fieldName, 'Please enter a valid email address', 'error');
    }
}

Comparing two dates in ServiceNow

If there is an requirement to check if any date field shouldnt be a past date you can use below code.
Make some tweaks and you can use to compare to date form fields as well.

(function executeRule(current, previous /*null when async*/) {
    // rightnow stores the current time
    var rightnow = new GlideDateTime();
    // Create a GlideDateTime object for the When needed date
    var whenNeeded = new GlideDateTime(current.u_when_needed);
  
    // If the When needed date is before rightnow, do not write the record to the database
    // Output an error message to the screen
    if(whenNeeded.before(rightnow)){
        gs.addErrorMessage("When needed date cannot be in the past.  Your request has not been saved to the database.");
        current.setAbortAction(true);
    }
    // Challenge:  Do not allow same-day requests
    // Get the date portion of rightnow and whenNeeded (no timestamp)
    var today = rightnow.getLocalDate();
    var istoday = whenNeeded.getLocalDate();
    // Compare today and istoday to see if they are the same day
    if(today.compareTo(istoday) == 0){
        gs.addErrorMessage("You cannot submit NeedIt requests for today.");
        current.setAbortAction(true);
    }
})(current, previous);