- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2023 01:54 AM
Hi All,
My code is just updating the else condition if the value is matched. i want to give the error message as well.
var server = request.split(',');
for (var i = 0; i < server.length; i++)
{
var gr = new GlideRecord("u_server_details");
gr.addQuery("u_server_name.name", server[i].trim());
gr.query();
while (gr.next()) {
if (!gr.next()){
response.setStatus(400);
respObj.body = {
"message": "server details are not updated",
"detail": "faliure",
};
}
else{
response.setStatus(200);
gr.patch_date = requestTime;
gr.update();
respObj.body = {
"message": "Server Details are updated",
"detail": "Success",
};
}
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2023 08:29 AM
next question
do you want to show error message containing the name of servers not found in table?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2023 03:17 AM
I think you are quering the table to see if there are any records, if there are no records you want to send thae message, if so the use this
for (var i = 0; i < server.length; i++)
{
var gr = new GlideRecord("u_server_details");
gr.addQuery("u_server_name.name", server[i].trim());
gr.query();
if (!gr.next()){
response.setStatus(400);
respObj.body = {
"message": "server details are not updated",
"detail": "faliure",
};
}
else{
response.setStatus(200);
gr.patch_date = requestTime;
gr.update();
respObj.body = {
"message": "Server Details are updated",
"detail": "Success",
};
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2023 03:32 AM
how many servers are you receiving in the json request body?
do you want to show error message containing the name of servers not found in table?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2023 05:27 AM
hi,
yes i am getting more than 1 server .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2023 06:14 AM
Hi Priya,
This snippet will treat your case. It shuffles your logic around and lets any servers that can be updated receive updates even when some can’t be found.
var server = request.split(',');
var responsePayload = {};
responsePayload.updateCount = 0;
responsePayload.errorCount = 0;
responsePayload.servers = {};
for (var i = 0; i < server.length; i++)
{
var grServer = new GlideRecord("u_server_details");
grServer.addQuery("u_server_name.name", server[i].trim());
//Uncomment if you want/expect to only find one server with this name - you dont need to use the while if that is true
//grServer.setLimit(1);
grServer.query();
// Bring this before your while loop and change to hasNext so you don't step into first record
if(!grServer.hasNext()){
if(server.length == 1){ // Only one server, set status
response.setStatus(400);
}
responsePayload.servers[server[i]] = {
"message": "No server named " + server[i] + " found. No update made",
"detail": "failure", //consider calling this key status or outcome because detail and message are very similar
};
responsePayload.errorCount++;
}
else{
while (grServer.next()) {
if(server.length == 1)
response.setStatus(200);
}
grServer.patch_date = requestTime;
grServer.update();
responsePayload.servers[server[i]] = {
"message": "Server patch date updated",
"detail": "success",
};
responsePayload.updateCount++;
}
}
}
if(server.length > 1 && responsePayload.updateCount > 0){
response.setStatus(200); // 200 unless they all fail
}
else{
response.setStatus(400);
}
Let me know if this helps.
Kind regards,
Astrid Sapphire
2023 Developer MVP