The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Callback function used for g_form.getReference is not working in portal

vanessaheux
Tera Contributor

Hello,

I have a country variable in a request item which is automatically filled thanks to the country of the location (location which is another variable in the same request item).
When location is filled, country is automatically filled.
This works thanks to a catalog client script. It only works on the platform but not in the portal.
I know that g_form.getReference can’t be used in catalog client script and so we need to create a callback function.
I created it but I still have the problem : ok on the platform but not in the portal.
Here is the catalog script I wrote:


function onChange(control, oldValue, newValue, isLoading) {
  var caller = g_form.getReference('user_location', doAlert); // doAlert is our callback function
}
function doAlert(caller) { //reference is passed into callback as first arguments
 g_form.setValue('user_country',caller.country);
}

Do you have any idea why it is not working in the portal ?
Thanks
Vanessa Heux

1 ACCEPTED SOLUTION

Harsh Vardhan
Mega Patron
Mega Patron

if above solution not working , replace the code with script include + glide ajax .

 

try now with below sample code.

 

Sample Code:

 

Script Include:

 

var getCountry = Class.create();
getCountry.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getCont : function(){

		var cnt = this.getParameter('sysparm_user_name');

		var gr = new GlideRecord('cmn_location');
		gr.get(cnt);
		var cmn = new GlideRecord('core_country');
		cmn.addQuery('name',gr.country.getDisplayValue());
		cmn.query();
		if(cmn.next()){
			return cmn.sys_id;
		}},

	type: 'getCountry'
});

 

find_real_file.png

 

Catalog client script:

 

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

	//Type appropriate comment here, and begin script below
	var caller = g_form.getValue('user_location'); // make sure about the variable name
	var ga = new GlideAjax('getCountry');
	ga.addParam('sysparm_name', 'getCont');
	ga.addParam('sysparm_user_name',caller);
	ga.getXML(HelloWorldParse);

	function HelloWorldParse(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");

		g_form.setValue('user_country',answer); // make sure about the variable name
	}

}

 

 

Note: Validate the variable name in above client script.

View solution in original post

25 REPLIES 25

Elijah Aromola
Mega Sage
Mega Sage

Is your UI type set to all?

vanessaheux
Tera Contributor

Hello 

 

Yes I already checked that.

Any other idea ?

can you add some alert in your client script ?

i am assuming here user_location is reference type field  ?

 

try now and let me know what are you getting in alert, make sure field name is correct and onChange has been set on correct field . 

 

 

function onChange(control, oldValue, newValue, isLoading) {

  var caller = g_form.getReference('user_location', doAlert); 
}
function doAlert(caller) { 
alert('Value is '+ caller.country);
 g_form.setValue('user_country',caller.country);
}

Hello

 

I tried your alert and the value is correct in the alert, however, is does not display in the variable.

And I know the search is correct as it works on the platform, the problem is only on the portal.

 

find_real_file.png