Can if and else block be executed on same time in a scheduled job

Ritik_23
Tera Contributor

So we have a scheduled job that runs daily and upload the attachments records of one table to AWS, in that job we have put a condition in if block that if the response code from server is 200 then we upload the file to AWS and we have a custom table as well so that record will be updated in the table as well with Attachment moved flag set as true.

And in the else block we have written that the Attachment moved flag will be false but still the file will be captured in the custom table.

 

Now we were changing the Access keys of AWS as this is a yearly thing to do, so while we were doing that the files were captured in the table with the flag as true which is the condition of the if block, but in Servicenow logs we checked and we got the log message that we have put in the else block and the record in the custom table shows flag as true but that is in the if block.

And in the error message we are also capturing the Response code now in the logs it shows the response code as 403.

 

So, overall the code written in both the blocks is being executed, now the file didn't reached to AWS Server, and got deleted from the Servicenow as it a part of the job.

 

Any idea why this could be happening???

3 REPLIES 3

Ravi Gaurav
Giga Sage
Giga Sage

Hi Ritik,

 

According to me this is a classic case of asynchronous behavior in ServiceNow!

It's likely that the issue is due to the way ServiceNow handles asynchronous requests. When you make an HTTP request to AWS, it's an asynchronous operation, meaning that the script doesn't wait for the response before moving on to the next line of code.

In your case, it's possible that the if block is being executed before the response from AWS is received, and then the else block is being executed when the response is finally received, which is why you're seeing both blocks being executed.

Here's what might be happening:

  1. The script makes an HTTP request to AWS to upload the file.
  2. The script doesn't wait for the response and moves on to the next line of code, which is the if block.
  3. The if block checks the response code, which is still pending, so it assumes it's 200 and sets the flag to true.
  4. Meanwhile, the response from AWS is received, and it's a 403 error.
  5. The script then executes the else block, which sets the flag to false, but it's too late, the flag has already been set to true.

To fix this issue, you can use the waitForResponse method to wait for the response from AWS before executing the next line of code. Here's an example:

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://aws.com/upload');
request.setMethod('POST');
request.setRequestBody(fileContent);

var response = request.executeAsync();
response.waitForResponse(10000); // wait for 10 seconds

if (response.getStatusCode() == 200) {
// upload successful, set flag to true
} else {
// upload failed, set flag to false
}


--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

The Async function that you mentioned i guess that's already has been depreciated in Servicenow.

So can't use them in our project.

ShubhamGarg
Kilo Sage

Hello @Ritik_23 ,

To execute something which is common (always execute) for if and else block, better to keep them outside of if-else block. Either If or Else gets executed at a given point in time.

 

If my response helps you in any way, kindly mark this as Accepted Solution/Helpful and help in closing this thread.

Regards,

Shubham