How to use the output of a function, outside the function

Vedang1
Tera Expert

Hi Guys,

 

I've have a change that I need to easily implement in multiple client scripts that have been currently hardcoded to a group. To avoid this, I have created a property which stores this value, and I'm calling the property via the client script.

Obviously, since that is server side, I'm using GlideAjax to call a script include within which I call the property (not creating a business rule and using scratchpad since the BR would need to be created on many different tables).

 

In order to easily make the changes dynamically in multiple scripts, I have performed the GlideAjax in a new function - getSD() (the idea is to just copy-paste the function in all these scripts). For the same reason, I do not want to set value inside the callback function, which I know will work.

 

Instead, I only want to output the value of this function, so that it can be used anywhere in the client script.

 

 

function onChange(control, oldValue, newValue, isLoading) {
	if (newValue == '') {
		g_form.clearValue('assignment_group_support');
		g_form.setReadOnly('assignment_group_support',false);
		return;
	}
	
	//Type appropriate comment here, and begin script below
	if (newValue == 'global_service_desk') {
		var sd = getSD();        
		alert('sd = ' + sd);

        // g_form.setValue('assignment_group_support','65d252364f4a4300bab60b318110c72c');
		g_form.setValue('assignment_group_support',sd); //Service Desk
		g_form.setReadOnly('assignment_group_support',true);
	}
	
	else {
		g_form.clearValue('assignment_group_support');
		g_form.setReadOnly('assignment_group_support',false);
	}
   
}

function getSD(getServiceDesk) {
	var ga = new GlideAjax('AAServiceDeskUtils'); 
    ga.addParam('sysparm_name', getServiceDesk); 
    ga.getXML(myCallBack); //This is our callback function, which will process the response.

    function myCallBack(response) {
        //Dig out the 'answer' attribute, which is what our function returns. 
        answer = response.responseXML.documentElement.getAttribute('answer');
        alert('answer = ' + answer); //alert the result to make sure all is well.
		return answer;
    }
}

 

 

Here is a script that I thought should work but did not.

I've tried various methods and looked up a lot of articles but have not been able to find this scenario, which is making me believe that it might not be possible at all. The part with calling the script include and getting the value works fine

but I am not able to send the value out of the function and use it elsewhere. I've also tried setting the direct value of 'answer' instead of using return but that does not work either.

 

Could you please help me with this and let me know if it is not possible.

 

Thanks in advance!

Please mark it as solution proposed and helpful if its serves your purpose.

Thank you,
Vedang
4 REPLIES 4

SanjivMeher
Kilo Patron
Kilo Patron

So if you want to run something from multiple places, what you are doing is already correct.

Creating a script include and doing GlideAjax to that script from multiple tables. But you can't send the value out of the onChange client script. I wonder why do you want to do that. Can you provide the use case and what value you are trying to set. I can tell you, you can't use a variable outside of client script. Every time you need that value, you need to do a GlideAjax.


Please mark this response as correct or helpful if it assisted you with your question.

Vedang1
Tera Expert

Hi @SanjivMeher 

 

Thanks for your reply!

 

I don't want to take the value out of the onChange client script, just out of the function myCallback() and use it in the main onChange() function in the following line:

g_form.setValue('assignment_group_support',sd); //Service Desk

 

What I don't want to do is to put the above line inside the myCallback() function because then its no longer a copy paste job for me to modify multiple scripts.

 

Hope that helps.

Please mark it as solution proposed and helpful if its serves your purpose.

Thank you,
Vedang

So since it is an asynchronous call, it will not wait and rest of your scripts will be executed. I would move everything that needs to be updated, inside the callback.

 

function onChange(control, oldValue, newValue, isLoading) {
	if (newValue == '') {
		g_form.clearValue('assignment_group_support');
		g_form.setReadOnly('assignment_group_support',false);
		return;
	}
	
	//Type appropriate comment here, and begin script below
	if (newValue == 'global_service_desk') {
		var sd = getSD();        
		g_form.setReadOnly('assignment_group_support',true);
	}
	else {
		g_form.clearValue('assignment_group_support');
		g_form.setReadOnly('assignment_group_support',false);
	}
   
}

function getSD(getServiceDesk) {
	var ga = new GlideAjax('AAServiceDeskUtils'); 
    ga.addParam('sysparm_name', getServiceDesk); 
    ga.getXML(myCallBack); //This is our callback function, which will process the response.

    function myCallBack(response) {
        //Dig out the 'answer' attribute, which is what our function returns. 
        answer = response.responseXML.documentElement.getAttribute('answer');
        alert('answer = ' + answer); //alert the result to make sure all is well.
	// g_form.setValue('assignment_group_support','65d252364f4a4300bab60b318110c72c');
	g_form.setValue('assignment_group_support',answer); //Service Desk
    }
}

  


Please mark this response as correct or helpful if it assisted you with your question.

Hi @SanjivMeher ,

 

That was actually somewhat helpful. Of course my main goal is to not use the action within the callback, so I can't use this solution, but at least I now understand that the issue is because of the asynchronous call.

 

I searched a few articles of how I can make synchronous calls, and I got a few like using getXMLWait(), but unfortunately this is not supported on the Service Portal, especially for onChange scripts as opposed to onSubmit.

 

But then is there a way to add a brief timer after

var sd = getSD(); 

so that the script waits to get an answer before it proceeds to the next step. Would that work?

Please mark it as solution proposed and helpful if its serves your purpose.

Thank you,
Vedang