Validation Rule Setup

Introduction

Validation rules improve data quality by preventing users from saving invalid data.

Use validation rules to:

  • Force users to enter values within an acceptable range.

  • Force users to enter date ranges in chronological order.

  • Make fields conditionally required depending on the value of another field.

Prerequisites

  • To set up validation rules, you need:

    • Minimum permissions for the FieldFX modules you are using

    • System permissions:

      • API Enabled

      • Author Apex

      • Customize Application

      • View Setup and Configuration

How it Works

Validation rules run:

When you save a record: When you change the status of a record:
  • If the record passes validation, the record saves.

  • If the record doesn’t pass validation, a warning displays and you can’t save the record until you enter valid data or discard your changes.

  • If the record passes validation, the record transitions to the new status.

  • If the record doesn’t pass validation, a warning displays and you can’t change the status until you enter valid data or discard your changes.

You can add validation rules for use in:

Examples

FieldFX Back Office

Validate the Input Qty field on the Ticket Item object.

( ( (FX5__Input_Qty__c < NULLVALUE(FX5__PBI_Minimum_Quantity__c, 0)) && NOT(ISNULL(FX5__PBI_Minimum_Quantity__c))) || ( (FX5__Input_Qty__c > NULLVALUE( FX5__PBI_Maximum_Quantity__c, 0)) && NOT(ISNULL(FX5__PBI_Maximum_Quantity__c))) )

This rule forces users to select an Input Qty that is greater than or equal to the Minimum Quantity, and less than or equal to the Maximum Quantity.

FieldFX Mobile

Validate the Customer and Price Book lookups on the Job object.

"FX5__Job__c" : {
  "QuoteValidation1": {
    "fullName": "QuoteValidation1",
    "errorConditionFormula": "!ISBLANK(FX5__Quote2__c) && ( ISCHANGED (FX5__CustomerAccount__c ) || ISCHANGED ( FX5__Price_Book__c ) )",
    "errorMessage": "You can't change the customer or price book for jobs created from quotes."
  }
}

This rule prevents users from changing the Customer or Price Book for jobs created from quotes.

Adding Validation Rules

FieldFX Back Office

Scenario

This guide explains how to add a validation rule to the Projected End Date field on the Job object.

This rule forces users to select a Projected End Date that follows the Projected Start Date for a job.

  1. Access FieldFX Back Office.

  2. Switch to Salesforce Classic.

  3. Add the validation rule:

    1. From Setup, enter object in the Quick Find box, then select Objects.

    2. Click Job.

    3. Under Validation Rules, click New.

    4. In Rule Name, enter Projected End Date.

    5. Select Active.

    6. In Description, enter "Prevents an invalid Projected End Date being entered".

    7. Under Error Condition Formula, enter the following formula:

      FX5__Projected_Start_Date__c > FX5__Projected_End_Date__c
    8. In Error Message, enter the following:

      The Projected End Date must follow the Projected Start Date.
    9. In Error Location, complete one of the following and click Save:

      • Select Top of Page to display validation error messages at the top of the page.

      • Select Field and "Projected End Date" to display validation error messages inline next to the Projected End Date field.

  4. Test the validation rule by adding a record and entering invalid data.

    A warning should display when you enter invalid data in the location you specified.

    Test a validation rule thoroughly before activating it.

    A poorly designed validation rule can prevent users from saving or syncing data.

FieldFX Mobile

Validation rules for FieldFX Mobile are managed in the Admin Portal’s Mobile Validations tool. You can use the Mobile Validation to add new rules, modify existing rules, and import rules from FieldFX Back Office

Scenario

This guide explains how to add a validation rule to the Price Book lookup on the Job object.

