Field values not getting cleared

GBS
Tera Contributor

I have created a Script Include and catalog client script on the Requested for field. But when I remove the requested for files value the remaining field values of location, city etc are not cleared and remaining the same.

 

Script Include: 

var User = Class.create();
User.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkRecordPresent: function() {
        var obj = {};
        var id = this.getParameter('sysparm_userID').toString();
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', id);
        gr.query();
        if (gr.next()) {
            obj['u_reference_1'] = gr.getValue('u_reference_1');
            obj['location'] = gr.getValue('location');
            obj['u_city'] = gr.getValue('u_city');
            obj['state'] = gr.getValue('state');
           
        }
        return JSON.stringify(obj);
    },

    type: 'User'
});
 
Catalog Onchange Client script:
function onChange(control, oldValue, newValue, isLoading) {
    if (newValue == '') {
        return;
    }

    var ga = new GlideAjax('VertivUser');
    ga.addParam('sysparm_name', "checkRecordPresent");
    ga.addParam('sysparm_userID', g_form.getValue('requested_for'));
    ga.getXMLAnswer(function(answer) {
        if (answer != 'not found') {
            var parser = JSON.parse(answer);
            g_form.setValue('req_manager', parser.u_reference_1); 
            g_form.setValue('city', parser.u_city);
            g_form.setValue('state', parser.state);

        }
    });
    //Type appropriate comment here, and begin script below

}
1 ACCEPTED SOLUTION

Ademir Amaral1
Kilo Sage

Hi @GBS 

 

It seems like the issue is that the Catalog Onchange Client script is only triggered when the requested_for field is changed to a non-empty value. When the requested_for field is cleared, the script is not triggered and therefore the remaining fields are not cleared.

To fix this, you can add an additional check in the Catalog Onchange Client script to see if the requested_for field is empty. If it is empty, you can manually clear the remaining fields using the g_form.setValue() method.

Here's an updated version of the Catalog Onchange Client script:

function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
// Clear remaining fields
g_form.setValue('req_manager', '');
g_form.setValue('city', '');
g_form.setValue('state', '');
return;
}

var ga = new GlideAjax('VertivUser');
ga.addParam('sysparm_name', "checkRecordPresent");
ga.addParam('sysparm_userID', g_form.getValue('requested_for'));
ga.getXMLAnswer(function(answer) {
if (answer != 'not found') {
var parser = JSON.parse(answer);
g_form.setValue('req_manager', parser.u_reference_1);
g_form.setValue('city', parser.u_city);
g_form.setValue('state', parser.state);
}
});
}

View solution in original post

1 REPLY 1

Ademir Amaral1
Kilo Sage

Hi @GBS 

 

It seems like the issue is that the Catalog Onchange Client script is only triggered when the requested_for field is changed to a non-empty value. When the requested_for field is cleared, the script is not triggered and therefore the remaining fields are not cleared.

To fix this, you can add an additional check in the Catalog Onchange Client script to see if the requested_for field is empty. If it is empty, you can manually clear the remaining fields using the g_form.setValue() method.

Here's an updated version of the Catalog Onchange Client script:

function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
// Clear remaining fields
g_form.setValue('req_manager', '');
g_form.setValue('city', '');
g_form.setValue('state', '');
return;
}

var ga = new GlideAjax('VertivUser');
ga.addParam('sysparm_name', "checkRecordPresent");
ga.addParam('sysparm_userID', g_form.getValue('requested_for'));
ga.getXMLAnswer(function(answer) {
if (answer != 'not found') {
var parser = JSON.parse(answer);
g_form.setValue('req_manager', parser.u_reference_1);
g_form.setValue('city', parser.u_city);
g_form.setValue('state', parser.state);
}
});
}