- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-18-2020 06:37 AM
Hi All,
i have a scenario in one of the integration like.
If i get response as "200" then update the task with state as "closed completed"
else if response get 400 then wait for 60 then update the task as "not successful" in the worknotes.
I have tried like below.. but wait time is not happening. can anyone help me on thisvar response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log("Display responsebody"+responseBody);
var requestStringECC = r.getRequestBody();
var requestString=responseBody.toString();
var parser = new JSONParser();
var parsed = parser.parse(responseBody);
if(httpStatus==200){
current.state==3;
current.work_notes = "Status: "+httpStatus+"Successful";
}
else if(httpStatus==400||httpStatus==500){
gs.log("inside 400 blok"+httpStatus);
var res=response.waitForResponse(60);
gs.log("inside response waittime blok"+res);
if(res>60){
gs.log("inside response waittime if blok"+res);
current.state==1;
current.work_notes="Status: "+httpStatus+"Unable toprocess your request"
}
}
Can anyone help to resolve the issue.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-18-2020 07:43 AM
Hi Rani,
please try this
else if(httpStatus==400||httpStatus==500){
gs.log("inside 400 blok"+httpStatus);
var seconds = 60;
var seconds = parseInt(seconds, 10) * 1000;
var start = parseInt(new Date().getTime()) + seconds;
while(start>parseInt(new Date().getTime())){
// do nothing
}
//var res=response.waitForResponse(60);
gs.log("outside the wwaitblock while loop response w");
current.state==1;
current.work_notes="Status: "+httpStatus+"Unable toprocess your request.";
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-18-2020 06:56 AM
you should use standard JavaScript function timeout for this purpose. Please refer below example
var varWarn1 = setTimeout(alertfunc1, 900000);
function alertfunc1 {
alert("This is warning message you should complete your request within next 15 minutes");
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-18-2020 06:56 AM
> if response get 400 then wait for 60 then update the task as "not successful" in the worknotes.
Do you want to wait for 60 seconds after getting a 400 or wait for 60 seconds for an operation to complete?
waitForResponse(60) is the later.
If the former, use setTimeout()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-18-2020 07:22 AM
Hi Rani,
please try this script
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log("Display responsebody"+responseBody);
var requestStringECC = r.getRequestBody();
var requestString=responseBody.toString();
var parser = new JSONParser();
var parsed = parser.parse(responseBody);
if(httpStatus==200){
current.state==3;
current.work_notes = "Status: "+httpStatus+"Successful";
}
else if(httpStatus==400||httpStatus==500){
var ms = 60*1000; // convert 60 seconds to milliseconds
var endSleep = new GlideDuration().getNumericValue() + ms;
while ( new GlideDuration().getNumericValue() < endSleep) {
//wait
}
current.state==1;
current.work_notes="Status: "+httpStatus+"Unable toprocess your request"
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-18-2020 07:37 AM
Hi Ankur,
Thanks for your response. I have tried as per your suggestion.
else if(httpStatus==400||httpStatus==500){
gs.log("inside 400 blok"+httpStatus);
var ms = 60*1000; // convert 60 seconds to milliseconds
var endSleep = new GlideDuration().getNumericValue() + ms;
gs.log("inside while waittime blok"+endSleep);
while (new GlideDuration().getNumericValue() < endSleep) {
//wait
gs.log("inside while waittime blok");
}
//var res=response.waitForResponse(60);
gs.log("outside the wwaitblock while loop response w");
current.state==1;
current.work_notes="Status: "+httpStatus+"Unable toprocess your request.";
}
The highlighted log is triggered this many times "222,975"
I am not sure is that expected behavior or i should make any changes?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-18-2020 07:43 AM
Hi Rani,
please try this
else if(httpStatus==400||httpStatus==500){
gs.log("inside 400 blok"+httpStatus);
var seconds = 60;
var seconds = parseInt(seconds, 10) * 1000;
var start = parseInt(new Date().getTime()) + seconds;
while(start>parseInt(new Date().getTime())){
// do nothing
}
//var res=response.waitForResponse(60);
gs.log("outside the wwaitblock while loop response w");
current.state==1;
current.work_notes="Status: "+httpStatus+"Unable toprocess your request.";
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader