How to trigger onChange event on onLoad script

Lucas Pereira1
Kilo Expert

Hello Guys!

I have a Catalog Script to set catalog variable values received via URI parameters.

This is working fine. 

The problem is, when I set a User Reference Variable, the Phone Number is not updated.  That is because onChange script on User variable does not fire. 

Is there a way to trigger the onChange script?

 

Set Variables Script (resides on Catalog Variable Set to be reused on any catalog item😞

function onLoad() {	
	
	
	//get parameter from URL
	var arrVariables = getParameterValue('sysparm_variables');
	
	jslog('sysparm_variables URI parameter value: ' + arrVariables);
	
	if (!arrVariables || !Array.isArray(arrVariables)) {
		//If variables is empty or not properly formatted, stop here.
		jslog('aborting due to a missing or problematic sysparm_variables URI parameter');
		return;
	}
	
	arrVariables.forEach(setFormValue);

}

function getParameterValue(parameterName){
	
	var gUrl = new GlideURL();
	gUrl.setFromCurrent();
	var parameterValue = gUrl.getParam(parameterName);
	jslog('ParameterValue extracted: '+parameterValue);
	var paramsArr = parameterValue.split('^');
	
	return paramsArr;
	
}

function setFormValue(keyValue){
try {
	var key = keyValue.split('=')[0];
	var value = decodeURIComponent(keyValue.split('=')[1]);
	
	if (g_form.hasField(key)) {
		jslog('Setting variable '+ key +' to value '+ value);
		g_form.setValue(key, value);
	} 
	
}
	catch(e){
		jslog('Unable to set form variable: '+keyValue + '['+e+']');
	}
}

 

OnChange Variable (reside on User/Phone variable set) 

This should be fired after 'Set Variables' script.

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

    var user = newValue;
	var getTelephone = new GlideAjax("Acc_GetUserTelephoneAJAX");
	getTelephone.addParam("sysparm_name","getUserTelephone");
	getTelephone.addParam("sysparm_userid",user);
	getTelephone.getXML(handleXML);
	
	function handleXML(response) {
		var telephone = response.responseXML.documentElement.getAttribute("answer");
		g_form.setValue("u_phone_number",telephone);
	}

}

 

Thanks!!

3 REPLIES 3

Tudor
Tera Guru

Hi Lucas,

Either try changing the order in which the script are triggering(onChange to have a higher order than onLoad, meaning it will trigger after the onLoad) or change the onLoad to an onChange client script and in the 

if (newValue==''){

return;

}

else if (isLoading) { 

var arrVariables = getParameterValue('sysparm_variables'); jslog('sysparm_variables URI parameter value: ' + arrVariables); if (!arrVariables || !Array.isArray(arrVariables)) { //If variables is empty or not properly formatted, stop here. jslog('aborting due to a missing or problematic sysparm_variables URI parameter'); return; } arrVariables.forEach(setFormValue); } function getParameterValue(parameterName){ var gUrl = new GlideURL(); gUrl.setFromCurrent(); var parameterValue = gUrl.getParam(parameterName); jslog('ParameterValue extracted: '+parameterValue); var paramsArr = parameterValue.split('^'); return paramsArr; } function setFormValue(keyValue){ try { var key = keyValue.split('=')[0]; var value = decodeURIComponent(keyValue.split('=')[1]); if (g_form.hasField(key)) { jslog('Setting variable '+ key +' to value '+ value); g_form.setValue(key, value); } } catch(e){ jslog('Unable to set form variable: '+keyValue + '['+e+']'); } }

else{

var user = newValue; var getTelephone = new GlideAjax("Acc_GetUserTelephoneAJAX"); getTelephone.addParam("sysparm_name","getUserTelephone"); getTelephone.addParam("sysparm_userid",user); getTelephone.getXML(handleXML); function handleXML(response) { var telephone = response.responseXML.documentElement.getAttribute("answer"); g_form.setValue("u_phone_number",telephone); }

}

Hope the above helps!

Regards,

 Tudor

Lucas Pereira1
Kilo Expert

Hi Tudor! Thanks for your reply!

Changing the order does not have any effect in this case. 

Merge the two scripts is not possible because they are used in different scenarios, a few times together.

I will try to change the script to OnChange, keeping separated and play with the order to see if I can get the result we need.

Regards!

 

Lucas

Alok Das
Tera Guru

Hi,

Removing the isLoading from the very next line of onchange client script will trigger the client script on load of form.

I believe you want the Set Variables Script to be triggered only while loading of the form after which OnChange Variable will be triggered. If my understanding is correct then you can have a single onChange client script and use it as onLoad as well as onChange. If my understanding is correct then you can find the sample script below which can be used :

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (newValue === '') {
		return;
	}
	else if(isLoading){
		//WRITE ONLOAD CLIENT SCRIPT
		
	}
	
	
	var user = newValue;
	var getTelephone = new GlideAjax("Acc_GetUserTelephoneAJAX");
	getTelephone.addParam("sysparm_name","getUserTelephone");
	getTelephone.addParam("sysparm_userid",user);
	getTelephone.getXML(handleXML);
	
	
	
}
function handleXML(response) {
	var telephone = response.responseXML.documentElement.getAttribute("answer");
	g_form.setValue("u_phone_number",telephone);
}

 

Kindly mark my answer as Correct and Helpful based on the Impact.

Regards,

Alok