Date restriction - earliest 7 working days from now, latest 3 month from today

Guna Shalini M
Tera Expert

I have a date field in a catalog item. It should be restricted within earliest 7 working days from now, latest 3 month from today. Please help on how to implement this @Ankur Bawiskar 

Thanks!

1 ACCEPTED SOLUTION

Guna Shalini M
Tera Expert

I completed it with UI policy

Date relative before 7 days from now or relative after 3 months from now -> Error message will be displayed using Run Script

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

so it should be either within 7 working days from now or 3 month from today?

can you explain this part

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

It should be both -> allow the previous 7 working days from now (Eg : today is 21/02/2022 and should allow until 10/02/2022) and latest 3 months from today. And the other dates should be disabled (if possible, for a better experience)

Hi,

you will have to use onChange client script + GlideAjax for this

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Guna Shalini ,

You can use below catalog client script and script include for that.

Client Script:-   

 

 

find_real_file.png

 

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

//Type appropriate comment here, and begin script below
var checkDate=new GlideAjax('validateDate');
checkDate.addParam('sysparm_name','checkDate');
checkDate.addParam('sysparm_date',newValue);

checkDate.getXML(checkDateFun); //callback function

function checkDateFun(response) {

var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer=='false'){
g_form.clearValue('date');
}
}


}    

 

 

Script Include:-    

 

find_real_file.png

 

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

checkDate:function(){

var SevenDays=new GlideDateTime();
SevenDays.addDays(-7);
var threeMonths=new GlideDateTime();
threeMonths.addMonthsLocalTime(3);
var getDate=new GlideDateTime(this.getParameter('sysparm_date'));
if(getDate>=SevenDays && threeMonths>=getDate)
{
return true;
}else{
return false;
}

},

type: 'validateDate'
});    

 

 

 

Please mark my answer as helpful/correct if it resolves your query.

 

Regards,

Gunjan Kiratkar

Consultant - ServiceNow, Cloudaction

Rising Star 2022

 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy