The CreatorCon Call for Content is officially open! Get started here.

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);
    }
}

 

1 ACCEPTED 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

View solution in original post

13 REPLIES 13

@CesarV_ Did you try this solution?


Raghav
MVP 2023
LinkedIn

CesarV_
Tera Contributor

I'm sorry, I am relatively new to ServiceNow, are I doing this in System UI>UI Policies, or Under the Catalog UI Policies within the Catalog Item?

@CesarV_ Did you try this solution?


Please mark the answer correct/helpful accordingly.


Raghav
MVP 2023
LinkedIn

Ankur Bawiskar
Tera Patron
Tera Patron

@CesarV_ 

no scripting required and when you use UI policy you need not worry on the date format as well.

UI policy will handle that for you.

check response from @RaghavSh for that UI policy condition

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

svirkar420
Tera Guru

Hi @CesarV_ , I had achieved something similar in my recent project.

I had used the onChange() client script instead of the onLOad().

Here is the script-

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

var selectedDate = new Date(newValue);
var today = new Date();selectedDate.setHours(0, 0, 0, 0);
today.setHours(0, 0, 0, 0);var twoWeeksLater = new Date(today);
twoWeeksLater.setDate(today.getDate() + 14);if (selectedDate < twoWeeksLater) {
g_form.addErrorMessage("Please select a date at least 2 weeks from today.");
g_form.clearValue('your_date_field'); 
}
}

replace field names with your actual field name.

 

If this solution helped you Please Mark this solution as accepted and helpful as it will be helpful for other users as well.
Best Regards.
Saurabh V.