how i write script include for date is less than end date

mahi6
Tera Contributor

by using client callabel

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

I've provided a script include example above but here is a script to do the check just in the client side.

I have 2 fields, start_date and end_date. Both of type Date.

User's date format may be arbitrary. That is, it may be dd-MM-yyyy or yyyy/MM/dd, or something else as selected by the user.

2 client scripts are necessary. One onChange client script on start_date. Another onChange client script on end_date.

1. start_date client script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    try {
        var endDate = g_form.getValue('end_date');
        if (endDate.length < 1 || newValue.length < 1) {  // if either of the field is empty, don't compare
            return;
        }
        var startDate = new Date(getDateFromFormat(newValue, g_user_date_format));  // get start_date. convert from user's date format
        endDate = new Date(getDateFromFormat(endDate, g_user_date_format)); // get end_date. convert from user's end format
		g_form.hideFieldMsg('end_date');  // hide error message in end_date
        if (startDate.getTime() > endDate.getTime()) {  // compare dates
			g_form.showFieldMsg('start_date', 'start date must be before end date.', 'error');  // show error message
        }
    } catch (err) {
        alert(err.message);
    }
}

2. end_date client script

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

    try {
        var startDate = g_form.getValue('start_date');
        if (startDate.length < 1 || newValue.length < 1) {  // if either of the field is empty, don't compare
            return;
        }
        var endDate = new Date(getDateFromFormat(newValue, g_user_date_format)); // get end_date. convert from user's end format
        startDate = new Date(getDateFromFormat(startDate, g_user_date_format));  // get start_date. convert from user's date format
		g_form.hideFieldMsg('start_date');  // hide error message on start_date if there is any
        if (endDate.getTime() < startDate.getTime()) {  // check if end_date is after start_date
			g_form.showFieldMsg('end_date', 'end date must be after start date.', 'error');  // show error message
        }
    } catch (err) {
        alert(err.message);
    }
}

Execution result:

date format: dd/MM/yyyy

1. start_date = 10/12/2021

find_real_file.png

2. end_date = 09/12/2021

find_real_file.png

3. start_date = 08/12/2021

find_real_file.png

View solution in original post

12 REPLIES 12

The field names are "start_date" and "end_date".

Execution result:

find_real_file.png

find_real_file.png

mahi6
Tera Contributor

StartDate: function(sd,ed) {
// var sd = current.getValue('u_start_date');
// var ed = current.getValue('u_end_date');
if (sd < ed) {
gs.addInfoMessage('date is save');

} else {
gs.addErrorMessage('please enter proper date');
}
},

 

 

BR

var obj = new StartDateAndEndDate();
obj.StartDate(current.start,current.end);

how i call this

The question mentioned client callable so I assumed the dates were coming from client script. In this case, it's necessary to get values being passed from the client using following code 

this.getParameter('<parameter name>')

where <parameter name> matches the parameter defined in the following client script.

ajax.addParam('<parameter name>', newValue);

"current" is used in business rule scripts and refers to the record that's currently being processes. There's no current is script include.

If just started learning server side scripting, it may be better to start with "Scripts - Background". It's possible to execute the scripts directly and see the results.

Raghu Ram Y
Kilo Sage

@mahi 

You can try with the simple below script. Tested working.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var end_date = g_form.getValue('u_end_date');
    if (newValue > end_date) {
        alert("Please enter the start date is lesser than End Date ");
        g_form.clearValue('u_start_date');
        return false;

    }
}

 

@Mahi 

Thanks for marking my response as helpful, I hope it resolves ur issue.. if so please mark my answer as correct and close the thread