[Service Portal] How to validate start and end dates in portal

Paul Bayani
Mega Expert

Hi! Appreciate your advice about how to correctly put a date validation in the servical portal.

What I'm trying to achieve:

There are 2 dates in the portal form:

Start Date

End Date

Validation: The end date should not be prior the start date

I tried that using below client script but it did not work.

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var first=g_form.getValue('u_effective_date_of_leave');
var last=g_form.getValue('u_last_worked_day');

if(last<=first){
alert("Last day worked should be on or prior to effective date of leave");
g_form.setValue('u_effective_date_of_leave','');
//Type appropriate comment here, and begin script below
}
}

Anything I'm missing? thanks in advance

11 REPLIES 11

And do make sure "Run scripts in UI type" is set to "All"!

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hello, I tried that but did not work for me unfortunately.

So is it not triggered at all? Did you try debugging? Is the Run Scripts set to "All"?

It's a proven method, so it should be able to work.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Sumanth Now
Tera Expert

Hi Paul,

You can do a GlideAjax to call script include and compare the date and time with current date and time and then get the response back to client script to show the alert accordingly.

Please find the Client Script and Script Include below:

 

Script Type: Client Script

Type: OnChange

Variable/Field Name: end_date

UI Type: All

 

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

 

if (isLoading || newValue == '') {

return;

}

g_form.hideFieldMsg('end_date');

var ga = new GlideAjax('DateTimeFunc');

ga.addParam('sysparm_name', 'compareDates');

ga.addParam('start_date', g_form.getValue('start_date'));

ga.addParam('end_date', g_form.getValue('end_date'));

ga.getXML(getEndDate);

}

 

 

function getEndDate(response) {

 

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

 

if (answer=='true')

g_form.showFieldMsg('end_date','End date should be after start date','error');

}

 

Amend the below Script include to use only the date validation function.

Script Include

Name : DateTimeFunc

Client Callable: true

Application: Global

 

var DateTimeFunc = Class.create();

DateTimeFunc.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

 

addTime: function() {

// Get the Start Date

var start = this.getParameter('start_date');

var timeToAdd = this.getParameter('time_to_add');

var gdt = new GlideDateTime();

gdt.setDisplayValue(start);

 

 

//add time to Start Date

gdt.addSeconds(timeToAdd);

 

//Return the End Date to Client Script

return gdt.getDisplayValue();

},

 

 

addTimeSchedule: function() {

 

 

var start = this.getParameter('start_date');

var timeToAdd = this.getParameter('time_to_add');

var sch = this.getParameter('schedule');

var gdt = new GlideDateTime();

gdt.setDisplayValue(start);

 

 

//Get a schedule by name to calculate duration

var schedRec = new GlideRecord('cmn_schedule');

schedRec.get('name', sch);

if (typeof GlideSchedule != 'undefined')

      var sched = new GlideSchedule(schedRec.sys_id);

else

      var sched = new Packages.com.glide.schedules.Schedule(schedRec.sys_id);

 

 

//Set the amount of time to add (in seconds)

durToAdd = new GlideDuration(timeToAdd*1000);

var newDateTime = sched.add(gdt, durToAdd, '');

 

//Return the new date

return newDateTime.getDisplayValue();

},

 

 

ValidateLeadTime: function() {

var se_start_date = this.getParameter('start_date');

 

 

var opened_date = gs.nowDateTime();

 

var currentDateTime = new GlideDateTime();

currentDateTime.setDisplayValue(opened_date);

 

var start_date = new GlideDateTime();

start_date.setDisplayValue(se_start_date);

 

if (se_start_date!='' && start_date<currentDateTime)

{

return 1; // Start Date Entered is in past

}

else if (se_start_date!='')

{

var dc = new DurationCalculator();

 

dc.setStartDateTime(currentDateTime);

 

if (!dc.calcDuration(14*24*3600))                       // Add 2 weeks

gs.log("*** Error calculating duration");

 

var newDateTime = dc.getEndDateTime();

if (start_date < newDateTime)

{

return 2; // Date entered is within the 2 weeks of lead time

}

}

},

 

 

compareDates: function() {

var chg_start_date = this.getParameter('start_date');

var chg_end_date = this.getParameter('end_date');

 

if (chg_start_date > chg_end_date)

{

return true;

}

return false;

},

 

ValidateBlackOuts: function() {

 

 

var ch_start_date = this.getParameter('start_date');

var start_date = new GlideDateTime();

start_date.setDisplayValue(ch_start_date);

 

 

var g = new GlideRecord('cmn_schedule');

 

 

var q = g.addQuery('name', 'Year-End Freeze');

q.addOrCondition('name','IT Infrastructure Blackouts');

g.query();

 

 

while (g.next()) {

var sched = new GlideSchedule(g.sys_id);

if (sched.isInSchedule(start_date)){

return true;

}

}

return false;

},

});

 

Please Mark Correct and/or mark 👍 Helpful as applicable.

Thanks
Sumanth

Namrata Khabale
Giga Guru

Hey Paul,

 

For Portal Try below code:

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

 

    if (isLoading || newValue == '')

{

          return;

    }

g_form.hideFieldMsg('end_date', true);

var startDate = g_form.getValue('start_date');

if ( newValue < startDate){

g_form.showFieldMsg('end_date',"End date can't be prior to Start date ",'error');

}

   //Type appropriate comment here, and begin script below

}

 

Mark Correct and Helpful if you find my response Worthy!!!

 

Best Regards,


Namrata.