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.
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
- 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.
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.
- Open the NeedIt application for editing in Studio if it is not already open.
- In the main ServiceNow browser window, use the Application Navigator to open System Applications > Studio.
- In the Load Application dialog, click the NeedIt application.
- Create the Fix Script record.
- In Studio, click the Create Application File button.
- In the Filter… field, enter the text fix OR select Server Development from the categories in the left-hand pane.
- Select Fix Script in the middle pane as the file type then click the Create button.
- Configure the Fix Script.
- Name: NeedIt create user and assign role
- Description: Create the NeedIt Requester user and assign the role x_58872_needit.needit_user to it.
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 = "email@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();
Click the Submit button.
Test the Fix Script
Click the Run Fix Script button.
Click the Proceed button.
When the Run Fix Script dialog shows the Total Time for the execution, click the Close button.
Open the user record.
Use the Application Navigator to open User Administration > Users.
Search for Users whose Name contains edna.
Click edna.mcphearson in the User ID column to open Edna’s User record.
Verify x_58872_needit.needit_user appears in the Roles related list.
Did you do the hands-on exercise in this blog? Click here to let us know!
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.