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.

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

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

Sohail Khilji
Kilo Patron

Hi @Shahida07 

 

You cannot use glide record in client script. You have to create a script include to check if the condition matches... And get the response back to client script and do the validation.


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

can you help me with the script please.

var LocationChecker = Class.create();
LocationChecker.prototype = {
    initialize: function() {
    },
    checkCoordinatorGroup :function(location)
    {
        var locationGr=new GlideRecord('cmn_location');
        locationGr.addQuery('name',location);
        locationGr.query();
        return true;
       
       
    },

    type: 'LocationChecker'
};
 
CS:
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }

    var location = g_form.getValue('u_location');
    var ga = new GlideAjax('LocationChecker');
    ga.addParam('sysparam_name', 'checkCoordinatorGroup');
    ga.addParam('location',location);
    ga.getXMLAnswer(function(response)
    {
     if(response=='true')
   {
    alert('test');
       }
        });

    }