Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Logging the message in logs through business rule & script include is not working

SK41
Giga Guru

Hi,

 

I have a requirement where I have to set the log message as "Record has id present" in logs if the id is present and if it is not present it should log error message as "Record does not have id present". I have to do the same thing few business rules, so I have created the script include and calling it in BRs. But the code is not working. Please help in identifying what is the issue here. This is in a scoped application. Below is the code: 

 

Script Include

var LogMessage = Class.create();
LogMessage.prototype = {
    initialize: function() {
    },

LogMsg : function(id) {
   if(id != " "){
    var currentTimestamp = new GlideDateTime();
    currentTimestamp = currentTimestamp.getDisplayValue();
    var logMessage = 'Record: Record has id present' + currentTimestamp;
    gs.info(logMessage);
}
else
var err = 'Record does not have id present';
gs.error(err);
    },
 
Business Rule
(function executeRule(current, previous /*null when async*/) {
 
var sc = new global.LogMessage();
         sc.LogMsg(current.sys_id);     //here it is passing the sys id of the id record.
})(current, previous);
 
But, the messages are not getting logged in logs.
Kindly, assist what is the issue here.
 
Thanks!
1 ACCEPTED SOLUTION

SK41
Giga Guru

There was issue with calling the script include and I passed the actual id instead of sys_id, then it worked:

Below is the working script:

Business Rule

 

var trd = current.trade_id;
    var sc = new LogMessage();
        sc.LogMsg(trd);
    
Script Include
 
var LogMessage = Class.create();
LogMessage.prototype = {
    initialize: function() {
    },

LogMsg : function(id) {
var tt = id;
   if(tt != " "){
    var currentTimestamp = new GlideDateTime();
    currentTimestamp = currentTimestamp.getDisplayValue();
    var logMessage = 'Record: Record has id present' + currentTimestamp;
    gs.info(logMessage);
}
else{
var err = 'Record does not have id present';
gs.error(err);
}
    },

View solution in original post

4 REPLIES 4

Bert_c1
Kilo Patron

Use the "Script Logs Statements" module. And query on message, starts with, "Record". Level = 'Information' or 'Error'.

James Chun
Kilo Patron

Hi @SK41,

 

Are you saying both of the .error() and .info() logs are not showing up?

Is your script include in a scope? If so, you wouldn't need to change the script in your BR.

 

var sc = new yourscope.LogMessage();

 

 

If you have done the above and it's still not working, add a log at the first line of the LogMsg function as well and see if it prints out anything.

 

Thanks

Rajdeep Ganguly
Mega Guru


Here are the possible issues and solutions:

1. **Scope Issue**: Since you mentioned that this is a scoped application, you need to ensure that the script include is accessible from the business rule. If the script include is in a different scope than the business rule, it won't be accessible. To fix this, you can either move the script include to the same scope as the business rule or make the script include accessible to all application scopes by checking the 'Accessible from all application scopes' checkbox in the script include form.

2. **Error in the Script Include**: There is an error in your script include. The else statement is not properly formatted. It should be like this:

javascript
LogMsg : function(id) {
if(id != " "){
var currentTimestamp = new GlideDateTime();
currentTimestamp = currentTimestamp.getDisplayValue();
var logMessage = 'Record: Record has id present' + currentTimestamp;
gs.info(logMessage);
} else {
var err = 'Record does not have id present';
gs.error(err);
}
}


3. **Error in the Business Rule**: The business rule is not properly formatted. It should be like this:

javascript
(function executeRule(current, previous /*null when async*/) {
var sc = new global.LogMessage();
sc.LogMsg(current.sys_id.toString()); //here it is passing the sys id of the id record.
})(current, previous);


4. **Logging Level**: Ensure that your instance logging level is set to include 'info' and 'error' messages. You can check this by navigating to 'System Logs > System Log Configuration' and ensuring that the 'Logging Level' is set to 'All'.

5. **Check the sys_id**: Ensure that the sys_id is not null or empty. If it is, the log message will not be printed.

6. **Check the Logs**: Ensure that you are checking the correct logs. The 'gs.info' and 'gs.error' messages are printed in the system logs. You can view these logs by navigating to 'System Logs > All'.


nowKB.com

For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - https://nowgpt.ai/

For the ServiceNow Certified System Administrator exams try this :
https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER

SK41
Giga Guru

There was issue with calling the script include and I passed the actual id instead of sys_id, then it worked:

Below is the working script:

Business Rule

 

var trd = current.trade_id;
    var sc = new LogMessage();
        sc.LogMsg(trd);
    
Script Include
 
var LogMessage = Class.create();
LogMessage.prototype = {
    initialize: function() {
    },

LogMsg : function(id) {
var tt = id;
   if(tt != " "){
    var currentTimestamp = new GlideDateTime();
    currentTimestamp = currentTimestamp.getDisplayValue();
    var logMessage = 'Record: Record has id present' + currentTimestamp;
    gs.info(logMessage);
}
else{
var err = 'Record does not have id present';
gs.error(err);
}
    },