The CreatorCon Call for Content is officially open! Get started here.

Try/Catch to do catch GlideRecord SetValue in ScriptInclude

Bruno Leclerc
Kilo Expert

Hi,

I can't catch this stupid error :

Let's say I initialize this GlideRecord in a stupid way. When calling setValue method throws an error that I am not able to catch.

Why ? Thanks

 

// ----------------------------------------------------------------------

var gr=new GlideRecord(null); // Missing table name
try
{
    gr.setValue(100,"toto");
}
catch(e)
{
    gs.log("Never goes here.");
}

// ----------------------------------------------------------------------

 

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

Hi,

That's because no error is thrown? I believe.

You can try:

// ----------------------------------------------------------------------

var gr=new GlideRecord(null); // Missing table name
gr.query();
try
{
    gr.setValue(100,"toto");
}
catch(e)
{
    gs.log("Never goes here.");
}

// ----------------------------------------------------------------------

So I added in a gr.query(); to now get it going.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

9 REPLIES 9

Hi Allen,

You are absolutely right. In fact, I did not see, there is indeed an error message which plants the script but it is not strictly speaking a throw I think. Which explains why we don't go into the catch. the isValid() does the trick well.

Thanks.

Akash Gurram1
Giga Guru

Hi Bruno,
For me it works when you add gr.query() as told by Allen inside the try block.

var gr=new GlideRecord(null); // Missing table name
try
{
    gr.query(); //Adding this inside the try block
    gr.setValue(100,"toto");
}
catch(e)
{
    gs.log("Never goes here.");
}

Please mark it correct and helpful if it works.

Thanks,
-Akash

Hemant Goldar
Mega Sage

Hi Bruno,

 

You may find the below thread helpful.
https://community.servicenow.com/community?id=community_blog&sys_id=a33e62addbd0dbc01dcaf3231f961952

Hope this helps!

Please mark the reply as Helpful/Correct, if applicable.

Regards,
Hemant

Gaurav Shirsat
Mega Sage

Hi Bruno

as per @Allen A A Suggestion,it worked at my side too.

try-catch-finally is used to handle runtime errors and prevent them from halting the execution of a program.

1. Return statement inside the try or catch block:-

If we have a finally block, the return statement inside try and catch block are not executed. It will always hit the finally block.

2. Variables declared inside a try block are not available in the catch or finally block

If we use let or const to declare a variable in the try block, it will not be available to catch or finally. This is because these variable declarations are block-scoped.

3. Catch without error details

In ES2019, the argument for the catch block is optional.

4. try…catch will not work on setTimeOut

If an exception happens in “scheduled” code, like setTimeout, then try..catch won’t catch it.

5. Adding a global error handler

We can register a window.onerror event listener that will run in case of an uncaught error. This will not handle the error but detect it— you would need to supply your own try…catch inside of it.

 

In the above link,you will get simples examples to learn:-

https://levelup.gitconnected.com/5-things-you-dont-know-about-try-catch-finally-in-javascript-5d6619...

Please Check the Video from Chuck Tomasi for your issue.

https://www.youtube.com/watch?v=-PAI9jmUWMM&list=PL3rNcyAiDYK2_87aRvXEmAyD8M9DARVGK&index=27

 

Please Mark Correct and Helpful

Thanks and Regards

Gaurav Shirsat

Jim Coyne
Kilo Patron

Yeah, it's kind of an odd thing that an error is not thrown, but you can check to see if the table exists or not with the "isValid()" method:

 

(function() {
    var gr=new GlideRecord(null); // Missing table name
    if (gr.isValid()) {
        try {
            gr.setValue(100,"toto");
        } catch(e) {
            gs.log("Never goes here.");
        }
    } else {
      gs.error("not a valid table");
    }
})();