Service Configuration forms: field client script help

bigbacon
Giga Guru

I am new to trying to do this stuffin Service Now and I have trouble finding the help in the documentation and I come from a web development background. 

 

I am trying to make an onchange script for a specific field in a form so that when it changes, it can go out and get a value from another table and place that value into another field on said form. Can someone point me in the right direction to figure out how to do that?

 

I have the script attached to the form field with on change event already there. I can see the sys_id selection from the field to use to get a value from a table's column.

 

SN is daunting...

2 REPLIES 2

bigbacon
Giga Guru

What I tried to do was copy a similar script/function from somewhere else and I don't know where to go from here. I don't see how you can debug what is going on in the script include file. 

 

my code for the onChange is: 

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

   try {
       
        var ga = new GlideAjax('sn_hr_core.HRUserUtils');
        ga.addParam('sysparm_name', 'getJobTitleCodeHRIS');
        ga.addParam('sysparm_sysid', newValue);
        ga.getXML(doJobTitleCallback);
    } catch (err) {
        alert('error is ' + err);
    }

    function doJobTitleCallback(response) {
		//alert(response.responseXML.documentElement);
        var answer = response.responseXML.documentElement.getAttribute("answer");
		alert(answer);
        answer = JSON.parse(answer); //Transform the JSON string to an object
        g_form.setValue('u_job_code_title', answer.jobcodeHRIS);
        
   }

   //Type appropriate comment here, and begin script below
   //alert(newValue + ' ' + control);
}

 

my function in the include file is:

getJobTitleCodeHRIS: function() {
		var sysid = this.getParameter('sysparm_sysid');
		//alert('we are in the function');
		var ghjobGR = new GlideRecord('sn_hr_core_greenhouse_job_import');
        ghjobGR.addQuery('sys_id', sysid);
		gs.warn(sysid);
        ghjobGR.query();
		if(ghjobGR.next()) {
			var objReturn = {
				jobcodeHRIS : ghjobGR.JobCodeHRIS.toString()
			};
			gs.warn(JSON.stringify(objReturn));
			return JSON.stringify(objReturn);
		}		
	},

 

Nothing seems to get returned. I copied this from a function that pulled data from a record in another tablew which looked like this

 

getUserData: function() {
        var userSysId = this.getParameter('sysparm_usersysid');
        //gs.warn("PEK " + userSysId);
        var profileGR = new GlideRecord('sn_hr_core_profile');
        profileGR.addQuery('user', userSysId);
        profileGR.query();
        if (profileGR.next()) {
            var userObj = {
                phone_number: profileGR.work_phone.getDisplayValue(),
                email_address: profileGR.user.email.getDisplayValue(),
                date_of_hire: profileGR.employment_start_date.getDisplayValue(),
                business_unit: profileGR.u_business_unit.getDisplayValue(),
                hr_business_partner: profileGR.u_business_unit.u_hr_business_partner.toString(),
                station_unit_name: profileGR.u_business_unit.toString(),
                position: profileGR.position.position.getDisplayValue(),
                employee_id: profileGR.employee_number.getDisplayValue(),
                street_address: profileGR.address.getDisplayValue(),
                city: profileGR.city.getDisplayValue(),
                state: profileGR.state.getDisplayValue(),
                zip_code: profileGR.zip.getDisplayValue(),
                department: profileGR.user.department.toString(),
                employment_type: profileGR.employment_type.getDisplayValue(),
                position_location: profileGR.user.location.toString(),
				manager: profileGR.user.manager.toString(),
				name: profileGR.user.name.toString(),
				location_name: profileGR.user.location.name.toString()
            };
//             gs.warn("PEK Phone Number " + userObj.phone_number);
//             gs.warn("PEK Email Address " + userObj.email_address);
//             gs.warn("PEK HRBP " + userObj.hr_business_partner);
//             gs.warn("PEK position " + userObj.position);
//             gs.warn("PEK department " + userObj.department);
//             gs.warn("PEK Unit " + userObj.station_unit_name);


            return JSON.stringify(userObj);
        }


    },

 

help?

bigbacon
Giga Guru

Okay, I think I see what is happening and I have no clue how to resolve it.

 

I have this include:

var HRTT4UTicket = Class.create();
HRTT4UTicket.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getJobTitleCodeHRIS: function() {
		var sysid = this.getParameter('sysparm_sysid');

		try {
			var ghjobGR = new GlideRecord('sn_hr_core_greenhouse_job_import');
			ghjobGR.addQuery('sys_id', sysid);
			ghjobGR.query();
			
			if(ghjobGR.next()) {
				//var objReturn = {
				//	jobcodeHRIS : ghjobGR.JobCodeHRIS.toString()
				//};
				//return JSON.stringify(objReturn);
				return ghjobGR.JobCodeHRIS;
			}
			else {
				return 'nothing';
			}	
		}
		catch(e) {
			return 'error' + e;
		}		
	},
    type: 'HRTT4UTicket'
});

 

and what seems to be happening is even though I am sending a valid sys_id to this function, it just returns an empty record set for the given table, which causes it to always send a null value back.

 

I can see this work when I just send back a stringified ghJobGR object back.

 

What am I missing here?