Unexpected Console Statement - BUT WHY???

G24
Kilo Sage

I wrote some happy sys_ui_script that runs on the Portal.  Since I'm on the portal, if something goes wrong, I want to write a message to the browser log.  But when I do so, I am told "Unexpected console statement" by the script field.  I would like to know WHY?  Why is this statement "unexpected"?  Since it works fine?

 

0005.png

 

0006.png

 

1 ACCEPTED SOLUTION

AnveshKumar M
Tera Sage
Tera Sage

Hi @G24 ,

 

This is how the linting configured in ServiceNow editor fields intentionally. Why because the console.warn() or console.log() messages are visible to any logged in user if they use Developer Tools in the browser. These console messages are not required for end users.

 

And ServiceNow have an inbuilt alternative to console.log() which is jslog(). The log messages will be shown in the console if the logged in user is an admin user. Ofcourse there is no alternative to console.warn().

 

If you still want to use console.warn() and don't want the editor to throw that Unexpected message, try enclosing those between these lint rules.

/* eslint-disable-next-line no-console */

 

OR

 

/* eslint-disable no-console */
/* eslint-enable */

 

 

Please mark my answer helpful and accept as solution if it helped you 👍

 

Thanks,
Anvesh

View solution in original post

5 REPLIES 5

Danish Bhairag2
Tera Sage
Tera Sage

Hi @G24 ,

 

Could you please take a look at this article if it helps:

https://www.servicenow.com/community/developer-forum/jslog-ok-is-there-a-jswarn-method/m-p/1785975

 

Please mark my answer helpful & accepted solution if it resolves your query.

 

Thanks,

Danish

AnveshKumar M
Tera Sage
Tera Sage

Hi @G24 ,

 

This is how the linting configured in ServiceNow editor fields intentionally. Why because the console.warn() or console.log() messages are visible to any logged in user if they use Developer Tools in the browser. These console messages are not required for end users.

 

And ServiceNow have an inbuilt alternative to console.log() which is jslog(). The log messages will be shown in the console if the logged in user is an admin user. Ofcourse there is no alternative to console.warn().

 

If you still want to use console.warn() and don't want the editor to throw that Unexpected message, try enclosing those between these lint rules.

/* eslint-disable-next-line no-console */

 

OR

 

/* eslint-disable no-console */
/* eslint-enable */

 

 

Please mark my answer helpful and accept as solution if it helped you 👍

 

Thanks,
Anvesh

@AnveshKumar M Thanks, but "jslog()" is not defined apparently.  Is that unavailable on the Service Portal???

Super frustrating.

@G24  Yes! It is not available in ServicePortal. Refer the KB from ServiceNow below.

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0728539

 

Thanks,
Anvesh