Need help in onchange client script

Shahida07
Tera Contributor

I have location filed in form.When I am changing the location field it should check if that location has group in cmn_location table.if it has a group then I should get popup message like do not submit request.How to do this.

 

I tried this but it is not working

 

 

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

        var location = g_form.getDisplayValue('u_location');
        var gr=new GlideRecord('cmn_location');
        gr.addQuery('full_name',location);
        gr.query();
alert(location);
        if(gr.next())
        {
            alert('location');
            varGroup=gr.group;
            if(Group!='')
            {
                spModal.alert('Selected Location has group .');
            }

        }








   



}
1 ACCEPTED SOLUTION

Maddysunil
Kilo Sage

@Shahida07 

Please below updated code:

 

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

    var location = g_form.getDisplayValue('u_location');
    var gr = new GlideRecord('cmn_location');
    gr.addQuery('full_name', location);
    gr.query();

    if (gr.next()) {
        var Group = gr.group; // Corrected variable name
        if (Group !== '') { // Check if group is not empty
        alert('Selected Location has group.'); // Corrected alert message
        }
    }
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

View solution in original post

6 REPLIES 6

Ademir Amaral1
Kilo Sage

Hi @Shahida07 

 

Using Glide Record in the client script is not a good practice, try using a Script Include and then calling a GA in your client script:

var LocationUtils = Class.create();

LocationUtils.prototype = {
initialize: function() {
},

hasGroup: function(location) {
var gr = new GlideRecord('cmn_location');
gr.addQuery('full_name', location);
gr.query();
return gr.next() && gr.group != null;
},

type: 'LocationUtils'
};

--
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}

var location = g_form.getValue('u_location');

var ga = new GlideAjax('LocationUtils');
ga.addParam('sysparm_name', 'hasGroup');
ga.addParam('sysparm_location', location);
ga.getXML(onSuccess);

function onSuccess(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'true') {
g_form.showFieldMsg('u_location', 'Selected location has group', 'error');
}
}
}

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

It is  not working.I tried this.