get the logged user company to show/ hide fields in catalog item

dioni95
Kilo Expert

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

 

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

Oh, meant to say the problem you are having is you are missing the "new" keyword:

var ga = new GlideAjax('GetUserCompany');

View solution in original post

5 REPLIES 5

Michael Fry1
Kilo Patron

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);

}

Jim Coyne
Kilo Patron

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

Jim Coyne
Kilo Patron

Oh, meant to say the problem you are having is you are missing the "new" keyword:

var ga = new GlideAjax('GetUserCompany');

Thanks!!!

 

I've been reviewing the script for two days and I was going crazy.