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 Kalyani,

your client script call on variable set not on catalog item correct???

Pavankumar_1
Mega Patron

Hi,

have you selected the UI type as ALL?

If not please select UI type as All in Onchange client script.

 

Thanks,

Pavankumar

 

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

Hi Pavan,

 I have selected it to All.

Hi,

please share your script screenshots will not help us.

Try to add below code in your client script and add field message as well in if loop

ajaxRoles.getXMLAnswer(getRolesinfo); //callback function

    function getRolesinfo(response) {
        if (answer == 'false') {
            g_form.clearValue('month_of_overtime');
        }
      
    }

 

if not resolved please share client script and script include or

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

Thanks,

Pavankumar

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

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

    },