how to use if inside while loop

Priya75
Tera Contributor

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",
};
}

 



}
}

1 ACCEPTED SOLUTION

@Priya75 

next question

do you want to show error message containing the name of servers not found in table?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

11 REPLIES 11

Anurag Tripathi
Mega Patron
Mega Patron

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",
};
}
}
-Anurag

Ankur Bawiskar
Tera Patron
Tera Patron

@Priya75 

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?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

hi,

 

yes i am getting more than 1 server .

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