Example of Creating a Custom Validation

If the user enters a new opportunity, then the following code makes sure the close date that the user enters occurs later than the current date:

validator.add_custom(validate_close_date,["close_date"],"msg_opportunity_close_dat
e_validation");

To do the validation, this code calls the following validate_close_date function:

function validate_close_date()
{
  var close_date = new Date(ctx.form.item.snapshot['Primary Revenue Close Date']);
  var original_close_date = new Date(ctx.form.item['Primary Revenue Close Date']);
  var today = new Date();
  var utc_today = new 
Date(today.getUTCFullYear(),today.getUTCMonth(),today.getUTCDate());
  if (close_date != null)
  {
  return close_date < utc_today ? ((original_close_date + 0) == (close_date + 0) ? 
true :
false ) : true;
  }
  else true;
}

For more information, see Customizing Form Functions. The following table decribes the parts of the validate_close_date function.

Code Description
var close_date = new 
Date(ctx.form.item.snapshot['P
rimary Revenue Close Date']);

This code creates the close_date variable and sets the value for this variable to the following value:

new

This code does the following:

  1. References the ctx object.

  2. eferences the form

  3. ccesses the item. In this example, this item is the current record.

  4. Examines the current state of the record.

  5. Retrieves the Primary Revenue Close Date field.

var original_close_date = new
Date(ctx.form.item['Primary 
Revenue Close Date']);

This code defines the original_close_date variable. Siebel CRM Desktop enters into this variable the value that the field contains when the user opens the form. To do this, it references the following items:

  • Ctx object

  • or

  • tem objec

var today = new Date();
var utc_today = new 
Date(today.getUTCFullYear(),
today.getUTCMonth(), 
today.getUTCDate());

This code creates the following variable and enters the current date into this variable:

today

To support the UTC (Coordinated Universal Time) format that Siebel CRM uses, this code uses the following variable to create a UTC date:

today

if (close_date != null)
{
  return close_date < utc_today 
? (
(original_close_date + 0) == 
(close_date + 0) ? true :
false ) : true;
}
else true;

This code does the following validation:

  • Determines if the user completed the field in the client. If the user did not complete the field, then this code returns the following value:

    true

  • If the close_date occurs before the current date, then this code determines if the close date in the snapshot is different from the close date that the user set when the user opened the record:

    • If these close dates are different, then this situation indicates that the user updated the close date. In this situation the close date must occur after today and the code returns the following value:

      false

    • If these close dates are not different, then this situation indicates that the user did not change the date and this code returns the following value:

      true