- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2025 04:24 AM - edited 01-29-2025 04:25 AM
Is it possible to print a stack trace from a try/catch block in ServiceNow? I want to capture the line of code where the error occurred and store it in an error record for my application.
try {
var x= new GlideRecord('incident');
var test = x.testFunction();
test.qwerty();
} catch (e) {
var stackTrace = e.????; // Need help here
gs.print('Stack trace: ' + stackTrace);
}
Expected Output:
I want something like this:
Evaluator: org.mozilla.javascript.EcmaError: "est" is not defined. Caused by error in script at line 4 1: 2: var gr = new GlideRecord('incident'); 3: var test = gr.testFunction(); ==> 4: est.adssad();
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2025 04:29 AM
@SandeepKSingh Please refer to this support article https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0683765 to know the steps to print stack trace.
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2025 04:31 AM
try {
var gr = new GlideRecord('incident');
// Intentionally calling an undefined function to trigger an error
testVariable.testFunction();
} catch (e) {
gs.error('Exception occurred:');
gs.error('Line Number: ' + (e.lineNumber || 'N/A'));
gs.error('Source Name: ' + (e.sourceName || 'N/A'));
gs.error('Error Name: ' + (e.name || 'N/A'));
gs.error('Error Message: ' + (e.message || 'N/A'));
}
the above will give you an Idea
outcome:
*** Script: LineNumber :3
*** Script: SourceName :<refname>
*** Script: Name: ReferenceError
*** Script: Message : "testVariable" is not defined.
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/

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2025 04:29 AM
@SandeepKSingh Please refer to this support article https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0683765 to know the steps to print stack trace.
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2025 04:31 AM
try {
var gr = new GlideRecord('incident');
// Intentionally calling an undefined function to trigger an error
testVariable.testFunction();
} catch (e) {
gs.error('Exception occurred:');
gs.error('Line Number: ' + (e.lineNumber || 'N/A'));
gs.error('Source Name: ' + (e.sourceName || 'N/A'));
gs.error('Error Name: ' + (e.name || 'N/A'));
gs.error('Error Message: ' + (e.message || 'N/A'));
}
the above will give you an Idea
outcome:
*** Script: LineNumber :3
*** Script: SourceName :<refname>
*** Script: Name: ReferenceError
*** Script: Message : "testVariable" is not defined.
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/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2025 04:34 AM
Great !!.. @Ravi Gaurav