Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

I cannot get the "End date" before "start date" client script to work.

mandie
Kilo Contributor

This is my current code for a catalog client script:

function onSubmit()

{

//g_form.addInfoMessage("Before error");

var datefrom = g_form.getValue('u_it_email_ooo_datefrom');

var dateto = g_form.getValue('u_it_email_ooo_dateto');

if(datefrom > dateto)

{

g_form.clearMessages();

g_form.addErrorMessage("End date cannot occur before start date.");

return false;

}

}

It works fine when the end date is before the start date, but only when they are in the same month.

When I change the end date to a day in the month before the start date, it allows me submit. Can anyone help? This is quite urgent because i have a meeting with my manager to put this through to our live system today and cannot get it to work properly!! Please help!!!

7 REPLIES 7

Anurag Tripathi
Mega Patron

Hi Amanda,



Please check the post below , that explaines in detail and with the scripts needed to do what you are trying to do.



Client Script Date/Time Functions


-Anurag

saprem_d
Giga Guru

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;


}




}


});


rajmasurkar
Giga Guru

Hi,



You can have a 'Before' 'Insert' & 'Update' BR to validate the dates, throw an error message and abort the submit action.



When to run should be


          'Start Date' Changes // you can use your start date field name here


OR


          'End Date' Changes. // you can use your end date field name here



Condition in Advanced would be


current.start_date.dateNumericValue() > current.end_date.dateNumericValue()'



and



Script in Advance would be



(function executeRule(current, previous /*undefined when async or display*/) {



gs.addErrorMessage(gs.getMessage("End date of the plan cannot be earlier than the start date"));


current.setAbortAction(true);




})(current, typeof previous != 'undefined' ?   previous : null);




Raj


Worked perfect! Thank you