Ben Sweetser

5 minute read

We are experimenting with new training content delivery methods. This tutorial blog post is one of those experiments. We are very interested in your feedback. Please let us know what you think about this format and the content in the comments below.

Introduction

Fix Scripts, like any other script type on the Now Platform, have a purpose to serve. Fix scripts run server-side JavaScript to make changes required by an application that cannot be captured as an application file. Fix Scripts run automatically when applications are installed from an application repository or from the ServiceNow Store.

Watch this video to see how Fix Scripts run automatically upon installation of a published application.

Users with the script_fix_admin role or the admin role can create and run fix scripts. Some examples of what you can do with Fix Scripts include:

  • Generating sample application data
  • Automatically assigning application roles to groups or users
  • Adjusting the value of a System Property

In this post, you learn to create and test a Fix Script for the NeedIt application. The NeedIt application, which is used to request services and goods from several departments, is already partially built for you. Follow the steps in Exercise: Prepare Instance for Server-side Scripting load the application into your Personal Developer Instance from a source code repository on GitHub.com.

NOTE: This tutorial uses the London release of ServiceNow. The User Interface (UI) has changed in Madrid.

Creating a Fix Script

Create Fix Scripts in Studio by clicking the Create Application File button and selecting Fix Script in the Create Application File dialog.

Outside Studio, set the application scope and use the Application Navigator to open System Definition > Fix Scripts to create and edit Fix Scripts. Click the New button to create a Fix Script.

Configure the Fix Script.

Configuring and Running a Fix Script

The new Fix Script form.

  • Name: Provide a descriptive name for the Fix Script.
  • Active: Select the check box to enable the Fix Script.
  • Unloadable: Select the check box to capture updates made by the Fix Script in an Update Set. Usually you do not want to capture updates made by the script in an Update Set, so the default configuration is Not selected.
  • Run once: Run the Fix Script only upon installation of the application. If Run once is Not selected, then the Fix Script runs every time the application is upgraded.
  • Flush cache: Select the check box to run a cache flush after any applicable Fix Scripts complete. If you do not know if you need to flush the cache after the script runs, leave the Flush cache option Not selected.
  • Before: Run the Fix Script before installing or updating the application instead of after.
  • Description: Describe the purpose of the Fix Script.
  • Script: Enter the server-side JavaScript to execute.

When the Fix Script is saved, click the Run Fix Script button to execute the script. Click the Proceed button to see the results in a dialog when the Fix Script execution completes. View logging statements Click the Proceed in Background for longer running processes.

Create a Fix Script

Users are not Application Files. When you create an application, Users and assignments of Roles to a User do not get captured in the application. Use a Fix Script to create a User and associate the NeedIt application user role to the User.

  1. Open the NeedIt application for editing in Studio if it is not already open.
    1. In the main ServiceNow browser window, use the Application Navigator to open System Applications > Studio.
    2. In the Load Application dialog, click the NeedIt application.
  2. Create the Fix Script record.
    1. In Studio, click the Create Application File button.
    2. In the Filter… field, enter the text fix OR select Server Development from the categories in the left-hand pane.
    3. Select Fix Script in the middle pane as the file type then click the Create button.
  3. Configure the Fix Script.
    1. Name: NeedIt create user and assign role
    2. Description: Create the NeedIt Requester user and assign the role x_58872_needit.needit_user to it.
  4. Copy the script and paste it into the Script field.

        
        // Create the user Edna McPhearson
        var newUser = new GlideRecord('sys_user');
        newUser.user_name = "edna.mcphearson";
        newUser.first_name = "Edna";
        newUser.last_name = "McPhearson";
        newUser.email = "edna@example.com";
        newUser.insert();
        
        // get NeedIt user role
        var niUserRole = new GlideRecord('sys_user_role');
            niUserRole.get('name','x_58872_needit.needit_user');
        
        // Add the x_58872_needit.needit_user role to the Edna's user record
        var userRole = new GlideRecord('sys_user_has_role');
        userRole.user = newUser.getValue('sys_id');
        userRole.role = niUserRole.getValue('sys_id');
        userRole.insert();
        
    
  5. Click the Submit button.

Test the Fix Script

  1. Click the Run Fix Script button.

  2. Click the Proceed button.

  3. When the Run Fix Script dialog shows the Total Time for the execution, click the Close button.

  4. Open the user record.

    1. Use the Application Navigator to open User Administration > Users.

    2. Search for Users whose Name contains edna.

    Use asterisk edna in the search to find users that contain edna.

    1. Click edna.mcphearson in the User ID column to open Edna’s User record.

    2. Verify x_58872_needit.needit_user appears in the Roles related list.

    Edna should have the NeedIt user role.

Did you do the hands-on exercise in this blog? Click here to let us know!

Closing Thoughts

Fix Scripts provide application developers with a way to make updates to the platform that cannot be captured in an application file. Use Fix Scripts with caution and test them before releasing an application.

Did you follow the hands-on parts of this tutorial or did you just read through the post? We are trying to determine interest in this type of content through our feedback form. Let us know “I did the hands-on.” or “I just read through the post.” Be sure to include the blog title in the Tell us more field with your feedback.


Comments