Server side script

ArpitG
Tera Contributor

Hello Team,

I am new in service now and willing to learn the java script. When I tried to run the script on server side I am getting the following message which you can see in the attached screenshot. Please check and suggest what needs to be done from my side and how can I learn javascript at fast paceJS error.png

5 REPLIES 5

Aniket Chavan
Tera Sage
Tera Sage

Hello @ArpitG ,

 

It looks like the reason you’re not seeing any output is that your script likely didn’t include any updates or `gs.info()` print statements. When you run a script in Scripts - Background without logging information or making changes to records, you won’t see much in the output section. To view results, try using `gs.info()` to print values or set some updates within the script.

 

Here's a simple examples to help you see output in the logs:

1. Basic Output Script This script simply prints a message to the log, so you can see how gs.info() works.

 

var message = "Hello Arpit, this is a test message!";
gs.info(message);

 

Expected Output: In the logs, you should see: *** Script: Hello Arpit, this is a test message!

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

2. Fetching and Printing User Information This script retrieves a user record by their username and prints their full name.

 

var userName = "admin"; // replace with any username
var gr = new GlideRecord('sys_user');
gr.addQuery('user_name', userName);
gr.query();

if (gr.next()) {
    gs.info("User Full Name: " + gr.getValue('name'));
} else {
    gs.info("User not found.");
}

 

Expected Output: If the user exists, you should see: *** Script: User Full Name: [Full Name of the User].
If not, you'll see: *** Script: User not found.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

3. Updating a Field and Logging the Update This script deactivates a user and logs the action. Be careful with this one, as it actually updates a record.

 

var userName = "admin"; // replace with the username of a test user
var gr = new GlideRecord('sys_user');
gr.addQuery('user_name', userName);
gr.query();

if (gr.next()) {
    gr.setValue('active', false);
    gr.update();
    gs.info("User " + userName + " has been deactivated.");
} else {
    gs.info("User not found.");
}

 

Expected Output: If the user exists and is deactivated, you'll see: *** Script: User [username] has been deactivated.
If not, you'll see: *** Script: User not found.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

4. Counting Open Incidents This script counts all open incidents and logs the number.

 

var gr = new GlideRecord('incident');
gr.addQuery('state', '!=', '7'); // assuming 7 is the closed state
gr.query();

var count = 0;
while (gr.next()) {
    count++;
}
gs.info("Total open incidents: " + count);

 

Expected Output: You’ll see the count of open incidents, like *** Script: Total open incidents: [number].

You can also add update actions to make changes in records, which will show in the logs as well.

 

These examples should give you an ideaof how to view and interpret output in the logs. Run them and experiment with adjusting the variables or logic to fit different cases! Let me know if you’d like more examples or have questions.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 

To help you get started with JavaScript and ServiceNow scripting, you can refer to the links provided by the experts @Dr Atul G- LNG  & @PrashantLearnIT . Additionally, here are a few more resources that can help you understand ServiceNow-related scripting from scratch:

 

 

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.


Regards,
Aniket