Ui Action not working in Workspace
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Below code for Workspace Client Script not working:
Requirement: If parent case contain open child tasks, while closing the case it will show a pop-up and close parent along with child
function onClick() {
if (!g_form.getValue("assigned_to")) {
g_form.addErrorMessage('Assigned To cannot be empty. Please fill the Assign To field before moving the request to Awaiting Info or Resolved.');
return false;
}
var close_notes = g_form.getValue("close_notes");
var resolution_code = g_form.getValue("resolution_code");
//var cause1 = g_form.getValue("cause");
if (!close_notes || !resolution_code) {
g_form.addErrorMessage(
getMessage(
'To propose a solution, you must select a resolution code and add resolution notes.'
)
);
return false;
}
// First check if there are open tasks
gaCheck = new GlideAjax("sn_customerservice.CheckTaskRecord");
gaCheck.addParam('sysparm_name', 'checkIfTask');
gaCheck.addParam('sysparm_request_id', g_form.getUniqueValue());
g_form.addInfoMessage(g_form.getUniqueValue());
gaCheck.getXMLAnswer(function(response) {
g_form.addInfoMessage("Test2");
var tasksAvailable = response;
if (tasksAvailable == true) {
g_form.addInfoMessage("Test1");
if (!confirm("Resolution of request will close all open tasks. Do you want to proceed?")) {
return;
} else {
gaCheck = new GlideAjax("sn_customerservice.CheckTaskRecord");
gaCheck.addParam('sysparm_name', 'updateTaskRecord');
gaCheck.addParam('sysparm_request_id', g_form.getUniqueValue());
gaCheck.getXMLAnswer(function(response) {
var res = response;
});
}
}
g_form.setValue("state", "6");
g_form.update();
});
}var CheckTaskRecord = Class.create();
CheckTaskRecord.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
checkIfTask: function() {
var parent = this.getParameter('sysparm_request_id');
var taskGR = new GlideRecord("sn_customerservice_task");
taskGR.addQuery("parent_case", parent);
taskGR.addQuery("state", "!=", "3");
taskGR.query();
if (taskGR.hasNext())
return true;
else
return false;
},
updateTaskRecord: function() {
var parent = this.getParameter('sysparm_request_id');
var taskGR = new GlideRecord("sn_customerservice_task");
taskGR.addQuery("parent_case", parent);
taskGR.addQuery("state", "!=", "3");
taskGR.query();
while (taskGR.hasNext()) {
taskGR.setValue("state", "3");
taskGR.setValue("work_notes", "Parent request has been resolved.");
taskGR.update();
}
return true;
},
type: 'CheckTaskRecord'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hello @Rosy14 ,
Try this
Workspace Client Script
Replace your client script with this. It replaces confirm() with the workspace-native g_modal.confirm() and handles the sequential saves properly.
function onClick(g_form) {
// 1. Mandatory field checks
if (!g_form.getValue("assigned_to")) {
g_form.addErrorMessage('Assigned To cannot be empty. Please fill the Assign To field before moving the request to Awaiting Info or Resolved.');
return false;
}
var close_notes = g_form.getValue("close_notes");
var resolution_code = g_form.getValue("resolution_code");
if (!close_notes || !resolution_code) {
g_form.addErrorMessage(
getMessage('To propose a solution, you must select a resolution code and add resolution notes.')
);
return false;
}
// 2. Check for open tasks asynchronously
var gaCheck = new GlideAjax("sn_customerservice.CheckTaskRecord");
gaCheck.addParam('sysparm_name', 'checkIfTask');
gaCheck.addParam('sysparm_request_id', g_form.getUniqueValue());
gaCheck.getXMLAnswer(function(response) {
// GlideAjax returns strings, ensure we compare properly
if (response === 'true') {
// 3. Workspace Native Modal Popup
g_modal.confirm({
title: getMessage('Resolve Case'),
message: getMessage('Resolution of request will close all open tasks. Do you want to proceed?')
}).then(function(confirmed) {
if (confirmed) {
// Update child tasks first, then resolve the parent case
var gaUpdate = new GlideAjax("sn_customerservice.CheckTaskRecord");
gaUpdate.addParam('sysparm_name', 'updateTaskRecord');
gaUpdate.addParam('sysparm_request_id', g_form.getUniqueValue());
gaUpdate.getXMLAnswer(function(updateResponse) {
g_form.setValue("state", "6");
g_form.submit('sysverb_update'); // Workspace prefers submit over update()
});
}
});
} else {
// No tasks found, resolve parent case immediately
g_form.setValue("state", "6");
g_form.submit('sysverb_update');
}
});
return false; // Crucial: Prevents the UI action from submitting before async logic executes
}
Script Include
Ensure this Script Include is marked as Client Callable. Note the critical fix from while(taskGR.hasNext()) to while(taskGR.next()) to avoid instance freeze/infinite loops.
var CheckTaskRecord = Class.create();
CheckTaskRecord.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
checkIfTask: function() {
var parent = this.getParameter('sysparm_request_id');
var taskGR = new GlideRecord("sn_customerservice_task");
taskGR.addQuery("parent_case", parent);
taskGR.addQuery("state", "!=", "3"); // Not Closed
taskGR.query();
if (taskGR.hasNext()) {
return "true";
}
return "false";
},
updateTaskRecord: function() {
var parent = this.getParameter('sysparm_request_id');
var taskGR = new GlideRecord("sn_customerservice_task");
taskGR.addQuery("parent_case", parent);
taskGR.addQuery("state", "!=", "3");
taskGR.query();
// FIXED: Changed from hasNext() to next() to prevent infinite loop
while (taskGR.next()) {
taskGR.setValue("state", "3");
taskGR.setValue("work_notes", "Parent request has been resolved.");
taskGR.update();
}
return "true";
},
type: 'CheckTaskRecord'
});
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"
Thank You,
Sujit Jadhav
