Issue with Catalog Item Client Script – Inconsistent Date Difference Validation

SakshyaR
Tera Contributor

Dear Community,

I am reaching out regarding a catalog item that includes a client-side script used to validate the difference between two date fields. The script is intended to ensure that the difference between these dates does not exceed 30 days.

However, we are currently facing an inconsistent behavior. At times, the validation works correctly, while at other times, it does not trigger or allows date differences greater than 30 days without any error message. This issue occurs randomly, making it difficult to identify a clear pattern.

We have checked the script logic, and it appears to be correct.

Below is the client script that is on change

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
   
    if(g_form.getValue('date_that_the_server') !='')
        {
    g_form.hideFieldMsg('back_to_sleep_status');
   
    var gaDate = new GlideAjax('ANCatalogItemServiceAjax');
    gaDate.addParam('sysparm_name','dateValidationMothBalledSleeperService');
    gaDate.addParam('sysparm_date', newValue);
    gaDate.addParam('sysparm_compare_date', g_form.getValue('date_that_the_server'));
    gaDate.addParam('sysparm_days_to_add', 30);
    gaDate.getXMLAnswer(dateValidation);
}
function dateValidation(answer) {
    if(answer !='validdate'){
        g_form.clearValue('back_to_sleep_status');
        g_form.showFieldMsg('back_to_sleep_status',answer);
    }
}
}
 
Any insights or recommendations would be greatly appreciated.
11 REPLIES 11

J Siva
Tera Sage

Hi @SakshyaR 
You can acheive this using UI policy as below.

JSiva_0-1749645584157.png
But still if you want to do this via client srcipt, then please share you script include logic.

Regards,
Siva

 

Brad Bowman
Kilo Patron
Kilo Patron

We would need to see the Script Include function to check that logic as well.

Muhammad Salar
Giga Sage

Hello,
Share script include function too for better understanding of the issue.

Ankur Bawiskar
Tera Patron
Tera Patron

@SakshyaR 

It's possibly not working due to date format which will be different for different users.

you can the UI policy based approach as shared by @J Siva and the date format will be handled automatically.

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