- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2023 07:30 AM
I need to get the answer from my GlideAjax script and use this answer in other if statements in my client script. How do I do this?
Simple GlideAjax:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2023 08:44 AM - edited 10-27-2023 08:45 AM
Hi @kemmy1 ,
Why don't u define your logic in your existing function where u receiving response from GlideAjax? Something like below
var ga = new GlideAjax('asu_GetLocationData');
ga.addParam('sysparm_name', 'getCampus');
ga.addParam('sysparm_buildingid', g_form.getValue("u_building"));
ga.getXMLAnswer(updateCampus);
}
function updateCampus(answer) {
var clearvalue; // Stays Undefined
if (answer) {
var returneddata = JSON.parse(answer);
g_form.setValue("campus", returneddata.sys_id, returneddata.name);
} else {
g_form.setValue("campus", clearvalue);
}
}
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2023 08:34 AM
Hi @kemmy1
Hmm...then can you try like calling function within function:
var getStudent = new GlideAjax('<glideAjax');
getStudent.addParam('sysparm_name', 'isLawStudent');
getStudent.getXMLAnswer(getStudentResult);
function getStudentResult(response) {
g_form.addInfoMessage("response: " + response);
var student2= response;
myNewFunction(student2);
}
function myNewFunction(student2){
g_form.addInfoMessage("student2: " + student2);
}
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2023 08:42 AM
Tried your solution and student2 is not defined (when I try to addInfoMessage out of the function)
var getStudent = new GlideAjax('x_g_dh5_digitalg28.G28Ajax');
getStudent.addParam('sysparm_name', 'isLawStudent'); //if user is a law student
getStudent.getXMLAnswer(getStudentResult);
// var student2 = '';
function getStudentResult(response) {
var student2 = response;
myNewFunction(student2);
}
function myNewFunction(student2) {
g_form.addInfoMessage("student2: " + student2);
}
g_form.addInfoMessage("otherstudent2: " + student2);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2023 08:16 AM
Try the following.
var getStudent = new GlideAjax('<glideAjax');
getStudent.addParam('sysparm_name', 'isLawStudent');
getStudent.getXMLAnswer(getStudentResult);
var student2='';
function getStudentResult(response) {
g_form.addInfoMessage("response: " + response);
student2 = response;
}
g_form.addInfoMessage("student2: " + student2); //not getting data
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2023 08:32 AM
So weird, I'm still not getting data.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2023 08:34 AM - edited 10-27-2023 08:37 AM
Hi @kemmy1 ,
Can u try below code:
var getStudent = new GlideAjax('<glideAjax');
getStudent.addParam('sysparm_name', 'isLawStudent');
getStudent.getXMLAnswer(getStudentResult);
function getStudentResult(answer) {
g_form.addInfoMessage("response: " + answer);
if(answer){
var student2 = answer;
}else{
}
}
Thanks,
Danish