g_form.clearValue() does not work in the catalog client script

Kalyani35
Tera Guru

HI experts,

Using a onchange client script(onchange of variable month_of_overtime) I want to clear the selected value in the selectbox type of a variable in a variable set based on the response received from the Glide Ajax call. Everything works fine as expected but, as soon as I add the statement to clear the selectbox variable it throws an error on the form.

Please look at the screens shot below.

Screenshot of error:

 

If I remove the clearValue statement the error message does not appear.  What could be the reason for this?

Note: the Script include is returning correct value so nothing is wrong there.

 

Thanks

 

13 REPLIES 13

Hi,

To debug the issue you can use below approach.

If you are using chrome then use ctrl+shift+i to open developer console. check error

please share that screenshot where you found error.

 

Thanks,

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Sagar Pagar
Tera Patron

Hi, Can you inspect the browser and share the error details?

Also, make sure to share the complete scripts here.

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Onchange cs:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var otmonth = g_form.getValue('month_of_overtime');

    var ajaxRoles = new GlideAjax('sn_hr_core.XXXXXXXXXXXXXX');
    ajaxRoles.addParam('sysparm_name', 'isCorrectOvertimeMonth');
    ajaxRoles.addParam('sysparm_app_otmonth', otmonth);

    ajaxRoles.getXML(getRolesInfo);

    function getRolesInfo(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == '0') {
            
            
            g_form.clearValue('month_of_overtime');
            g_form.showFieldMsg("month_of_overtime", "You are allowed to select only past 2 months.", "error");
            
        }
    }

}

 

Script include function:

isCorrectOvertimeMonth: function() {

        var otmonth = this.getParameter('sysparm_app_otmonth');
        var currentTime = new GlideDateTime();

        var currentMonth = currentTime.getMonthUTC();
        if (currentMonth > otmonth) {
            var monthDiff = currentMonth - otmonth;

            if (monthDiff > 2) {
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }

    },

Aziz GUEYE
Tera Contributor

Use this alternatif

g_form.setValue("variable_name", []);

Please check and let me know if still facing the issue.