- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2019 06:44 AM
Hi all,
I need to identify the requester company to hide/ show specific fields. I´ve been trying with a client script and script include, but when I load the form a javascript appears. ' TypeError: Cannot read property 'initialize' of undefined'
Client Script:
function onLoad() {
//Type appropriate comment here, and begin script below
var ga = GlideAjax('GetUserCompany');
ga.addParam('sysparm_name','getComp');
ga.getXML(fulfillParse);
function fulfillParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('company', answer);
}}
Script includes:
var GetUserCompany = Class.create();
GetUserCompany.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getComp: function() {
return gs.getUser().getCompanyID(); //returns sys id of an company to client script as an answer
}
});
Any advice will be helpful.
Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2019 11:38 AM
Oh, meant to say the problem you are having is you are missing the "new" keyword:
var ga = new GlideAjax('GetUserCompany');

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2019 09:23 AM
How about something like:
function onChange(control, oldValue, newValue, isLoading) {
g_form.getReference('requested_for', reqCompCallback);
}
function reqCompCallback(caller) {
//Get company
var comp = caller.company;
g_form.setValue('company',comp);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2019 11:36 AM
Couple things I would do:
1. Add the following to the "Default value" field on the company variable:
javascript:gs.getUser().getCompanyID();
That will, as the field name implies, set the default value of the company variable. No point loading the form then going back to the server for the data. I'm assuming you have the same set for the User.
2. Change the Client Script to be "onChange" instead:
function onChange(control, oldValue, newValue, isLoading) {
//skip onLoad
if (isLoading) {
return;
}
//clear the Company variable if User is cleared
if (newValue == '') {
g_form.setValue("company", "");
return;
}
//call an Ajax method to get the Company ID and display name
var ga = new GlideAjax("UCustomAjaxUtilities");
ga.addParam("sysparm_name", "getUserCompany");
ga.getXMLAnswer(uShowResults);
function uShowResults(answer) {
var result = JSON.parse(answer); //Transform the JSON string to an object
g_form.setValue("variables.company", result.companyId, result.companyName); //use the reference field's display value in the 3rd parameter for better performance
}
}
3. Create 1 custom Script Include to contain your custom code:
Name: UCustomAjaxUtilities
Client callable: checked
Script:
var UCustomAjaxUtilities = Class.create();
UCustomAjaxUtilities.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserCompany: function() {
var result = {};
result.companyId = "";
result.companyName = "";
var gr = new GlideRecord("sys_user");
if (gr.get(gs.getUserID())) {
result.companyId = gr.getValue("company");
result.companyName = gr.company.getDisplayValue();
}
return JSON.stringify(result);
},
//add other custom functions in here
type: "UCustomAjaxUtilities"
});
You can just add any new Ajax methods to the same Script Include.
4. any UI Policies to show/hide variables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2019 11:38 AM
Oh, meant to say the problem you are having is you are missing the "new" keyword:
var ga = new GlideAjax('GetUserCompany');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2019 11:58 AM
Thanks!!!
I've been reviewing the script for two days and I was going crazy.