On Demand Script Include

In this exercise, you will write an on demand Script Include to validate email address syntax. The Script Include can be used by any scoped application and not just the NeedIt application.

Preparation

Add a string field to the NeedIt table to store an email address.
  1. In Studio, use the Application Explorer to open Forms & UI > Forms > NeedIt [Default view].
  2. In the Field Navigator, switch to the Field Types tab.
  3. Locate the String data type in the Fields Types tab. Click, hold, and drag the String data type to the NeedIt form. Drop the String data type beneath the Requested for field. The Requested for email field
  4. Hover over the new field and click the Edit Properties button (The Edit Properties button is only available on hover) to configure the new field.
  5. Configure the field: The Requested for email field properties.
  6. Close the Properties dialog by clicking the Close button (The close button is in the upper right of the Properties dialog).
  7. Click the Save button in the Form Design header to add the field to the NeedIt table in the database.

Write the On Demand Script Include

  1. Create a Script Include.
    1. In Studio, click the Create Application File button.
    2. In the Filter… field enter the text Script OR select Server Development from the categories in the left hand pane.
    3. Select Script Include in the middle pane as the file type then click the Create button.
  2. Configure the Script Include:
              Name: validateEmailAddress
              API Name: (this field value is automatically populated)
              Application: (this field value is automatically populated)
              Accessible from: All application scopes
              Active: Selected (checked)
              Description: On demand Script Include to validate email address syntax using regular expressions.
  3. Delete the template from the Script field. Copy this script and paste it into the Script field.
    function validateEmailAddress(emailStr){
      // Use JavaScript coercion to guarantee emailStr is a string
      emailStr = emailStr + '';
      // Compare emailStr against the allowed syntax as specified in the regular expression
      // If emailStr has allowed syntax, return true, else return false
      if(emailStr.match(/^(([^<>()\[\]\\.,;:\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,}))$/)){
       return true;
      }
      else {
       return false;
      }
     }
  4. Click the Submit button.

Use the Script Include in a Business Rule

  1. Create a Business Rule.
    1. In Studio, click the Create Application File button.
    2. In the Filter… field enter the text Business OR select Server Development from the categories in the left hand pane.
    3. Select Business Rule in the middle pane as the file type then click the Create button.
  2. Configure the Business Rule:
              Name: Email Address Syntax Validate
              Table: NeedIt [x_<your_company_code>_needit_needit]
              Active: Selected (checked)
              Advanced: Selected (checked)
  3. Switch to the When to run section and continue configuring the Business Rule:
              When: Before
              Insert: Selected (checked)
              Update: Selected (checked)
  4. Click the Submit button.
  5. Switch to the Advanced section.
  6. Copy this script and paste it into the executeRule function in the Script field. Do not overwrite the template; paste the script after the Add your code here comment.
    // Pass the Requested for email to the Script Include.  Store the return
     // value from the Script Include in the isEmail variable
     var isEmail = validateEmailAddress(current.u_requested_for_email);
     // If isEmail is false (email address syntax is not valid) do not save
     // the record.  Write an error message to the screen.
     if(isEmail == false){
      gs.addErrorMessage(current.u_requested_for_email + " is not a valid email address.  You must provide a valid email address.");
      current.setAbortAction(true);
     }
  7. Click the Update button.
QUESTION: You could write the validation logic in the Business Rule instead of using a Script Include. Given that, why use a Script Include? If you aren’t sure, scroll to the Answers section at the bottom of this page.

Test the Business Rule and Script Include

  1. Switch to the main ServiceNow browser window and use the browser’s refresh button to reload ServiceNow.
    QUESTION: Why do you have to reload ServiceNow? If you aren’t sure, scroll to the Answers section at the bottom of this page.
  2. Create a new NeedIt record or open an existing NeedIt record for editing.
  3. Enter an invalid email address, such as hello, in the Requested for email field.
  4. Click the Additional actions menu (Additional actions menu) and select the Save menu item. Do not click the Submit or Update buttons because that will take you away from the form.
  5. Examine the error message.
  6. Enter a different invalid email address, such as an address with no .com at the end, in the Requested for email field.
  7. Click the Additional actions menu (Additional actions menu) and select the Save menu item.
  8. Enter an email address with valid syntax in the Requested for email field.
  9. Click the Additional actions menu (Additional actions menu) and select the Save menu item. There should be no error message.

Answers

Question: Why not write the email syntax validation in the Business Rule and not in the Script Include?
Answer: Many applications or even tables within the same application will have email fields. Instead of writing the same logic for each application or table, create a Script Include that is accessible by all application scopes. This allows you to write the validation script logic only once.
Question: Why do you have to reload the main ServiceNow browser window?
Answer: You added a field to the NeedIt Default view. ServiceNow had to be reloaded to display the new field on the NeedIt form.