Script Analysis !!

SandeepKSingh
Kilo Sage

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();

2 ACCEPTED SOLUTIONS

Sandeep Rajput
Tera Patron
Tera Patron

@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.

View solution in original post

Ravi Gaurav
Giga Sage
Giga Sage

Hi @SandeepKSingh 

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/

View solution in original post

3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@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.

Ravi Gaurav
Giga Sage
Giga Sage

Hi @SandeepKSingh 

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/

Great !!.. @Ravi Gaurav