The CreatorCon Call for Content is officially open! Get started here.

End Date cannot be before start date

Munna1
Tera Contributor

Stop selection of end date before start date. for this I have written an On change client script. But it is not working out.

 

On Change client script :  on Date field

--------------------------------------------------------------------------------------------------------------

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


g_form.hideErrorBox('end_date_travel');

var dateSelected = new Date( moment( g_form.getValue('end_date_travel') , "DD-MM-YYYY").toDate() );
var startDate = new Date( moment( g_form.getValue('start_date_travel') , "DD-MM-YYYY").toDate() );

if (dateSelected.getTime() < startDate.getTime()) {
g_form.setValue('end_date_travel','');
g_form.showErrorBox('end_date_travel','End date must be after start date.');
}
}

4 REPLIES 4

Sebas Di Loreto
Kilo Sage

@Munna1 

Don't complicate your life.

Check the change_request table for client scripts doing exactly that already.

Here is the perfect example: /sys_script_client.do?sys_id=30b44867676222004792adab9485ef9b

 

SebastianDL_0-1671641841530.png

 

It calls a function inside the StartEndDateValidation UI script: /sys_ui_script.do?sys_id=42518fd3672222004792adab9485ef76

 


If I helped you with your case, please click the Thumb Icon and mark as Correct.


Julian12
Mega Guru

I believe this does not work on portal

You can simply create UI Policy to achieve this.

 

When to Run : End date is before Start Date

 

UI Policy Type : All

 

Then go to the script tab of UI Policy and write below code

 

 

g_form.clearValue('<variable_name_of_end_date>') ;

g_form.showFieldMsg('<variable_name_of_end_date>', " Your Message Here", error );

 

Sumanth16
Kilo Patron
Hi @Munna1 ,



Hi,




You need to use GlideAjax to complete this requirement. Refer below scripts




Client script:


function onChange(control, oldValue, newValue, isLoading) {


if(newValue){

var ga = new GlideAjax('SvcCatalogCheckEndDate'); //Name of the Script Include

ga.addParam('sysparm_name', 'chkCatEndDate'); //Name of the function in the script include

ga.addParam('sysparm_date',g_form.getValue('start_date')); //Parameter to pass to the script include

ga.addParam('sysparm_endDate', g_form.getValue('end_date')); //Parameter to pass to the script include

ga.getXML(SvcCatalogCheckEndDateParse);


}

}



//Function that gets the response and will return to the client. You can place your alert in this function



function SvcCatalogCheckEndDateParse(response){



var answer = response.responseXML.documentElement.getAttribute("answer");



if(answer == 'true'){



return;



}



if(g_form.getValue('end_date') != '' && answer == 'false'){



alert(getMessage("The End Date cannot be a date that is before the Planned Start Date"));



g_form.setValue('end_date', '');



}



}





Script include: Should be client callable




var SvcCatalogCheckEndDate = Class.create();




SvcCatalogCheckEndDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {



chkCatEndDate : function() {




var start = this.getParameter('sysparm_date'); //Passing the start date from the client



var end = this.getParameter('sysparm_endDate'); //Passing the end date from the client





var dif = gs.dateDiff(start, end, true); //Get the Different between dates.



if (dif <= 0){



      return false;



}



else



{





      return true;



}





}



});



If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!



Thanks & Regards,
Sumanth Meda