Oracle CRM On Demand Desktop Administration Guide > Customizing Oracle CRM On Demand Desktop > Process of Customizing Objects in Oracle CRM On Demand Desktop >

Defining the Validation Rules


This task is a step in Process of Customizing Objects in Oracle CRM On Demand Desktop.

Oracle CRM On Demand Desktop allows you to define which Oracle CRM On Demand fields are required to make sure the user completes the form correctly. When you define a field or set of fields as required, they are automatically highlighted in red.

NOTE:  Some fields in Microsoft Outlook are required by default, for example, the Full Name field for Contact records. When a user breaks the native Microsoft Outlook validation rule, the native Microsoft Outlook message is displayed and cannot be customized.

For more information, see Correct Usage of the Forms_xx.XML File and Object ID.

The following procedure describes how to define a validation rule so that both the Full Name and Account fields are required to save a Contact Record. If the user attempts to save a record without first entering or selecting values for these fields, then an error message is displayed.

In this procedure, you use the following files:

  • forms.js. This file requires JavaScript.
  • package_res.xml. This file requires an XML addition to specify the localized message that is displayed when a validation rule is violated.

To define validation rules

  1. Open the forms.js file.
  2. Locate the XML code that defines the fields and scripts used for the Contact form, for example, locate:

    'id="On Demand Contact"'

  3. Define a validation object by adding the following code to the form script:

    var validation = ctx.validator;

    This object is used to define all the validation rules on a form.

  4. Add the rule to this validation object by calling the validate_empty_field function on your validation object. This function accepts five arguments:

    validation.validate_empty_field(field, control, message_id, highlight_control, group);

    where:

    • field. This is the Oracle CRM On Demand field name you are going to specify as Required.
    • control. This is the ID of the control on a form that represents this field.
    • message_id. This is the ID of a string resource in package_res.xml. The string with the specified ID is shown as an error message when the validation fails.
    • highlight_control. If this is set to True, then the control will be highlighted if validation fails. The default value of this is True.
    • group. The name of the validation group. It is not required.
  5. Repeat Step 4 for each required field.
  6. Open the package_res.xml file.
  7. Identify where you want to add the new XML.

    TIP:   Messages and labels are grouped together by comments. Validation rule messages are grouped under the <!-- Script and validation Messages --> comment.

  8. Add a new <str> element to the XML code, for example:

    <str key="msg_first_name_validation">First Name is required.</str>

    The key value must match the message_id argument passed to the validate_unique_fields function.

Oracle CRM On Demand Desktop allows you to validate the format of Oracle CRM On Demand fields, for example, email address, and phone number, and so on. This feature prevents users from entering incorrect data into Oracle CRM On Demand records. If a user enters data in an incorrect format, then Oracle CRM On Demand Desktop displays an error message, and Oracle CRM On Demand Desktop does not save the data.

In this example, the Business Phone field is required to save a Contact record. If the user attempts to save a record with a value that is not in the correct format, then Oracle CRM On Demand Desktop displays an error message.

In this procedure, you use the following files:

  • forms.js. This file requires JavaScript.
  • package_res.xml. This file requires an XML addition to specify the localized message that is displayed when a validation rule is violated.

To define a validation rule for value formats

  1. Open the forms.js file.
  2. Locate the XML code that defines the fields and scripts used for the Contact form, for example:

    'id="On Demand Contact"'

  3. Define a validation object by adding the following code to the form script:

    var validation = ctx.validator;

    This object is used to define all the validation rules on a form.

  4. Write a function that validates the phone number format.

    This function must return True if the value in a field matches a specific pattern. Otherwise, it must return False. For example, this function uses regular expression to validate the Work Phone number (#) field format:

    function validate_phone_number() {
        var phonetest = new RegExp(/^[+]?[0-9]{0,3}[\s]?[\(-. ]?[0-9]{2,3}[\)-. ]?[\s]?[0-9]{3}[-. ]?[0-9]{2}[-.]?[0-9]{2}$/);
        var snapshot = ctx.form.item.snapshot;
        return snapshot["Work Phone #"] != '' ? ( snapshot["Work Phone #"].match(phonetest) != null ? true : false) : true
    }

    You can also use any other methods to validate the field format.

  5. Add the rule to this validation object by calling the add_custom function on your validation object. This function accepts four arguments:

    validation.add_custom(custom_function, array_of_controls, message_id , group);

    where:

    • custom_function. This is the function you wrote in Step 4.
    • array_of_controls. This is the array of IDs of the control on a form that represents fields that must be highlighted if the custom rule is not followed.
    • message_id. This is the ID of a string resource in package_res.xml. The string with the specified ID is shown as an error message when a validation fails.
    • group. This is the name of the validation group. It is not required.
  6. Open the package_res.xml file, and define the required messages.
  7. Identify where you want to add the new XML.

    TIP:   Messages and labels are grouped together by comments. Validation rule messages are grouped under the <!-- Script and validation Messages --> comment.

  8. Add a new <str> element to the XML code, for example:

    <str key="msg_business_phone_validation">Business Phone format is not correct.</str>

    The key value must match the message_id argument passed into your custom function that you wrote in Step 4.

Oracle CRM On Demand Desktop Administration Guide, Version 5.1, Rev A Copyright © 2017, Oracle and/or its affiliates. All rights reserved. Legal Notices.