can any one explain me what is the use of schedule script execution ?

vellanki
Kilo Contributor

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 

 

1 ACCEPTED SOLUTION

Rajesh Mushke
Mega Sage
Mega Sage

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.

NeedIt Approval workflow with the Create Task activity which creates NeedIt Task records

 

For this exercise you need several NeedIt Task records, some with overdue Due dates. You will create NeedIt Task records manually.

  1. 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.
  2. Create an overdue NeedIt Task record with the State field value Open.
      1. Click the New button.
      2. Configure the NeedIt Task.
            Assigned to: Beth Anglin
            Due date: Choose a date of your choice that is in the past
            State: Open
            Short description: Overdue
  3. Click the Submit button.

  4. Create a NeedIt Task for the future with the State field value Open.

      1. Click the New button.
      2. Configure the NeedIt Task.
            Assigned to: Beth Anglin
            Due date: Choose a date of your choice that is in the future
            State: Open
            Short description: Not overdue
  5. Click the Submit button.

  6. Create a NeedIt Task for the past with the State field value Work in Progress.
      1. Click the New button.
      2. Configure the NeedIt Task.
            Assigned to: Beth Anglin
            Due date: Choose a date of your choice that is in the past
            State: Work in Progress
            Short description: Overdue
  7. Click the Submit button.

  8. Create a NeedIt Task for the past with the State field value Closed Complete.
      1. Click the New button.
      2. Configure the NeedIt Task.
            Assigned to: Beth Anglin
            Due date: Choose a date of your choice that is in the past
            State: Closed Complete
            Short description: Due date in past but record is closed
  9. Click the Submit button.

Create a Scheduled Script Execution

  1. If the NeedIt application is not open in Studio from the last exercise, open it now.
    1. In the main ServiceNow browser window use the Application Navigator to open System Applications > Studio.
    2. In the Load Application dialog, click the NeedIt application.
  2. Create a Scheduled Script Execution.
  3. In Studio, select the Create Application File button.
  4. In the Filter… field enter the text Scheduled OR select Server Development from the categories in the left hand pane.
  5. Select Scheduled Script Execution in the middle pane as the file type then select the Create button.
  6. Configure the Scheduled Script Execution:
            Name: Find NeedIt Overdue Tasks
            Active: Selected (checked)
            Run: Daily
            Time: 08 00 00
  7. 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);
    	}
  8. Click the Submit button to save the Scheduled Script Executions.

  9. 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

  1. Force the Scheduled Script Execution to run by clicking the Execute Now button in the Scheduled Script Execution form.
  2. Switch to the main ServiceNow browser window and use the Application Navigator to open System Logs > System Log > Application Logs.
  3. 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 (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

View solution in original post

8 REPLIES 8

Rajesh Mushke
Mega Sage
Mega Sage

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.

NeedIt Approval workflow with the Create Task activity which creates NeedIt Task records

 

For this exercise you need several NeedIt Task records, some with overdue Due dates. You will create NeedIt Task records manually.

  1. 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.
  2. Create an overdue NeedIt Task record with the State field value Open.
      1. Click the New button.
      2. Configure the NeedIt Task.
            Assigned to: Beth Anglin
            Due date: Choose a date of your choice that is in the past
            State: Open
            Short description: Overdue
  3. Click the Submit button.

  4. Create a NeedIt Task for the future with the State field value Open.

      1. Click the New button.
      2. Configure the NeedIt Task.
            Assigned to: Beth Anglin
            Due date: Choose a date of your choice that is in the future
            State: Open
            Short description: Not overdue
  5. Click the Submit button.

  6. Create a NeedIt Task for the past with the State field value Work in Progress.
      1. Click the New button.
      2. Configure the NeedIt Task.
            Assigned to: Beth Anglin
            Due date: Choose a date of your choice that is in the past
            State: Work in Progress
            Short description: Overdue
  7. Click the Submit button.

  8. Create a NeedIt Task for the past with the State field value Closed Complete.
      1. Click the New button.
      2. Configure the NeedIt Task.
            Assigned to: Beth Anglin
            Due date: Choose a date of your choice that is in the past
            State: Closed Complete
            Short description: Due date in past but record is closed
  9. Click the Submit button.

Create a Scheduled Script Execution

  1. If the NeedIt application is not open in Studio from the last exercise, open it now.
    1. In the main ServiceNow browser window use the Application Navigator to open System Applications > Studio.
    2. In the Load Application dialog, click the NeedIt application.
  2. Create a Scheduled Script Execution.
  3. In Studio, select the Create Application File button.
  4. In the Filter… field enter the text Scheduled OR select Server Development from the categories in the left hand pane.
  5. Select Scheduled Script Execution in the middle pane as the file type then select the Create button.
  6. Configure the Scheduled Script Execution:
            Name: Find NeedIt Overdue Tasks
            Active: Selected (checked)
            Run: Daily
            Time: 08 00 00
  7. 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);
    	}
  8. Click the Submit button to save the Scheduled Script Executions.

  9. 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

  1. Force the Scheduled Script Execution to run by clicking the Execute Now button in the Scheduled Script Execution form.
  2. Switch to the main ServiceNow browser window and use the Application Navigator to open System Logs > System Log > Application Logs.
  3. 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 (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

thanks you so much for your valuable 

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..

could you explain what is the mistake i am doing..

i am not finding any records in the logs

 

Hey Vellanki,

 

did your issue is resolved ?

 

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

Rajesh Mushke
Mega Sage
Mega Sage

you can also refer below links:

Useful scheduling scripts

Creating a Scheduled Script Execution

 

Thanks,

Rajashekhar Mushke

Community Leader - 18



Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke