Return value from a call back function

Snow Tomcal
Tera Expert

Hi All,

I have a client script that use GlideAjax.

I want to return a value from the call back function, does anyone know how to implement this?

 

my script:

function funcationName (number) {

var ga = new glideAjax('ScriptName');

ga.addParam('sysparm_name', 'function_name');

ga.addParam('sysparm_number', number);

return gs.getXMLAnswer(callBackFunction);

}

 

function callBackFunction(response){

response = JSON.parse(response);

 

return response[0] == 'name' ? true : false;

}

 

Thanks In Advance 🙂

5 REPLIES 5

Sanjeev Kumar1
Kilo Sage

Hi ,

 

It's good first you create an exmp. as per doc.

 

https://docs.servicenow.com/en-US/bundle/utah-api-reference/page/app-store/dev_portal/API_reference/...

 

Use function like

var ga = new GlideAjax('HelloWorld'); // HelloWorld is the script include class 
ga.addParam('sysparm_name','helloWorld'); // helloWorld is the script include method 
ga.addParam('sysparm_user_name',"Bob"); // Set parameter sysparm_user_name to 'Bob' 
ga.getXML(HelloWorldParse);  /* Call HelloWorld.helloWorld() with the parameter sysparm_user_name set to 'Bob' 
      and use the callback function HelloWorldParse() to return the result when ready */

// the callback function for returning the result from the server-side code
function HelloWorldParse(response) {  
   var answer = response.responseXML.documentElement.getAttribute("answer"); 
    alert(answer);
}

 

The returned value is okay but when I try to use it in the original function it's value is undefined.

Ankur Bawiskar
Tera Patron

@Snow Tomcal 

whatever further processing you want to do after the ajax do it in callback method only.

What's the point in adding that logic outside the callback?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

I'm using more than one GlideAjax so I want to combine the logic in the main function