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.

catalog client script not working in service portal

Thomas99
Tera Contributor
Can someone please help!
I cant get this catalog client script working in service portal, however works fine in the native UI.
 
 
 
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }


    var currentDate = new Date();
    var minStartDate = calculateMinStartDate(currentDate, 2);

    var startDateField = g_form.getControl('start_date'); // Replace 'start_date' with the actual field name
    var selectedStartDate = new Date(startDateField.value);

    if (selectedStartDate < minStartDate) {
        var message = 'Start date must be at least two business days from today';
            g_form.showErrorBox('start_date', message);
        startDateField.value = ''; // Clear the field
    }


    function calculateMinStartDate(currentDate, days) {
        // Add 'days' business days to the current date, excluding weekends
        var minDate = new Date(currentDate);
        while (days > 0) {
            minDate.setDate(minDate.getDate() + 1);
            if (minDate.getDay() !== 0 && minDate.getDay() !== 6) {
                days--;
            }
        }
        return minDate;
    }


}
5 REPLIES 5

Shruti
Mega Sage
Mega Sage

Hi,

g_form.getControl() does not work in Service portal

Sagar Pagar
Tera Patron

Hi @Thomas99,

 

Yes I agree with @Shruti . g_form.getControl() will not work.

 

Can you try updating line as

var startDateField = g_form.getValue('start_date'); // Replace 'start_date' with the actual field name

 

If my response helps you resolve your issue. Kindly mark it as helpful & correct. It will be helpful to future readers! 👍🏻
Thanks,
Sagar Pagar

The world works with ServiceNow

Hello @Thomas99 ,

Service Portal does not support all client scripting APIs that are available in the platform UI. The script you provided uses the g_form.getControl() method, which is not supported in Service Portal. 

1. Remove the line var startDateField = g_form.getControl('start_date'); because g_form.getControl() is not supported in Service Portal.

2. Replace startDateField.value with g_form.getValue('start_date') to get the value of the 'start_date' field.

3. Replace startDateField.value = ''; with g_form.setValue('start_date', ''); to clear the field.

 

 

Here's the modified script:

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

var currentDate = new Date();
var minStartDate = calculateMinStartDate(currentDate, 2);

var selectedStartDate = new Date(g_form.getValue('start_date'));

if (selectedStartDate < minStartDate) {
var message = 'Start date must be at least two business days from today';
g_form.showErrorBox('start_date', message);
g_form.setValue('start_date', ''); // Clear the field
}

function calculateMinStartDate(currentDate, days) {
// Add 'days' business days to the current date, excluding weekends
var minDate = new Date(currentDate);
while (days > 0) {
minDate.setDate(minDate.getDate() + 1);
if (minDate.getDay() !== 0 && minDate.getDay() !== 6) {
days--;
}
}
return minDate;
}
}

 

If my response helps you resolve your issue. Kindly mark it as helpful & correct. It will be helpful to future readers! 

Thanks and Regards,
Abhijeet Pawar.

 

 

Thank you Abhiijeet. 

 

This works and the dates clear, however , I do not get the error on the field.