I am very new to ServiceNow I have two tables company and location.
location table has the column name. company table has columns name and location. Here the location type is reference.
The company and location table data are below.
Location
Name
Chennai HYD
Company
Name Location
ABC Chennai
CDE HYD
My task is to query name and the location from Company table. The query as below.
var company = new GlideRecord('u_company'); subCat.query(); while (company.next()) { alert("Location: " + company.location); }
Here always the value is hexa decimal value. How can I get the actual text value for location?
11 Answer
What you are seeing should be the sys_id of the location reference field.
If you're doing this in a Business Rule, you can pull this fairly easily using the getRefRecord() method.
var company = new GlideRecord('u_company'); company.query(); while (company.next()) { var loc = company.location.getRefRecord() gs.log("Company Name: " + company.getValue('name')); gs.log("Location Name: " + loc.getValue('name')); } If you're doing this as a Client Script, it's a bit more involved but you can use GlideAjax. Take a look at the examples there, but here is a quick example.
Create a new Script Include that is Client Callable.
var MyCompanyUtils = Class.create(); MyCompanyUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, { getLocationName : function() { // parameters var company = this.getParameter('sysparm_my_company'); // query var rec = new GlideRecord('u_company'); rec.addQuery('sys_id', company); rec.query(); data = "Company not found." while (rec.next()) { var loc = rec.location.getRefRecord(); data = loc.name; } return data; }, type : "MyCompanyUtils" }); Create a Client Script that calls this Script Include.
function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === '') { return; } // get the company referece value var company = g_form.getValue('company'); var ga = new GlideAjax('MyCompanyUtils'); // call the object ga.addParam('sysparm_name', 'getLocationName'); // call the function ga.addParam('sysparm_my_company', company); // pass in company ga.getXML(showLocation); } function showLocation(response) { var answer = response.responseXML.documentElement.getAttribute("answer"); alert("Location: " + answer); }