Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

getDateFromFormat not defined Error in Service Portal.

Rosy
Kilo Contributor

I am getting getDateFromFormat not defined in Service Portal. I have two fields on my form start date and end date. We have also users in Europe where their date format is dd--mm--yyyy

So our requirement is start date should not allow past dates and users should only select a date at least two business days into the future.

And end date should not be less than the start date. Can someone help me with the code.

I have written the following code but i am getting getDateFromFormat not defined.

//this code is written on start date filed

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

var today = new Date();
//g_form.addInfoMessage("Today's date is" + today);

var ga = new GlideAjax('emeaDateRestrict');
ga.addParam('sysparm_name','dateRestrict');
ga.addParam('sysparm_date', g_form.getValue('date_start'));
ga.getXML(ajaxParse);

function ajaxParse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
//g_form.addInfoMessage(answer);//see the full JSON response
answer = JSON.parse(answer);

if((answer <86400 && answer>-86400) || answer==0){
g_form.clearValue('date_start');
g_form.addInfoMessage('Please select a valid date. Please select a date at least two business days into the future..');
}
else if(answer<-86400){
g_form.addInfoMessage('You may not select a previous date. Please select a date at least two business days into the future');
g_form.clearValue('date_start');
}
}
}

//glideajax

var  = Class.create();
emeaDateRestrict.prototype = Object.extendsObject(AbstractAjaxProcessor, {

dateRestrict: function(){


var today = new Date();
var date = this.getParameter('sysparm_date');
var string = "G'day World";
var json = new JSON();
string = string + date + today;
var diffSeconds = gs.dateDiff(today, date, true);
//diffSeconds.addLocalTime(-1);



return json.encode(diffSeconds);


},
type: 'dr'
});

 

/''''''''''''''''''''''''''''''''''''''''' end date script'''''''''''''''''''''''''''''''''''''while using this script i am getting comparedates not defined error 

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

var end = g_form.getValue("date_end");
var start = g_form.getValue("date_start");

if (start == "" || end == "") {
return;
}

var format = g_user_date_time_format;
var isEndBeforeStart = compareDates(start, format, end, format);

if (end < start) {
alert("end must be after start");
g_form.setValue('date_end', '');
}
}

// i tried different code on end date variable after searching in the community, now i am getting getdatefromformart not defined error

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

var end = g_form.getValue("date_end");
var start = g_form.getValue("date_start");
// skip if start or end is blank
if (start == "" || end == "") {
return;
}

// get user's date time display format

var isValidStart = getDateFromFormat(date_start, g_user_date_format);

var isValidEnd = getDateFromFormat(date_end, g_user_date_format);


if (end < start) {
alert("end must be after start");
g_form.setValue('u_end_date', '');
return 1;
}
else{
return 0;
}



}

 

12 REPLIES 12

it's publicly available. Additionally you can point to your own instance and get it.

Took a little while to figure this out but got there in the end, thanks for the help

Ranjith22
Mega Contributor

Thanks a lot guys, i achieved through below client script. 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var user_df = g_user_date_format;
var start_d = g_form.getValue('start date variable name'); //First Date/Time field
var end_d = g_form.getValue('end date variable name'); //First Date/Time field
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getNowDateTimeDiffDays');
ajax.addParam('sysparm_fdt', start_d);
ajax.addParam('sysparm_fdt', end_d);
ajax.getXML(getDiff);


function getDiff(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
//alert(answer);

if (start_d > end_d) {


g_form.clearValue("etime");
g_form.hideFieldMsg('etime', true);
g_form.showFieldMsg('etime', 'End date should be in future then start date ', 'error');
}
else
{
g_form.hideFieldMsg('end date variable name', true);
}

}

}