How do i log my script?

asd22
Tera Contributor

How do i log my code so i can see in system logs where my script goes wrong?

 

(function executeRule(current, previous /*null when async*/ ) {

var grL = new GlideRecord('u_leiekategori');
var a = current.u_maanedspris;
grL.u_gjeldene_leiepris = a;
grL.update();


})(current, previous);

 

i wanna log this so i can view where the error happens.

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

Hi,

Out of box, if there's an error here it'll automatically be captured in the system log.

However, based on what you're doing with GlideRecord, this may not throw an error and instead it'll create a record (because you didn't format this correctly to successfully query the table and are trying to "update" nothing...so a new record would be created).

You can insert your own logging though by using example:

(function executeRule(current, previous /*null when async*/ ) {

gs.info("***DEBUG - Hi I'm at the start of this script");
var grL = new GlideRecord('u_leiekategori');
var a = current.u_maanedspris;
gs.info("***DEBUG - This is the value set for JavaScript variable 'a': " + a);
grL.u_gjeldene_leiepris = a;
grL.update();
gs.info("***DEBUG - Bye!");


})(current, previous);

So the above shows 3 places I'm logging things.

Otherwise, you may want to review using a try catch: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch

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

12 REPLIES 12

Anurag Tripathi
Mega Patron
Mega Patron

You can add a log as shown below

gs.log('message', 'source'); //source here can be any string that makes it easier to find the logs or report on them if needed.

In the log table the  i add a filter for all logs created for the source i added and created in the last 15 mins. this gives me a refined list and don't have to go finding my log in  a whole pile of messages that are not useful for me.

 

On a side note, your glide record is not correct, you need to find a record and then try to update it. currently your just query the table and trying to update but what record?

See this: Querying tables in script | ServiceNow Docs

-Anurag

Oh i see its supposed to update the field u_gjeldene_leiepris in the table u_leiekategori

Which row of the tab;e?

-Anurag

The script is meant to update u_gjeldene_leiepris in the u_leiekategori table, to the same valueas u_maanedspris wich is in the u_leiepris table has when its updated