In schedule job any user select half year and year based on that it will trigger

Gillerla Rajesh
Tera Contributor

I created half year and yearly in scheduled job,

So when I select half year then the job will run half year and similarly when i select year the job will run yearly how can I achive this 

 

Pls provide me any script.

4 REPLIES 4

Hanook
Tera Contributor

i have same requirement

Iraj Shaikh
Mega Sage
Mega Sage

Hi @Gillerla Rajesh 

You can create scheduled jobs that run at specific intervals, such as half-yearly or yearly. To achieve this, you would typically use a Scheduled Script Execution (SSE) record where you can define the script to run and the schedule for execution.

Here's a basic example of how you might set up a script to run on a half-yearly or yearly schedule based on a selection. This example assumes you have a field that captures the user's selection for the frequency (e.g., `half_year` or `year`).

First, you would create a new Scheduled Script Execution record and then use a script similar to the following to determine the next run time based on the selection:

 

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

    // Get the user's selection from the record (assuming the field is named 'run_frequency')
    var frequency = current.getValue('run_frequency');

    // Calculate the next run time based on the frequency
    var nextRun = new GlideDateTime(); // Represents the current date and time

    if (frequency === 'half_year') {
        // Add 6 months for half-yearly schedule
        nextRun.addMonthsLocalTime(6);
    } else if (frequency === 'year') {
        // Add 12 months for yearly schedule
        nextRun.addMonthsLocalTime(12);
    } else {
        // Handle other cases or set a default
        gs.error('Invalid frequency selection for scheduled job: ' + frequency);
        return;
    }

    // Set the next run time for the scheduled job
    current.setValue('next_action', nextRun);
    current.update();

})(current, previous);

 


In this script:

- `current` is the GlideRecord of the scheduled job.
- `run_frequency` is the field where the user selects the frequency (you'll need to replace this with the actual field name).
- `nextRun` is a GlideDateTime object representing the current date and time.
- `addMonthsLocalTime()` is a method that adds the specified number of months to the GlideDateTime object.
- `setValue()` is used to set the `next_action` field of the scheduled job to the calculated next run time.
- `update()` is called to save the changes to the scheduled job record.

To implement this, you would need to:

1. Create a new Scheduled Script Execution record in ServiceNow.
2. Add the script to the "Script" field of the SSE record.
3. Configure the other fields of the SSE record as needed, such as the name, when to run the first time, and any conditions for execution.

Please mark this response as correct or helpful if it assisted you with your question.

Hanook
Tera Contributor

Hello@iraj

 

could you please guide us with these steps? from your post

 

assumes you have a field that captures the user's selection for the frequency (e.g., `half_year` or `year`).

First, you would create a new Scheduled Script Execution record and then use a script similar to the following to determine the next run time based on the selection:

Hanook
Tera Contributor

is it working