Service Catalog client script variable query and assignment

Sean Addington
Tera Expert

Hello all,

I've been researching and testing a recent request for our instance, and I seem to be running into a wall. Also, I'm just starting to learn the ServiceNow scripting side, so please forgive my ignorance if I'm not approaching this correctly, or if there's something I'm missing from a conceptual standpoint.

At my organization, we have a catalog item that needs to be updated to include a new variable field to capture the e-mail address of the employee noted on a different variable field. The issue I'm running into is when I attempt to run my query, nothing is being returned to assign to the designated variable field.  

If anyone is willing to offer any advice on how to best approach this, I'd greatly appreciate any suggestions.

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

   //Type appropriate comment here, and begin script below
   var emp = g_form.getValue('employees_name');
	
	var empGR = new GlideRecord('sys_user');
	empGR.addQuery('sys_id',emp);
	empGR.query();
	if (empGR.next())
		{
			gform.setValue('employee_email', empGR.email);
		}
}
1 ACCEPTED SOLUTION

Chetan Mahajan
Kilo Sage
Kilo Sage

Hi Saddington,

                       foremost using glide record in client script is not good practice, you have to use GlideAjax and Script Include for server-side code as below

 

Client Script :-

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

    var emp = g_form.getValue('employees_name'); // onchnage emp value below script will get run make sure empployee name is refernce field and refer to sys_user

    var glideAjax = new GlideAjax('MyFavoritesAjax'); // client callbale script include name
    glideAjax.addParam('sysparm_name', 'get_email'); // function name
    glideAjax.addParam('sysparm_user', emp ); // parameter to pass on script include
    glideAjax.getXML(getResults);
}

function getResults(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    g_form.setValue('employee_email', answer);
}

Script Include :(client callable true) 

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

    get_manager: function() {

        var empID = this.getParameter('sysparm_user');
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', empID);
        gr.query();
        if (gr.next()) {
            return gr.email.getDisplayValue();  // or use   return gr.email.toString(); it will return email to client  script as answer
        }
    },

    type: "MyFavoritesAjax"

});

 

Let me know if you get issue in this script

Kindly mark correct and helpful if applicable

 

View solution in original post

8 REPLIES 8

Sagar Pagar
Tera Patron

Hi,

You have to use the get reference method with callback function or glideajax with script include.

Avoid GlideRecord in client scripts.

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Thanks for the quick response, Sagar! I think I was trying a callback function at one point, but it wasn't working either and I deleted that script since it was a mess. I'll research more on the glideajax as well and test with that to hopefully get something working better. Again, I appreciate the quick advice on this.

Hi,

g_form.getReference() function with Callback method.

	var userID = g_form.getReference("employees_name", getEmailID);
		
		function getEmailID(userID) {
			g_form.setValue("employee_email", userID.email);
			
		}	

 

You can used it for single dot walk.

For multiple dot walk use GlideAjax with Client callable Script include suggested by Chetan.

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Thanks again, Sagar. This script does work, but I think Chetan is right with his response, as some additional research on GlideAjax shows that method may be the proper approach for this since I may want to fill in some additional fields that aren't always included on form submissions for us. I do appreciate you taking the time to show where I went wrong on this originally.