Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

I have a requirement for the cat item . where i have a date/time field .

pavankumars3585
Tera Contributor

I want to allow users only to select the date .today+3 . 

 
 
 

 

7 REPLIES 7

svirkar420
Tera Guru

Hi @pavankumars3585 , For users to allow selected date after 3 days. You can use UI policy to achieve this.

Create an onChange client script and write this script - 

var dateStr = g_form.getValue('date_field_name'); // replace field name

if (!dateStr)
return;

var selectedDate = new Date(dateStr);
var today = new Date();

selectedDate.setHours(0, 0, 0, 0);
today.setHours(0, 0, 0, 0); // sets time to midnight

var allowedDate = new Date(today);
allowedDate.setDate(today.getDate() + 3);

if (selectedDate.getTime() !== allowedDate.getTime()) {

g_form.addErrorMessage('You must select a date exactly 3 days from today.');
g_form.clearValue('date_field_name'); // replace field name
}

 

This will also clear the value if wrong value is added so from won't be submitted before entering the valid date.

 

If this response helped resolve your issue, please consider marking it as Accepted Solution and giving it a 👍.
This helps others in the community find useful answers more easily.

Regards,
Saurabh V.

Deepak Shaerma
Mega Sage

hi @pavankumars3585 ,

Create one onchange catalog client script, and select your date variable and use the below code .

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

    var selectedDateStr = g_form.getValue('your_date_variable_name'); // Replace with your variable name
    var selectedDate = new Date(selectedDateStr);
    selectedDate.setHours(0, 0, 0, 0);

    // Get Today
    var today = new Date();
    today.setHours(0, 0, 0, 0);

    // Get Target (Today + 3)
    var targetDate = new Date(today);
    targetDate.setDate(today.getDate() + 3);

    // Check if the selected date is Today OR within the next 3 days
    // Logic: selectedDate >= today AND selectedDate <= targetDate
    if (selectedDate.getTime() < today.getTime() || selectedDate.getTime() > targetDate.getTime()) {
        g_form.clearValue('your_date_variable_name');
        g_form.showFieldMsg('your_date_variable_name', 'Please select a date between Today and 3 days from now.', 'error');
    } else {
        g_form.hideFieldMsg('your_date_variable_name');
    }
}

Its_Azar
Kilo Sage

Hi there @pavankumars3585 

 

Try with a onlnoad script 

function onLoad() {
    var d = new Date();
    d.setHours(0,0,0,0);
    d.setDate(d.getDate() + 3);

    var yyyy = d.getFullYear();
    var mm = ('0' + (d.getMonth() + 1)).slice(-2);
    var dd = ('0' + d.getDate()).slice(-2);

    g_form.setValue('delivery_date', yyyy + '-' + mm + '-' + dd);
}

 

☑️ If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.

Kind Regards,
Azar
Serivenow Rising Star
Developer @ KPMG.

poyntzj
Kilo Sage

Use a UI policy for this - quite simple and no need for scripting