This rule forces users to select the customer’s price book when adding a job.

  1. Add an FX_Mobile_Rules static resource:

    1. Open a text editor.

    2. Enter the following to name the static resource:

      window.FX_Mobile_Rules = {
        "validationRules" : {
        }
      }
    3. Enter the API name of the object to which the field belongs:

      window.FX_Mobile_Rules = {
        "validationRules" : {
          "FX5__Job__c" : {
          }  }}
      • You can only add validation rules for fields on the Job, Ticket, Ticket Item, and FX Form objects.

      • You can add an unlimited number of validation rules for each supported object.

    4. Enter a name for the validation rule:

      window.FX_Mobile_Rules = {
        "validationRules" : {
          "FX5__Job__c" : {
            "PriceBookValidation1": {
              "fullName": "PriceBookValidation1",
            }
          }
        }
      }
    5. Enter the formula to use for the validation rule:

      window.FX_Mobile_Rules = {
        "validationRules" : {
          "FX5__Job__c" : {
            "PriceBookValidation1": {
              "fullName": "PriceBookValidation1",
              "errorConditionFormula": "(FX5__Price_Book__r.FX5__Account__c <> FX5__CustomerAccount__c) && NOT(ISBLANK(FX5__Price_Book__r.FX5__Account__c)) && NOT(ISBLANK(FX5__CustomerAccount__c))",
            }
          }
        }
      }
      Refer to the Formulas article for a list of supported functions and operators.
    6. Enter the name of a field so that validation error messages display inline next to that field:

      window.FX_Mobile_Rules = {
        "validationRules" : {
          "FX5__Job__c" : {
            "PriceBookValidation1": {
              "fullName": "PriceBookValidation1",
              "errorConditionFormula": "(FX5__Price_Book__r.FX5__Account__c <> FX5__CustomerAccount__c) && NOT(ISBLANK(FX5__Price_Book__r.FX5__Account__c)) && NOT(ISBLANK(FX5__CustomerAccount__c))",
              "errorDisplayField": "FX5__Price_Book__c",
            }
          }
        }
      }

      If you don’t specify an errorDisplayField, validation error messages display at the top of the page.

    7. Enter the message to display when users enter invalid data:

      window.FX_Mobile_Rules = {
        "validationRules" : {
          "FX5__Job__c" : {
            "PriceBookValidation1": {
              "fullName": "PriceBookValidation1",
              "errorConditionFormula": "(FX5__Price_Book__r.FX5__Account__c <> FX5__CustomerAccount__c) && NOT(ISBLANK(FX5__Price_Book__r.FX5__Account__c)) && NOT(ISBLANK(FX5__CustomerAccount__c))",
              "errorDisplayField": "FX5__Price_Book__c",
              "errorMessage": "You must select the customer's price book."
            }
          }
        }
      }
    8. Add braces { } to end the validation rule:

      window.FX_Mobile_Rules = {
        "validationRules" : {
          "FX5__Job__c" : {
            "PriceBookValidation1": {
              "fullName": "PriceBookValidation1",
              "errorConditionFormula": "(FX5__Price_Book__r.FX5__Account__c <> FX5__CustomerAccount__c) && NOT(ISBLANK(FX5__Price_Book__r.FX5__Account__c)) && NOT(ISBLANK(FX5__CustomerAccount__c))",
              "errorDisplayField": "FX5__Price_Book__c",
              "errorMessage": "You must select the customer's price book."
            }
          }
        }
      }

      Every left brace { should have a matching right brace }.

    9. Save the file with "FX_Mobile_Rules" as the file name.

  2. Upload the FX_Mobile_Rules file to FieldFX Back Office:

    1. Access FieldFX Back Office.

    2. Switch to Salesforce Classic.

    3. From Setup, enter static in the Quick Find box, then select Static Resources.

    4. Click *New*l

    5. In Name, enter FX_Mobile_Rules.

    6. In Cache Control, select "Public".

    7. Click Choose File.

    8. Select the FX_Mobile_Rules file and click Save.

      When you upload a new FX_Mobile_Rules file, the validation rules in the new file replace any existing validation rules.
  3. Test the validation rule:

    1. Access FieldFX Mobile.

    2. Sync your data.

      New validation rules don’t activate until after you sync.
    3. Add a record and enter invalid data.

      A warning should display when you enter invalid data.

      Test a validation rule thoroughly before activating it. A poorly designed validation rule can prevent users from saving or syncing data.

Validation Rule Builder

You must define validation rules for FieldFX Back Office and FieldFX Mobile separately.

The Validation Rule Builder makes it easier to add validation rules that work system-wide.

How It Works

When you run the Validation Rule Builder, it:

  • Analyzes FieldFX Back Office to look for validation rules.

  • Creates a new FX5__Mobile_Rules static resource containing every validation rule in FieldFX Back Office.

  • Creates a backup copy of the current FX5__Mobile_Rules file for your org.

  • Replaces the existing FX5__Mobile_Rules file with the new FX5__Mobile_Rules file.

  • Uploads the backup copy of the previous FX5__Mobile_Rules file to the static resource list for your org.

Once the Validation Rule Builder uploads a new FX5_Mobile_Rules file, the validation rules listed in the file work systemwide.

Setting Up the Validation Rule Builder

  1. Access FieldFX Back Office.

  2. Switch to Salesforce Classic.

  3. From Setup, enter remote in the Quick Find box, then select Remote Site Settings.

  4. Click New Remote Site.

  5. In Remote Site Name, enter SFDCToolingAPI.

  6. In Remote Site URL, enter the domain in which your org runs.

    To get the URL for your domain, copy the first part of the URL from the address bar in your browser:

    Use your customized my domain URL that is specific to your org instead of your instance specific URL. You can find your my domain here.

    Don’t use instanced URLs when logging in to Salesforce with code or as a user. When your org is moved to another Salesforce instance, code using the instanced URL breaks. If you find instanced URLs in your code, replace them with your My Domain login URL or the default Salesforce login URL. See Log In to Salesforce with Code for more information.

    The Remote Site URL field is case-sensitive.Make sure the URL you enter uses the correct capitalization.
  7. Click Active and then click Save.

  8. Complete one of the following:

Scheduling the Validation Rule Builder to Run Automatically

Setting up to Automatically Run

  1. Access FieldFX Back Office.

  2. Switch to Salesforce Classic.

  3. From Setup, enter "apex" in the Quick Find box, then select Apex Classes.

  4. Click Schedule Apex.

  5. In Job Name, enter Validation Rule Builder.

  6. In Apex Class, select "ValidationRuleHandler".

  7. Select how often you want the Validation Rule Builder to run.

    Select a frequency of at least once per week.
  8. Click Save

Running the Validation Rule Builder Manually

  1. Access FieldFX Back Office.

  2. Access Workbench.

  3. Select UtilitiesApex Execute.

  4. Enter the following script:

    System.debug(FX5.FXWorkerLauncher.DoWork('FX5.ValidationRuleHandler', null));
  5. Click Execute.

    The Validation Rule Builder:

    • Analyzes FieldFX Back Office to look for validation rules.

    • Creates a new FX5__Mobile_Rules static resource containing every validation rule in FieldFX Back Office.

    • Creates a backup copy of the current FX5__Mobile_Rules file for your org.

    • Replaces the existing FX5__Mobile_Rules file with the new FX5__Mobile_Rules file.

    • Uploads the backup copy of the previous FX5__Mobile_Rules file to the static resource list for your org.

Sample FX_Mobile_Rules File