How to make duration available only after 15 days from the date of requesting

2022_ServiceNow
Tera Expert

Hi Everyone,

I have an use-case where I need to make the duration to be selected after 15 days from the current date. How can I achieve this? Please help!!

Example: If I'm filling this form today, then I want the days from 15 to 29 to be greyed or not possible to select and only days after 15 days user should be able to select.

find_real_file.png

Regards,

Priya

1 ACCEPTED SOLUTION

Pavankumar_1
Mega Patron

Hi,

Please try UI policy if it still not resolved.

1. use relative condition as below and select your date field and give 15 days as shown below.

with this you can select date after 15 days from current date.

find_real_file.png

find_real_file.png

 

Script:

function onCondition() {
    alert("Enter valid date, You can select date after 15 days from today");
    g_form.clearValue('select_the_duration'); //give your variable name

}

 

Hope you it helps you.

Please Mark ✅ Correct/helpful if applicable, Thanks!! 

 

Regards

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

28 REPLIES 28

Hi,

when you select date which is within 15 days then nothing came?

Can you share your script?

regards
Ankur

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

Hi,

find_real_file.png

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

	var selectedDate = new Date(newValue);

	var todayDate = new Date(); // Now
	todayDate.setDate(todayDate.getDate() + 15);

	if (selectedDate.getTime() < todayDate.getTime()) {
		g_form.clearValue('select_the_duration');
		g_form.showErrorBox('select_the_duration', 'Date should be selected 15 days from today');
	}
}

Hi,

check what's coming in alert for both the time values?

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

    var selectedDate = new Date(newValue);

    var todayDate = new Date(); // Now
    todayDate.setDate(todayDate.getDate() + 15);

    alert('seleced date' + selectedDate.getTime());
    alert('15 days ahead time' + todayDate.getTime());
    
    if (selectedDate.getTime() < todayDate.getTime()) {
        g_form.clearValue('select_the_duration');
        g_form.showFieldMsg('select_the_duration', 'Date should be selected 15 days from today', 'error');
    }
}

regards
Ankur

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

Hi Ankur,

find_real_file.png

find_real_file.png

Hi,

update as this

 

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

    var selectedDate = new Date(getDateFromFormat(newValue, g_user_date_format));

    var todayDate = new Date(); // Now
    todayDate.setDate(todayDate.getDate() + 15);

    alert('seleced date' + selectedDate.getTime());
    alert('15 days ahead time' + todayDate.getTime());
    
    if (selectedDate.getTime() < todayDate.getTime()) {
        g_form.clearValue('select_the_duration');
        g_form.showFieldMsg('select_the_duration', 'Date should be selected 15 days from today', 'error');
    }
}

Regards
Ankur

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