Serverless app working in local, failing after deployment

We have built an integration with ServiceNow using serverless custom app in freshdesk.
Code is working as expected in local, however when we deploy the code on server code is neither executing nor we are any error message. We are able to log till the promise statement where the SN API is being called. After that no logging is happening.

Code Snippet of SN API is being called-

function createTickettoITSM(body, actor, iparams) {
    if (!is_empty(body)) {
        var body = clean(body);
        sn.createSNTicket(body, iparams).then(function(data) {
            var snID = data['data']['result']['sid'];
            var html = `<b>Action Performed:</b> Create ITSM Ticket<br><b> Result:</b> Success  <b>ITSM Ticket ID:</b> ${snID}<br>
                        User: ${actor['name']}  Email: ${actor['email']} <br><br> <i> Please note: Maximum attachment size accepted by ITSM is 5MB<i>`
            addPrivateNote(html, body['fid'], iparams);
            var stringBody = `{\"custom_fields\":{\"${iparams['itsmField']}\":\"${snID}\","${iparams['itsmTicketUpdate']}\":\"Unresolved\"}}`
            var updateBody = JSON.parse(stringBody);
            fd.updateTicket(body['fid'], updateBody, iparams)
        }).catch(function(error) {
            var html = `Action Performed: <b>Create ITSM Ticket</b><br> Result: <b>API Failure</b><br>
            User: ${actor['name']}  Email: ${actor['email']}<br>
            <b>Error Message:</b> ${JSON.stringify(}`
            errorFunction(body['fid'], error, html, iparams)
            var stringBody = `{\"custom_fields\":{\"${iparams['itsmField']}\":\"API Failure\"}}`
            var updateBody = JSON.parse(stringBody);
            fd.updateTicket(body['fid'], updateBody, iparams)


Hi Ananya,

Happy to see you back in the community! Thanks for sharing the code along.

The Serverless functions have limited compute power and have a timeout. The timeout differs depending on which serverless event is being used. When running the app locally using FDK CLI, your whole computer’s CPU power is used to execute the function and will be faster.

I don’t see any logging statement in this code after the sn.createSNTicket method.

  1. Which Serverless event is this app using?
  2. Please add more logs in the positive flows and test until the code is executed and logged, and let us know.
  3. How much time does the API used for the integration take? It has to be calculated to check if this API and the further function will get executed within the timeout of the Serverless function.

Hi Raviraj,

  1. We are using “onTicketCreate” event.
  2. Yes, Code is getting stuck in “sn.createSNTicket” method. I tried to add logs to both
    success and failure condition of this method. But neither of them worked.
  3. As it is a serverless app, I can’t check how much time API is taking.

Hi Ananya,

The “onTicketCreate” event, like the other product events, gets 20 seconds timeout.

If the immediate log in the success or error flow didn’t get printed in the serverless logs, the execution is stuck with the same method and has timed out before the execution completes and goes to either flow.
Since you have mentioned that it works fine in the local environment, I assume there’s no issue with the syntax or the method itself.

For question #3, please check the time the API takes in other tools like cURL, Postman, or similar tools to verify outside our platform how much time the API alone requires to succeed or error out. This will help you decide if the API will succeed within 20 seconds and if it leaves more time for the other APIs in the same event handler to complete.

Does the app use the Request Method or any HTTP libraries to make API or an API SDK on the sn.createSNTicket method?
It will take time to import the library before executing the event handler if it’s a third-party HTTP library or an API SDK. So, those libraries and their dependencies’ size also matter.
Could you mention which library is used, if any?

Hi Raviraj,

We have added proper catch method to catch all the error scenarios including timeout. You can find the code mentioned above. If I can do anything else to catch the timeout error, please let me know.

However the same code is executing local, I believe 20 seconds timeout is valid for both server and local. Also I have tested the API in postman is taking 12-15 secs.

Yes, HTTP method is used to trigger a post request of ServiceNow API. We are using AXIOS version- 0.27.2

Can we have a small call to get this sorted?

Hi Ananya,

I have checked the code above. No logs are available in the success scenario of the sn.createSNTicket function. Please add a log immediately after this function succeeds before doing anything else and check if it gets logged. If not, the Serverless function is timing out before this function completes execution.

I have explained above the difference in performance between a local app simulation and running it on production. So, there will be performance differences in the function execution and internet bandwidth. The API would take almost the same time. But, 12-15 seconds for an API is closer to 20 seconds, and it might get a timeout if another API is made while considering bandwidth for importing libraries and code execution.

To optimize this function, we must understand the complete business case here. I have shared my office hours calendar link on the chat. Please find the chat widget in the bottom right of this forum and block some time to go over this. Thanks.

Hi @Ananya_Rout,

To understand the use case to suggest solutions or workarounds, we have had a solutioning office hour call today.

The causes for the issue:

  • The onTicketCreate product event executes a sequence that makes an API call to ServiceNow, and the returned ticket ID is used.
  • The product events have 20 seconds timeout. This is a hard limit and cannot be extended. This will not be limited when the app is tested locally with the Freshworks CLI (FDK).
  • When more than 1 attachment is used in the API to ServiceNow, the API fails to complete the execution. Probably because it takes more time, and it doesn’t have enough runtime to execute completely before the 20 seconds timeout of the whole event handler.
  • The event handler makes multiple API requests, and when summed up, it doesn’t leave much time for the ServiceNow API to complete and gets timed out.
  • Serverless product event timing out cannot be handled from the app or notified to the developer anywhere else.

The workaround suggested:

  1. Part of the workflow can be deleted to a Scheduled Event to execute later. It will successfully complete the execution as this handler will get 20 seconds for its execution. This will delay the workflow by at least 5 minutes as the scheduler cannot be scheduled before that. Depending on the child ticket creation volume, the scheduled event rate limit must be increased.
  2. If the limitations of the above workaround are not tolerable, an external API server can be implemented and delegate all the workflow in the onTicketCreate product event to this API. The app can just trigger the API with the necessary information. This will not have any limitations as maintained by the app developer.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.