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.

How to set only the future dates to variable

Thrupthi
Tera Expert

Hi,

I have a field called "Expected order delivery date" , it means this date field should be made to select only today's and future days, its should be restricted to select the Past dates. How can i change this only for one catalog item variable to select the future dates.

Can someone please help me here.

15 REPLIES 15

Shobhit Sharma
Giga Contributor

I have write this code on Incident table to accept only today's and future days,

// Script Include //

 

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

date : function()
{
var clientDate = this.getParameter('sysparm_todaysDate');
var currentDate = gs.nowDateTime();


var a = gs.dateDiff(currentDate,clientDate,true);
gs.log('datediff is'+ a);



if( a > 0)
{
return 'wrong' ;

}
else
{
return 'right' ;
}
},



type: 'IncidentOccurence'
});

 

 

//  Client script //

 

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

var ga = new GlideAjax('IncidentOccurence');
ga.addParam('sysparm_name', 'date');
ga.addParam('sysparm_todaysDate', g_form.getValue('u_date'));
ga.getXML(HelloWorldParse);

function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == "wrong")
{
g_form.clearValue('u_date');
g_form.setMandatory('u_date' , true);
alert('Please enter the past and today date only');
}

}

}

Harsh Vardhan
Giga Patron

you can make it more simple

create catalog ui policy and put the condition like screenshot below.

 

find_real_file.png

 

 

condition would be like <data variable> | Before | Today

 

find_real_file.png

 

then put the alert in "execute if true" section and g_form.clearValue('<date variable name>')

 

Note: Now you will only be able to select future and current date.

Hi Harsha,

I have tried this and it is not working...

can you share the screenshot of your ui policy.

because i have used this and it worked perfectly fine