Not getting response from Server Script to Client Controller
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-03-2025 02:57 AM - edited ‎01-03-2025 03:04 AM
I was building a widget to escalate an incident, but in the client controller, the response from the server is null. The client controller is unable to retrieve the response from the server script.
Client Controller
api.controller = function ($uibModal, $scope, spUtil) {
var c = this;
// Function to open the modal
c.openEscalationModal = function () {
$('#escalationModal').modal('show');
};
// Function to submit the escalation request
c.submitEscalation = function () {
var reason = document.getElementById("escalation-reason").value.trim();
// Validate input
if (!reason) {
spUtil.addErrorMessage("Please provide a reason for escalation.");
return;
}
// Set action and reason
c.data.action = 'esc';
c.data.reason = reason;
// Debug log
console.log("DEBUG: Sending data to server:", c.data);
// Send the data to the server
c.server.update().then(function (response) {
console.log("DEBUG: Server response:", response);
// Check if response is valid
if (response && response.data) {
// Handle different statuses
if (response.data.status === 'queued') {
spUtil.addInfoMessage(response.data.message || "Incident escalated successfully!");
} else if (response.data.status === 'error') {
spUtil.addErrorMessage(response.data.message || "An error occurred while escalating the incident.");
} else {
spUtil.addInfoMessage(response.data.message || "No action performed.");
}
} else {
spUtil.addErrorMessage("Unexpected server response format.");
}
// Reset the modal and textarea
$('#escalationModal').modal('hide');
document.getElementById("escalation-reason").value = "";
}).catch(function (error) {
// Handle unexpected errors
console.error("DEBUG: Error from server:", error);
spUtil.addErrorMessage("Unexpected error: " + error);
});
};
};
Server Script
(function () {
// Initialize the `data` object
data = {};
try {
// Log input for debugging
console.log("DEBUG: Input received: " + JSON.stringify(input));
// Fetch the sys_id
data.sys_id = input && input.sys_id ? input.sys_id : $sp.getParameter("sys_id");
if (!data.sys_id) {
console.log("sys_id is missing or undefined.");
data.status = 'error';
data.message = "sys_id parameter is missing.";
return data;
}
// Get the incident record
var gr = new GlideRecord('incident');
if (!gr.get(data.sys_id)) {
console.log("Incident not found for sys_id: " + data.sys_id);
data.status = 'error';
data.message = "Incident not found for the given sys_id.";
return data;
}
// Handle escalation action
if (input && input.action === 'esc') {
var reason = input.reason || "No reason provided.";
console.log("DEBUG: Escalation action triggered. Reason: " + reason);
// Update comments on the incident record
gr.comments = "Ticket Escalated. Reason: " + reason;
gr.update();
// Trigger escalation event if assigned_to has an email
if (gr.assigned_to && gr.assigned_to.email) {
gs.eventQueue(
"incident.escalation",
gr,
gr.assigned_to.email,
reason
);
// Populate the `data` object with success status
data.status = 'queued';
data.message = "Incident escalated successfully. Reason: " + reason;
} else {
// Handle case where no valid email is found
console.log("Assigned to user does not have a valid email address.");
data.status = 'error';
data.message = "Assigned to user does not have a valid email address.";
}
} else {
// Handle cases where no action is taken
console.log("DEBUG: No action performed.");
data.status = 'no action';
data.message = "No action was performed.";
}
} catch (e) {
// Catch unexpected errors
console.log("Unexpected server error: " + e.message);
data.status = 'error';
data.message = "Unexpected server error: " + e.message;
}
// Log the final data being returned for debugging
console.log("DEBUG: Final data being returned: " + JSON.stringify(data));
// Return the `data` object to the client
return data;
})();
The email is being sent, additional comments are being added, and the data on the server side contains the values. However, when the data is called by the client, it appears empty. How can this be fixed?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-03-2025 04:05 AM
what debugging have you done so far?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-05-2025 11:18 PM
The values from the client script is getting sent and the server script is adding the additional comment and sending the mail and when i am printing the data in the end of server script it returns
but when i try to send it to client controller it fetches
Server contains everything but it sent undefined to client controller
I think there is some issue in calling the server from client
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-03-2025 04:15 AM
Hello @ankitgarai
In the server-side script, the initialization of data as an empty object (data = {};) is unnecessary because data is initially null by default.
Here’s the updated server-side script:
(function () {
try {
// Log input for debugging
console.log("DEBUG: Input received: " + JSON.stringify(input));
// Fetch the sys_id
data.sys_id = input && input.sys_id ? input.sys_id : $sp.getParameter("sys_id");
if (!data.sys_id) {
console.log("sys_id is missing or undefined.");
data.status = 'error';
data.message = "sys_id parameter is missing.";
return data;
}
// Get the incident record
var gr = new GlideRecord('incident');
if (!gr.get(data.sys_id)) {
console.log("Incident not found for sys_id: " + data.sys_id);
data.status = 'error';
data.message = "Incident not found for the given sys_id.";
return data;
}
// Handle escalation action
if (input && input.action === 'esc') {
var reason = input.reason || "No reason provided.";
console.log("DEBUG: Escalation action triggered. Reason: " + reason);
// Update comments on the incident record
gr.comments = "Ticket Escalated. Reason: " + reason;
gr.update();
// Trigger escalation event if assigned_to has an email
if (gr.assigned_to && gr.assigned_to.email) {
gs.eventQueue(
"incident.escalation",
gr,
gr.assigned_to.email,
reason
);
// Populate the `data` object with success status
data.status = 'queued';
data.message = "Incident escalated successfully. Reason: " + reason;
} else {
// Handle case where no valid email is found
console.log("Assigned to user does not have a valid email address.");
data.status = 'error';
data.message = "Assigned to user does not have a valid email address.";
}
} else {
// Handle cases where no action is taken
console.log("DEBUG: No action performed.");
data.status = 'no action';
data.message = "No action was performed.";
}
} catch (e) {
// Catch unexpected errors
console.log("Unexpected server error: " + e.message);
data.status = 'error';
data.message = "Unexpected server error: " + e.message;
}
// Log the final data being returned for debugging
console.log("DEBUG: Final data being returned: " + JSON.stringify(data));
// Return the `data` object to the client
return data;
})();
Result:
Hope this helps!
"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
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-05-2025 11:08 PM
Yes, that works, but in the client controller script, line 31 is returning null. I can retrieve the server data directly in the HTML, but how can I get it in the client controller? I want to receive confirmation from the server script in the client controller, not in the HTML. Thank you.