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.

client script auto fill variable value email

Jeff W NZAO B
Mega Guru

Hello,

Please i want to auto fill a variable field which is a single line text depending on a referenced variable field. I created a script include and then a client script. 

script include:

getFunctionEmail: function() {
        try {


            var varBeneficiary = this.getParameter('sysparm_userid');

            var grUser = new GlideRecord('sys_user');
            if (grUser.get(varBeneficiary)) {
                var json = new JSON();
                return json.encode(grUser.getDisplayValue('email'));
            }
        } catch (e) {}
    }

 

client script :

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	
	var ga = new GlideAjax('IncludeName');
		ga.addParam('sysparm_name', 'getFunctionEmail');
		ga.addParam('sysparm_userid', newValue.toString());
		ga.getXML(populateEmail);
  
	function populateEmail(response) {
		 
		  var answer = response.responseXML.documentElement.getAttribute("answer");
	

			
			g_form.setValue("mail_beneficiary", answer.toString().replace(/['"]+/g, ''));
			
		   
	}

	
}

 

1 REPLY 1

Sandeep Rajput
Tera Patron
Tera Patron

@Jeff W NZAO B Please update your script include method as follows and see if it works.

 

getFunctionEmail: function() {
        try {
            var varBeneficiary = this.getParameter('sysparm_userid');
            var grUser = new GlideRecord('sys_user');
            if (grUser.get(varBeneficiary)) {               
                return grUser.getValue('email');
            }
        } catch (e) {}
    }