Date not older than another date field

I have 2 date field in my opportunties - 

Expected Close Date

and

Start Date

I would like that you cant save the opportunity if Start date is the same or older than Expected Close Date. How do I write that formula?

Parents
  • Hi Lena Andersson,

    I've set up this exact validation by using the addValidationTask method in RecordView and CreateView controllers like this:

    In custom/modules/Opportunities/clients/base/views/record/record.js:

    initialize: function (options) {
    this._super('initialize', [options]);

    app.error.errorName2Keys['check_project_dates'] = 'ERROR_CHECK_PROJECT_DATES';
    this.model.addValidationTask('check_project_dates', _.bind(this._doValidateProjectDates, this));
    },

    _doValidateProjectDates: function (fields, errors, callback) {

    console.log('Validating project dates in record.js');

    let start_date = start_date_c;

    let end_date = end_date_c;

    let expected_close_date = close_date_c;

    if (this.model.get(expected_close_date) && this.model.get(start_date) && this.model.get(end_date)) {

       if (!this.isDateRangeValid(start_date, end_date) || !this.isDateRangeValid(expected_close_date, start_date)) {
          errors[start_date] = errors[start_date] || {};
          errors[start_date].check_project_dates = true;

          app.alert.show('message-id', {
             level: 'error',
             messages: 'Make sure that 1) project start date is before project end date and 2) project start date is after          expected close date',
          autoClose: false
          });
       }
    }

    callback(null, fields, errors);
    },

    /**
    * Is this date range valid?
    * @return {boolean} `true` when start date is before end date, `false` otherwise
    */
    isDateRangeValid: function (start_date, end_date) {
       var start = this.model.get(start_date),
       end = this.model.get(end_date),
       isValid = false;

       if (start && end) {
          if (app.date.compare(start, end) < 1) {
          isValid = true;
          }
       }

       return isValid;
    },

Reply
  • Hi Lena Andersson,

    I've set up this exact validation by using the addValidationTask method in RecordView and CreateView controllers like this:

    In custom/modules/Opportunities/clients/base/views/record/record.js:

    initialize: function (options) {
    this._super('initialize', [options]);

    app.error.errorName2Keys['check_project_dates'] = 'ERROR_CHECK_PROJECT_DATES';
    this.model.addValidationTask('check_project_dates', _.bind(this._doValidateProjectDates, this));
    },

    _doValidateProjectDates: function (fields, errors, callback) {

    console.log('Validating project dates in record.js');

    let start_date = start_date_c;

    let end_date = end_date_c;

    let expected_close_date = close_date_c;

    if (this.model.get(expected_close_date) && this.model.get(start_date) && this.model.get(end_date)) {

       if (!this.isDateRangeValid(start_date, end_date) || !this.isDateRangeValid(expected_close_date, start_date)) {
          errors[start_date] = errors[start_date] || {};
          errors[start_date].check_project_dates = true;

          app.alert.show('message-id', {
             level: 'error',
             messages: 'Make sure that 1) project start date is before project end date and 2) project start date is after          expected close date',
          autoClose: false
          });
       }
    }

    callback(null, fields, errors);
    },

    /**
    * Is this date range valid?
    * @return {boolean} `true` when start date is before end date, `false` otherwise
    */
    isDateRangeValid: function (start_date, end_date) {
       var start = this.model.get(start_date),
       end = this.model.get(end_date),
       isValid = false;

       if (start && end) {
          if (app.date.compare(start, end) < 1) {
          isValid = true;
          }
       }

       return isValid;
    },

Children
No Data