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?
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?
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;
},
Hi Lena,
An alternative solution by using native functionality in Sugar, is to create a custom text field in the Opportunities module. The field should be flagged as required and the calculation would look similar to this:
ifElse(or(greaterThan(daysUntil($start_date_c),daysUntil($date_closed)),equal(daysUntil($start_date_c),daysUntil($date_closed))),"Start date valid","")
By marking the field required and calculated, that means it will only have a value in the field if the two date fields meet your requirements. Otherwise, the field will be empty and prevent the record from being saved. The downside to this approach is that the error message delivered to the user will not be obvious on what is wrong when the start date is earlier than the close date. Below is a screenshot showing the error message a user receives when the 'Date Validator' field does not pass the required check:
If you are looking for a more helpful error message when the start date is not valid, Yury's approach would be more suitable.
Thank you Chris! I did a try with your formula but I do wrong somewhere
ifElse(or(greaterThan(daysUntil($plannedstartdate_c),daysUntil($date_closed)),equal(daysUntil($plannedstartdate_c),daysUntil($date_closed))),"plannedstartdate_c","")
I have tried in the last section both with plannedstartdate and plannedstartdate_c
Can you see where it goes wrong?
The formula seems to be ok, which kind of field did you create and subject of such formula?
Both fields are date fields- nots datetime.
It is in the module Opportunity
Best regards
I mean: did you configure such formula inside which field? What is the type of such field?
I did the formula under the Field planned startdate
Its a calculated field
Hämta Outlook för iOS<https://aka.ms/o0ukef>
Hi Lena,
If I am understanding correctly, the formula referencing plannedstartdate_c is defined in the field of the same name. If that is accurate, a formula cannot be self-referencing. In my example, I have the following:
date_closed - Stock date field
start_date_c - Custom date field; non-calculated and defined by the user
date_validator_c - Custom, required text field; calculated with the formula I referenced
If my understanding of your configuration is inaccurate, please provide more details about what is not working as expected with screenshots if possible. Do you receive an error when trying to save the formula? Does the validation not work when saving the opportunity?
One final note is that I am unable to view your Sugar instance. In general, I recommend against publicizing identifiable details about your instance such as the URL as this is a public community.