- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2020 08:50 AM
I am working on a scoped application to store the service tickets generated on an another application. The application has custom tables for storing the service Tickets. The scoped application has scripted REST API to store the tickets.
The REST API iterates through the JSON input for tickets and stores them in to the custom table. When I am trying to insert batch of 2000 records, I am getting transaction time out error with maximum execution time exceeded. I can reduce the batch of tickets so that I do not get transaction time out error or increase the time out value for transaction quota.
However my question, is how to handle such platform errors in the Scripted REST API. I have tried enclosing the code in the try-catch however it looks like whenever "RESTRuntimeException" is thrown by platform the REST API does not get chance to handle the exception.
Sample code for scripted REST API:
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
try {
var inputData = request.body.data;
var tickets = inputData.tickets;
for (var i = 0; i <tickets.length; i++) {
// Code to insert the records
}
} catch (e) {
gs.error("Failed to process the request");
var serviceError = new sn_ws_err.ServiceError();
serviceError.setStatus(500);
serviceError.setMessage(e.message);
serviceError.setDetail("Failed to process request with error message: " + e.message);
response.setError(serviceError);
}
})(request, response);
Error Logs shows below stacktrace:
com.glide.rest.util.RESTRuntimeException: com.glide.rest.util.RESTRuntimeException: com.glide.rest.serializer.impl.JSONSerializer.handleSerializeException(JSONSerializer.java:166)
com.glide.rest.serializer.impl.JSONSerializer.serializeServiceResult(JSONSerializer.java:60)
com.glide.rest.handler.impl.ServiceResultHandlerImpl.serialize(ServiceResultHandlerImpl.java:130)
com.glide.rest.handler.impl.ServiceResultHandlerImpl.processServiceResultBody(ServiceResultHandlerImpl.java:96)
com.glide.rest.handler.impl.ServiceResultHandlerImpl.processServiceResult(ServiceResultHandlerImpl.java:40)
com.glide.rest.processors.RESTAPIProcessor.process(RESTAPIProcessor.java:291)
com.glide.processors.AProcessor.runProcessor(AProcessor.java:576)
com.glide.processors.AProcessor.processTransaction(AProcessor.java:264)
com.glide.processors.ProcessorRegistry.process0(ProcessorRegistry.java:181)
com.glide.processors.ProcessorRegistry.process(ProcessorRegistry.java:169)
com.glide.ui.GlideServletTransaction.process(GlideServletTransaction.java:44)
com.glide.sys.Transaction.run(Transaction.java:2228)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
java.lang.Thread.run(Thread.java:748)
I am facing two issue because of this platform behaviour
1. Since error is not handled by the REST APIs, I am not able to return custom error response to the client
2. Part of the batch records are persisted successfully and part of the batch records are not persisted. However since client has received 500 error code, client has no clue about successfully persisted records.
Appreciate any help on this.
Thanks
Ashutosh
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2020 07:53 PM
Discussed this case with ServiceNow support as well.
The platform behaviour is as designed. The support team responded that the Transaction time out error with maximum execution time exceeded is an application level error, platform cannot handled it at javascript level. (exception cannot be caught because this is client side scripting (java script))
Platform cannot send partial response when the application error occurs (for example how many records were persisted and how many were not), if the timeout happens, it just throw generic exception.
The solution is to reduce the batch size of tickets so that transaction time out error can be avoided or increase the timeout value for transaction quota to get past this error.
Thanks.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2020 10:17 PM
Hi Ashutosh,
Could you please mark answer correct so, it could be removed from unanswered question list.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2020 02:49 PM
Would it be possible to create an empty array or collection of some sort before the try/catch and within the for-loop for every successful insertion push it to the empty collection. Add the collection information to the error message?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2020 08:05 PM
Thanks for your inputs.
That's what I am precisely doing for as part of my response to the client to provide the records successfully saved on ServiceNow application. This works perfectly either when there are no errors or when there are any programming errors in the script. However whenever there is platform exception like "RESTRuntimeException" as in case of cancelled transaction due to maximum execution time exceeded, the REST API does not get the control to handle the exception. The code in the catch block is never invoked in case of "RESTRuntimeException".
I am looking for some way to handle the platform exceptions for scripted REST APIs so that there is better error handling on the client side.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2020 08:06 PM
As mentioned in earlier, I am looking for a standard way to handle platform errors programatically in my custom Scripted REST API so that I have better control over the error response sent to the client.
Any thoughts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2020 07:53 PM
Discussed this case with ServiceNow support as well.
The platform behaviour is as designed. The support team responded that the Transaction time out error with maximum execution time exceeded is an application level error, platform cannot handled it at javascript level. (exception cannot be caught because this is client side scripting (java script))
Platform cannot send partial response when the application error occurs (for example how many records were persisted and how many were not), if the timeout happens, it just throw generic exception.
The solution is to reduce the batch size of tickets so that transaction time out error can be avoided or increase the timeout value for transaction quota to get past this error.
Thanks.