OnChange Client Script condition - Not sure how to add it to this script

Annette Kitzmil
Tera Guru

Hello,

I have a request which requires the Client Script Below to be changed with a condition that will not run the part that is magenta that sets the business area as highlighted in the screen shot below.  Currently, the script below populates everything on the left side and the business area on the right side on the screen shot below when this is a new case based on the cost center of the Risk analyst. 

 

When another user with a specific role opens the case, they are able to change the Risk Analyst, this in turn is changing the Business Area field, which I want to stay as is.  I have tried a few variations with no luck yet.  So any help would be greatly appreciated.

 

 

AnnetteKitzmil_1-1714582793466.png

 

Client Script:

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

    var ga = new GlideAjax('clientUtilsCustom');
    ga.addParam('sysparm_name', 'getUserDetails');
    ga.addParam('sysparm_nm', newValue);
    ga.getXMLAnswer(getResp);
   
}

function getResp(response) {

    if(response!='false'){
        var dat = response.split(',');
        g_form.setValue('user_name',dat[0]);
        var cost_center = dat[1];
        var bav = '';
        if (cost_center == '6052') {
            bav = 'Retail(Cost Center 6052)';
        } else if (cost_center == '0714') {
            bav = 'COR Support (Cost Center 0714)';
        } else if (cost_center == '6053') {
            bav = 'Digital (Cost Center 6053)';
        } else if (cost_center == '1043') {
            bav = 'TCC Sales (Cost Center 1043)';
        } else if (cost_center == '0198') {
            bav = 'TCC Lending (Cost Center 0198)';
        } else if (cost_center == '0734') {
            bav = 'TCC Business (Cost Center 0734)';
        }
        g_form.setValue('business_area',bav);

    }

}


AJAX Script Include:
getUserDetails: function() {
        var dat = [];
        var usr = new GlideRecord('sys_user');
        usr.addQuery('user_name', this.getParameter('sysparm_nm'));
        usr.query();
        if (usr.next()) {
            dat.push(usr.getValue('sys_id'));
            dat.push(usr.getValue('u_cost_center_number'));
            return dat.toString();
        } else {
            return false;
        }


    },
1 ACCEPTED SOLUTION

Then something like this should work:

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

    var ga = new GlideAjax('clientUtilsCustom');
    ga.addParam('sysparm_name', 'getUserDetails');
    ga.addParam('sysparm_nm', newValue);
    ga.getXMLAnswer(getResp);

}

function getResp(response) {

    if (response != 'false') {
        var dat = response.split(',');
        g_form.setValue('user_name', dat[0]);
        if (g_form.getValue('business_area') == '') {
            var cost_center = dat[1];
            var bav = '';
            if (cost_center == '6052') {
                bav = 'Retail(Cost Center 6052)';
            } else if (cost_center == '0714') {
                bav = 'COR Support (Cost Center 0714)';
            } else if (cost_center == '6053') {
                bav = 'Digital (Cost Center 6053)';
            } else if (cost_center == '1043') {
                bav = 'TCC Sales (Cost Center 1043)';
            } else if (cost_center == '0198') {
                bav = 'TCC Lending (Cost Center 0198)';
            } else if (cost_center == '0734') {
                bav = 'TCC Business (Cost Center 0734)';
            }
            g_form.setValue('business_area', bav);
		}
	}
}

View solution in original post

6 REPLIES 6

You are welcome!

 

 

Connect with me https://www.linkedin.com/in/brad-bowman-321b1567/

Jim Coyne
Kilo Patron

I know you marked the question as answered, but I'm really confused by what you are doing.  And the Client script is far from being efficient performance wise.

 

Questions:

- why do you have a text field to enter in the Risk Analyst ID instead of using a Reference field?

- related to the first question, why populate the Risk Analyst Name field (which would seem to be a Reference field to User) by using a GlideAjax call to get the sys_id of the User record that matches the Risk Analyst ID?  You can setup the Auto-complete for Reference fields to search on other fields (https://docs.servicenow.com/bundle/washingtondc-platform-administration/page/administer/field-admini...) which means your users could enter in the value of "u_cost_center_number" in order to find the User record of the Risk Analyst

- the Reference field can have a Reference qualifier on it to limit what User records can be selected

- do you not have a table that contains your Business Areas?  That would come in handy here

- is the Business Area field a Choice list?  If so, what data is contained in the "value" field for the Choice list items?

- you could set the "value" field to be the same as the data in the "u_cost_center_number" on the User.  That would mean you could get rid of all your if/else if statements

 

As for performance, the big problem is where you are setting the value of the "user_name" field.  Because it is a Reference field (again, I'm assuming), when you set the value of the field with just the sys_id, the system has to go back to the server to get the Display value of that record.  It's always better to use setValue() with 3 parameters when setting the value of a Reference field to avoid that call to the server.  As it is now, you make the Ajax call to the server and once you get the result, another call is made for the display value of the field/record.

 

To use that 3rd parameter of setValue(), you want to retrieve both the sys_id and display value of the record in the Ajax call.  Take a look at this post which shows a better way of returning data from an Ajax call using JSON and not and array: TNT: Returning Data from GlideAjax Calls

 

With all that being said, your Script Include could look something like this instead:

 

 

getUserDetails: function() {
    var result = {};  //use an object instead of an Array, which does not make sense here
    result.sysId = "";
    result.name = "";
    result.costCenter = "";
    var user = new GlideRecord("sys_user");
    if (user.get("user_name", this.getParameter("sysparm_nm")) {
        result.sysId = user.getValue("sys_id");
        result.name = user.getDisplayValue();
        result.costCenter = user.getValue('u_cost_center_number'));
    }
    return JSON.stringify(result);
    },

 

 

 

And the getResponse function could look like:

 

 

function getResponse(response) {
    if (response.sysId != "") {
        g_form.setValue("user_name", response.sysId, response.name);  //no need to go back to the server for the display value
        if (g_form.getValue("business_area") == '') {
            g_form.setValue("business_area", response.costCenter);  //assuming the value of the Choice list items match the "u_cost_center_number" field on the User table
        }
    }
}

 

 

Sorry, but there was a lot to unpack there.  Hopefully this helps a bit.  I could not test the core, but I think it's pretty sound.