- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2016 08:04 AM
Hi,
I have a requirement to use the logged in user's company to validate some other fields. Now as far as I know,fetching the company name directly using User object is not possible. However we can get the company sys_id by using gs.getUser().getCompanyID. Now if i want to fetch the company name by query the company table with the company sys_id,what should be the code I have to use? I want to search the company table for the company name(comparing the auto poulated company sys_id i get into the sys_id field for the logged in user) for the logged in user and use the company name to validate some other fields. Like- if the user belongs to a certain company he should see some certain fields etc.
P.S- Very bad in coding,so a little explaining with any helpful answer will be really appreciated
Thanks,
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-24-2016 12:10 AM
Please find the following GlideAjax script -
Client script - onLoad
function onLoad() {
var ga = new GlideAjax('companyUtil');
ga.addParam('sysparm_name', 'getCompanyName');
ga.getXML(analyzeResponse); // Always try to use asynchronous (getXML) calls rather than synchronous (getXMLWait)
}
function analyzeResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
//you can do anything here with the form, the answer variable holds the name of the company
}
Script include -
- When creating the script include, you must select the Client callable check box.
FYI, GlideAjax - ServiceNow Wiki
var companyUtil = Class.create();
companyUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCompanyName: function() {
var compName = ''; // Return value
var grComp = new GlideRecord('core_company');
if (grComp.get(gs.getUser().getCompanyID())) {
compName = grComp.name;
}
return compName;
}
});
Hopefully this should help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2016 08:07 AM
You have to write a GlideAjax call in onLoad script - this should help you and it has sample scripts also -
Client Script Best Practices - ServiceNow Wiki
Hopefully it helps

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2016 08:55 AM
Hi Anurag,
Assuming you wanna do the validation at client side then you have to make a GlideAjax call.
The GlideAjax class allows the execution of server-side code from the client.
Please go through the below example for reference and adjust your code. Let me know if you are blocked.
http://wiki.servicenow.com/index.php?title=GlideAjax#gsc.tab=0
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2016 11:20 PM
Hi Pradeep,
If you can share what the code should be it will be really helpful
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-24-2016 12:10 AM
Please find the following GlideAjax script -
Client script - onLoad
function onLoad() {
var ga = new GlideAjax('companyUtil');
ga.addParam('sysparm_name', 'getCompanyName');
ga.getXML(analyzeResponse); // Always try to use asynchronous (getXML) calls rather than synchronous (getXMLWait)
}
function analyzeResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
//you can do anything here with the form, the answer variable holds the name of the company
}
Script include -
- When creating the script include, you must select the Client callable check box.
FYI, GlideAjax - ServiceNow Wiki
var companyUtil = Class.create();
companyUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCompanyName: function() {
var compName = ''; // Return value
var grComp = new GlideRecord('core_company');
if (grComp.get(gs.getUser().getCompanyID())) {
compName = grComp.name;
}
return compName;
}
});
Hopefully this should help