Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

onChnage script needs to work iteratively.

Shidhi
Tera Contributor
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
 
 
    var sysid = g_user.userID; //get current user sysid
    var ga = new GlideAjax('global.Check_user_group'); //script include name
    ga.addParam('sysparm_name', 'getgroup'); //function name
    ga.addParam('sysparm_name_sysid', sysid); //passing sysid to server
    ga.getXMLAnswer(getGroup);
 
    function getGroup(response) {
        if (response == 'true') {
            g_form.setReadOnly('domain', true);
        } else {
            g_form.setReadOnly('domain', false);
        }
    }
}
 
 
 
//Type appropriate comment here, and begin script below
 
 
If I change it from user A to user B the script is working but not working vice versa. Any suggestions where i'm missing
1 ACCEPTED SOLUTION

Brian Lancaster
Kilo Patron

Can you share the script include code? This is an on change script. What field is it on change of? Also by using g_user.UserID() you are passing the logged in users sys_id. So if it is on change of a reference field this will not work.

 

So I'm thinking that if this is on change and it is based on a field on your form that is a reference to the user table then this line should be var sysid = newValue; //get current user sysid

View solution in original post

3 REPLIES 3

Brian Lancaster
Kilo Patron

Can you share the script include code? This is an on change script. What field is it on change of? Also by using g_user.UserID() you are passing the logged in users sys_id. So if it is on change of a reference field this will not work.

 

So I'm thinking that if this is on change and it is based on a field on your form that is a reference to the user table then this line should be var sysid = newValue; //get current user sysid

@Brian Lancaster 

 

Var sysid = newValue

 

This solved my issue. Thank you!

Community Alums
Not applicable

Hi @Shidhi ,

Please correct your glideajax syntex. Your callback function is not correct check below script 

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

    //Type appropriate comment here, and begin script below
    var sysid = g_user.userID; //get current user sysid
    alert("here = " + sysid);
    var ga = new GlideAjax('global.Check_user_group'); //script include name
    ga.addParam('sysparm_name', 'getgroup'); //function name
    ga.addParam('sysparm_name_sysid', sysid); //passing sysid to server
    ga.getXMLAnswer(getGroup);

    function getGroup(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert(answer);
        if (answer == 'true' || answer == true) {
            g_form.setReadOnly('domain', true);
        } else {
            g_form.setReadOnly('domain', false);
        }
    }

}

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak