- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2025 11:35 AM
Hi Community,
I have tried many times to call script includes from client-side UI actions to fetch some data from server side. It never works and always give me error on the browser console:
In the Client-side UI actions, use g_modal to show fields on the pop up window, there's a choice field whose options are dynamically fetched using the GlideAjax to call a script include to fetch the data. The g_modal shows but can't fetch any data from the server side.
The UI action Workspace Client Script:
function onClick(g_form) {
g_modal.showFields({
title: "Recommendation",
fields: [{
type: 'choice',
name: 'recommendation',
label: 'Choose your recommendation here. If your recommendation is not listed, please add your own recommendation',
choice: getRecommendations(),
mandatory: true
},
{
type: 'textarea',
name: 'comments',
label: getMessage('Please add close comment here'),
mandatory: true
}
],
size: 'lg'
}).then(function(fieldValues) {
g_form.setValue('work_notes', "Recommendation: " + fieldValues.recommendation);
g_form.setValue('comments', fieldValues.comments);
g_form.setValue('sub_state', 6); // set sub state to recommendated by preliminary analysis
g_form.setValue('state', 3); // Set state to closed
g_form.save();
});
}
function getRecommendations() {
var pr_case = g_form.getValue('pr_case');
var ga = new GlideAjax('<the full API Name of the script include');
ga.addParam('sysparm_name', 'getRecommendations');
ga.addParam('sysparm_pr_case', pr_case);
ga.getXMLAnswer(function(response) {
var recommendations = JSON.parse(response);
if (recommendations.length === 0) {
alert("no recommendations");
return;
}
// Create dropdown options for recommendations
var options = recommendations.map(function(rec) {
return {
displayValue: rec.recommendation,
value: rec.sys_id
};
});
});
}
The script include function:
getRecommendations: function() {
var prCaseId = this.getParameter('sysparm_pr_case'); // Get PR Case value from Case Task
var recommendations = [];
var recGR = new GlideRecord('<recommendation table name>'); // Recommendation table
recGR.addQuery('case', prCaseId); // Match recommendation's 'case' field with case task's 'pr_case'
recGR.query();
while (recGR.next()) {
recommendations.push({
sys_id: recGR.getUniqueValue(),
recommendation: recGR.getValue('recommendation')
});
}
return JSON.stringify(recommendations); // Return as JSON
},
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2025 12:16 PM
Hi @ShuRiseUp2023,
Please switch the order, first do GlideAjax to get recommendations, and in the callback function, display the modal.
function onClick(g_form) {
var pr_case = g_form.getValue('pr_case');
var ga = new GlideAjax('<the full API Name of the script include');
ga.addParam('sysparm_name', 'getRecommendations');
ga.addParam('sysparm_pr_case', pr_case);
ga.getXML(displayModal);
}
function displayModal(response) {
var options;
var recommendations = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
if (recommendations.length === 0) {
alert("no recommendations");
}
// Create dropdown options for recommendations
options = recommendations.map(function(rec) {
return {
displayValue: rec.recommendation,
value: rec.sys_id
};
});
g_modal.showFields({
title: "Recommendation",
fields: [{
type: 'choice',
name: 'recommendation',
label: 'Choose your recommendation here. If your recommendation is not listed, please add your own recommendation',
choice: options,
mandatory: true
},
{
type: 'textarea',
name: 'comments',
label: getMessage('Please add close comment here'),
mandatory: true
}
],
size: 'lg'
}).then(function(fieldValues) {
g_form.setValue('work_notes', "Recommendation: " + fieldValues.recommendation);
g_form.setValue('comments', fieldValues.comments);
g_form.setValue('sub_state', 6); // set sub state to recommendated by preliminary analysis
g_form.setValue('state', 3); // Set state to closed
g_form.save();
});
}
Please test it and let me know if it works or if it throw an error message with screenshots to assist further on this,
If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2025 11:53 AM
The next step is to add some gs.info logging lines at the beginning of the getRecommendations function to confirm it is running and the value passed in, and to see where it may be going wrong, so something like this:
getRecommendations: function() {
var prCaseId = this.getParameter('sysparm_pr_case'); // Get PR Case value from Case Task
gs.info('SI running prCaseId = ' + prCaseId)
var recommendations = [];
var recGR = new GlideRecord('<recommendation table name>'); // Recommendation table
recGR.addQuery('case', prCaseId); // Match recommendation's 'case' field with case task's 'pr_case'
recGR.query();
while (recGR.next()) {
gs.info ('SI in while ' + recGR.name) //identifying field name on rec table
recommendations.push({
sys_id: recGR.getUniqueValue(),
recommendation: recGR.getValue('recommendation')
});
}
return JSON.stringify(recommendations); // Return as JSON
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2025 08:15 AM - edited 03-12-2025 08:15 AM
Do I have to put g_modal inside the getRecommendations function after calling the GlideAjax?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2025 12:16 PM
Hi @ShuRiseUp2023,
Please switch the order, first do GlideAjax to get recommendations, and in the callback function, display the modal.
function onClick(g_form) {
var pr_case = g_form.getValue('pr_case');
var ga = new GlideAjax('<the full API Name of the script include');
ga.addParam('sysparm_name', 'getRecommendations');
ga.addParam('sysparm_pr_case', pr_case);
ga.getXML(displayModal);
}
function displayModal(response) {
var options;
var recommendations = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
if (recommendations.length === 0) {
alert("no recommendations");
}
// Create dropdown options for recommendations
options = recommendations.map(function(rec) {
return {
displayValue: rec.recommendation,
value: rec.sys_id
};
});
g_modal.showFields({
title: "Recommendation",
fields: [{
type: 'choice',
name: 'recommendation',
label: 'Choose your recommendation here. If your recommendation is not listed, please add your own recommendation',
choice: options,
mandatory: true
},
{
type: 'textarea',
name: 'comments',
label: getMessage('Please add close comment here'),
mandatory: true
}
],
size: 'lg'
}).then(function(fieldValues) {
g_form.setValue('work_notes', "Recommendation: " + fieldValues.recommendation);
g_form.setValue('comments', fieldValues.comments);
g_form.setValue('sub_state', 6); // set sub state to recommendated by preliminary analysis
g_form.setValue('state', 3); // Set state to closed
g_form.save();
});
}
Please test it and let me know if it works or if it throw an error message with screenshots to assist further on this,
If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2025 04:33 PM
Thank you for your response. I figured out by myself. first move the g_modal after calling glideAjax. then change the script include to callable from All application scope. 3rd, in the g_modal choice field, fixed the typo 'choice' to 'choices' before options.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2025 04:39 PM
Glad you got it working! I believe my solution goes with the approach you followed. Could you please close this thread by accepting the solution as this would benefit me and future readers.
If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.