Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Need Catalog Client script to set error if i choose sunday or saturday on a Date field Catalog Item

Francesco15
Tera Contributor

Hi,i need a Catalog client script or an alternative solution that takes the value of a Date field on the catalog item and returns an error message if the chosen date is a Saturday or Sunday.

Thanks

I tried this but not working :

 

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue === '') {
      return;
   }
var date = new Date(newValue);
//if(isNaN(selectedDate)){
    var dayOfWeek = date.getDay();
    if(dayOfWeek === 0 || dayOfWeek === 6){
    g_form.addErrorMessage('Datum ist kein Werktag');
g_form.clearValue('u_datum_der_vereinbarung');

    } else {
        return true;
    }

   
}
1 ACCEPTED SOLUTION

Its_Azar
Tera Guru
Tera Guru

Hi there @Francesco15 

 

Try this 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    var date = new Date(newValue);
    var dayOfWeek = date.getUTCDay(); /
    if (dayOfWeek === 0 || dayOfWeek === 6) { /
        g_form.addErrorMessage('Datum ist kein Werktag');
        g_form.clearValue('u_datum_der_vereinbarung');
    } else {
        return true;
    }
}

 

If this helps kindly accept the answer thanks much.

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

Kind Regards,

Mohamed Azarudeen Z

Developer @ KPMG

View solution in original post

4 REPLIES 4

Bert_c1
Kilo Patron

Hi,

 

In my instance, using the above in a client script shows Saturday =5 and Sunday = 6

Its_Azar
Tera Guru
Tera Guru

Hi there @Francesco15 

 

Try this 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    var date = new Date(newValue);
    var dayOfWeek = date.getUTCDay(); /
    if (dayOfWeek === 0 || dayOfWeek === 6) { /
        g_form.addErrorMessage('Datum ist kein Werktag');
        g_form.clearValue('u_datum_der_vereinbarung');
    } else {
        return true;
    }
}

 

If this helps kindly accept the answer thanks much.

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

Kind Regards,

Mohamed Azarudeen Z

Developer @ KPMG

SN_Learn
Kilo Patron
Kilo Patron

Hi @Francesco15 ,

 

Your script is working correctly for me, tested in PDI

 

Selected 6th July 2024 Saturday

SN_Learn_0-1720381730256.png

 

Selected 5th July Friday, No error msg appeared

SN_Learn_1-1720381837339.png

 

Again, checked for 14th July 2024 Sunday

SN_Learn_2-1720381888761.png

 

 

Please consider checking the format of date.

 

 

Mark this as Helpful / Accept the Solution if this helps

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

Bert_c1
Kilo Patron

You can easily debug the script, by adding and alert() as in:

 

var date = new Date(newValue);
//if(isNaN(selectedDate)){
    var dayOfWeek = date.getDay();
alert('dayOfWeek = ' + dayOfWeek);