Catalog Client Script

CesarV_
Tera Contributor

Hi all,

 

I am trying to configure a Date variable on a Catalog Item form so users can only select a date that is at least two weeks out from the current date. Using a Catalog Client Script, I tried scripting this function, which gives me no errors but seems to not change anything on the form. 

 

Can anyone take a look at my script and tell me what I might be doing wrong? 

 

Example: highlighted dates should be grayed out and not selectable:

CesarV__0-1758897188360.png

 

My Script: 

 

function onLoad() {
    var today = new Date();
    var minDate = new Date();
    minDate.setDate(today.getDate() + 14);

    // Format minDate as yyyy-MM-dd
    var yyyy = minDate.getFullYear();
    var mm = String(minDate.getMonth() + 1).padStart(2, '0');
    var dd = String(minDate.getDate()).padStart(2, '0');
    var formattedMinDate = yyyy + '-' + mm + '-' + dd;

    // Set the min attribute on the date input field
    var dateField = g_form.getControl('requested_fulfillment_date');
    if (dateField) {
        dateField.setAttribute('min', formattedMinDate);
    }
}

 

5 REPLIES 5

GlideFather
Tera Patron

Hi @CesarV_,

I don't think there would be possible way to "disable" the other options.

 

Instead you can add a field or info message clarifying that the date must be within defined range (2 weeks from today) and if user select any date in that range then doesn't allow to submit.

 

It would be:

  • 1a) onLoad catalog client script to show the field message ALWAYS (upon loading the form)
  • 1b) onChange CCS to show if the particular field is displayed conditionally (e.g. user selects a category in another varibale, this date field becomes visible and was not visible when a form was loaded, otherwise 1a) is enough)
  • 2) onSubmit CCS with return false; which makes the submission impossible

Just a small example, you need to process teh 14 days condition better, it is just to show you hwo to disabel submission

if (g_form.getValue('your_date)' > 14) {
return false;
} else {
return true;
}

 

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


TejasSN_LogicX
Tera Contributor

Hi @CesarV_,

You can solve this with an onChange Catalogue Client Script on your date variable. In the script, compare the selected date with today + 14 days. If the chosen date is less, show an error and clear the field. and make field value null ( make sure you mark date field is mandetory) 

 

script -> here my date field name is 'my_date'. so replace it. 

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

    // Today's date
    var today = new Date();
    today.setHours(0, 0, 0, 0);

    // Minimum allowed date (14 days from today)
    var minDate = new Date();
    minDate.setDate(today.getDate() + 14);
    minDate.setHours(0, 0, 0, 0);

    // Selected date from user
    var selectedDate = new Date(newValue);

    if (selectedDate < minDate) {
        // Show error message under the field
       g_form.addErrorMessage("You cannot select a date less than 2 weeks from today");
        g_form.setValue('my_date', '');
    } else {
        // Clear any previous error message when valid
       // g_form.hideFieldMsg('my_date', true);
	   g_form.addInfoMessage("date is valid");
    }
}

Output – this is my script

tejas1111_0-1758972794140.png

if i select the date less than 2 weeks from now (today's date is 27-09-2025)

tejas1111_1-1758972899737.png

Then it shows the error

tejas1111_2-1758972931829.png

 

 

you can use this script as on change or on submit .let me know if you need  script . but on change clientr script would be best solution.

 

 

 

RaghavSh
Kilo Patron

@CesarV_ I believe you dn't need client script in this case, did you try UI policy.

 The condition should be <date variable> "Relative (operator)  after  14 Days from now".

 

Once you select the variable and operator as "relative", you will get other options to select. This way you can achieve this without script login.

 

After the condition, in the run script under "execute if true" , you can throw and alert or clear the field if earlier dat is selected.


Please mark the answer correct/helpful accordingly.


Raghav
MVP 2023
LinkedIn

@RaghavSh you are right, this is excellent advice!

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */