- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-21-2018 01:11 AM
hi everyone..
i am learning servicenow .while practising the reports i came across this schedule script excecution..
As the example stated in the servicenow docs i have created it..
what i have done is wanted the incidents that are older than 30 days
but now the problem is i am unable to understand where is the execution taking place?
could someone help me out with this
thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-21-2018 11:50 AM
Hey Vellanki,
Here is an example:
Exercise: Create a Scheduled Script Execution
In this exercise you will create and test a Scheduled Script Execution to find overdue NeedIt tasks.
Preparation
Ordinarily NeedIt Task records are created by a workflow when NeedIt requests are approved.
For this exercise you need several NeedIt Task records, some with overdue Due dates. You will create NeedIt Task records manually.
- In the main ServiceNow browser window (not Studio), use the Application Navigator to open NeedIt > NeedIt Tasks. You may or may not have NeedIt Task records already.
- Create an overdue NeedIt Task record with the State field value Open.
- Click the New button.
- Configure the NeedIt Task.
Assigned to: Beth AnglinDue date: Choose a date of your choice that is in the pastState: OpenShort description: Overdue -
Click the Submit button.
-
Create a NeedIt Task for the future with the State field value Open.
- Click the New button.
- Configure the NeedIt Task.
Assigned to: Beth AnglinDue date: Choose a date of your choice that is in the futureState: OpenShort description: Not overdue -
Click the Submit button.
- Create a NeedIt Task for the past with the State field value Work in Progress.
- Click the New button.
- Configure the NeedIt Task.
Assigned to: Beth AnglinDue date: Choose a date of your choice that is in the pastState: Work in ProgressShort description: Overdue -
Click the Submit button.
- Create a NeedIt Task for the past with the State field value Closed Complete.
- Click the New button.
- Configure the NeedIt Task.
Assigned to: Beth AnglinDue date: Choose a date of your choice that is in the pastState: Closed CompleteShort description: Due date in past but record is closed - Click the Submit button.
Create a Scheduled Script Execution
- If the NeedIt application is not open in Studio from the last exercise, open it now.
- In the main ServiceNow browser window use the Application Navigator to open System Applications > Studio.
- In the Load Application dialog, click the NeedIt application.
- Create a Scheduled Script Execution.
- In Studio, select the Create Application File button.
- In the Filter… field enter the text Scheduled OR select Server Development from the categories in the left hand pane.
- Select Scheduled Script Execution in the middle pane as the file type then select the Create button.
- Configure the Scheduled Script Execution:
Name: Find NeedIt Overdue TasksActive: Selected (checked)Run: DailyTime: 08 00 00
-
Copy this script and paste it into the Run this script field.
// Get today's time and date var rightNow = new GlideDateTime(); // Query the database for NeedIt Task records with Due date field values older // then the current time. Only return NeedIt Task records that do not have // a State field value of Closed Complete. var overdueNITasks = new GlideRecord('x_58872_needit_needit_task'); overdueNITasks.addQuery('due_date','<=',rightNow); overdueNITasks.addQuery('state','!=',3); overdueNITasks.query(); // Write a log message for each overdue NeedIt Task Record while(overdueNITasks.next()){ gs.info("Overdue NeedIt Task record = " + overdueNITasks.number); }
-
Click the Submit button to save the Scheduled Script Executions.
- Examine the script to make sure you understand what it does.
QUESTION: Which of the NeedIt Task records you created should be returned by the Scheduled Script Execution script query? If you aren’t sure, scroll to the Answers section at the bottom of this page.
Test the Scheduled Script Execution
- Force the Scheduled Script Execution to run by clicking the Execute Now button in the Scheduled Script Execution form.
- Switch to the main ServiceNow browser window and use the Application Navigator to open System Logs > System Log > Application Logs.
- Look for the Overdue NeedIt Task record messages. Do you see messages for the records you expected? If you don’t see any messages, the Scheduled Job may not have finished execution yet. To refresh the list, click the Additional Actions menu (
) and select the Refresh List menu item.
Challenge
Create a Scheduled Script Execution to query the database for NeedIt Task records with Due dates between now and 24 hours from now.
- Use the Find Overdue NeedIt Tasks Scheduled Script Execution script as a model
- Call the new Scheduled Script Execution Find NeedIt Tasks Due Soon.
- Make the Scheduled Script Execution run Daily at 8:05 AM.
- The NeedIt Tasks should not have a State field value of Closed Complete
- Test the Scheduled Script Execution to make sure it works. You may have to create or modify NeedIt Tasks to meet the criteria of between now and 24 hours from now.
There are several possible script solutions. If you get stuck, see the Answers section at the bottom of this page for a possible solution.
Answers
Question: Which of the NeedIt Task records you created should be returned by the Scheduled Script Execution script query?
Answer: The two NeedIt Task records with Due dates in the past and the State value of Open or Work in Progress should be returned by the query. Both of the records have a Short description value of Overdue. If you had existing NeedIt Task records from exercises from other modules, more than two records may be returned.
Challenge: Create a Scheduled Script Execution to query the database for NeedIt Task records with Due dates between now and 24 hours from now.
// Get today's time and date
var rightNow = new GlideDateTime();
// Query the database for NeedIt Task records with Due date field values within
// 24 hours from now. Only return NeedIt Task records that do not have
// a State field value of Closed Complete.
var dueSoon = new GlideRecord('x_58872_needit_needit_task');
dueSoon.addQuery('due_date','>=',rightNow);
dueSoon.addQuery('due_date','<',gs.hoursAgo(-24));
dueSoon.addQuery('state','!=',3);
dueSoon.query();
// Write a log message for each overdue NeedIt Task Record
while(dueSoon.next()){
gs.info("NeedIt Task due soon record = " + dueSoon.number);
}
This solution uses the GlideSystem hoursAgo() method. When passed a positive number, the method looks into the past. When passed a negative number, the method looks into the future. It may seem counterintuitive for a negative number to reference the future. The method is hoursAgo() which is why positive numbers are in the past. Two hours ago is a positive number.
Please Refer:
Exercise: Create a Scheduled Script Execution
Let me know if you need more help.!!
Thanks,
Rajashekhar Mushke
Community Leader - 18
Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-21-2018 01:48 AM
Hi
Scheduled Script Executions, also known as Scheduled Jobs, are automated, server-side script logic which executes at a specific time or on a recurring basis.
So depending on what condition you have specified the schedule script will execute in that manner.
Please send the code script which you have written. According to me the records on basis of "created" field of system the incidents that are older than 30 days will be queried.
Mark Correct if it helps.
Warm Regards,
Omkar Mone
www.dxsherpa.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-21-2018 08:51 PM
HI omkar
var gr=new glideAggregate("incident");
gr.addAggregate('COUNT');
gr.addQuery('active','true');
gr.addQuery('sys_created_on','<',gs.daysAgo(30));
gr.query();
while(gr.next());
gr.getAggregate('COUNT')!=='0';
this is the script i was using..
so to check the where the records are queried i have to check the self view ...
is this right?
what i was thinking was how to create the report using this information?
As i am new to this platform..i am trying hard on my basics ..please help me out
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-21-2018 03:15 AM
Scheduled Script is like a scheduled job in Servicenow. You are writing a script based on your business requirement and then schedule it (SPecific time of a day, daily, monthly etc) to run at a specific time.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-21-2018 11:47 AM
Hey Vellanki,
What is a Scheduled Script Execution?
Scheduled Script Executions, also known as Scheduled Jobs, are automated, server-side script logic which executes at a specific time or on a recurring basis.
Use Scheduled Script Executions when application processes require script logic to be executed based on a time schedule. For example:
- Periodically query a database table
- Find records with overdue Due dates
- Find records with Due dates in the near future
- Locate all requests from a particular user
- Look for records which have been in a certain state, such as resolved, too long
- Update records
- Set the State field value to Closed Complete after a fixed time has elapsed
- Assign unassigned records to a user
- Delete or re-assign all records created by a user
DEVELOPER TIP: Although not the primary use case, Scheduled Script Executions are useful for testing server-side script logic because they can be configured to execute on demand.
Please Refer:
Scheduled Script Executions and Events
Thanks,
Rajashekhar Mushke
Community Leader - 18
Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke