Create an incident on every month first Monday by using schedule job script

Nani18
Tera Contributor

Hello Experts,

 

My requirement is 

 

On every month first Monday(not Day 1 because in this month first Monday is on 5th of this week) I need to create an incident manually . Instead of manual incident creation I am thinking to create a schedule job .

how to achieve this by script,

 

 fields mentioned below 

caller_id = ABc (reference field)

contact_type = Self (choice)

urgency = support (choice)

short_description = ABCD

 

Best regards,

Nani

6 REPLIES 6

Sohail Khilji
Kilo Patron
Kilo Patron

@Nani18 ,

 

The below logic can help you, you can update the code as needed :

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

  execute: function() {
    var today = new GlideDateTime();
    var firstMonday = new GlideDateTime();
    firstMonday.setMonthLocalTime(today.getMonthLocalTime() + 1);  // Move to the next month
    firstMonday.setDayOfMonth(1);  // Set the day to the 1st
    firstMonday.setDayOfWeek(2);  // Set the day to Monday (Sunday = 1, Monday = 2, Tuesday = 3, etc.)

    // Check if today is the first Monday of the month
    if (today.equals(firstMonday)) {
      // Create an incident here
      var inc = new GlideRecord('incident');
      inc.initialize();
      inc.short_description = 'First Monday Incident';
      inc.description = 'Incident created on the first Monday of the month';
      inc.insert();
    }
  },

  type: 'scheduleJobScript'
};

// Execute the scheduled job
var job = new scheduleJobScript();
job.execute();

 

Hope this helps. !

 

 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Sai Shravan
Mega Sage

Hi @Nani18,

 

To automate the creation of an incident on the first Monday of every month, you can create a scheduled job script

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

  execute: function() {
    // Find the first Monday of the current month
    var now = new GlideDateTime();
    var firstMonday = new GlideDateTime(now.getYear(), now.getMonth(), 1);
    while (firstMonday.getDayOfWeek() !== 2) { // Monday is represented by 2
      firstMonday.addDays(1);
    }

    // Check if today is the first Monday of the month
    if (now.equals(firstMonday, true)) {
      // Create the incident
      var incident = new GlideRecord('incident');
      incident.initialize();
      incident.caller_id = 'ABc'; // Set the caller ID
      incident.contact_type = 'self'; // Set the contact type
      incident.urgency = '2'; // Set the urgency (2 represents 'Support')
      incident.short_description = 'ABCD'; // Set the short description
      incident.insert();
    }
  },

  type: 'scheduleJobScript'
};

// Execute the scheduled job
var job = new scheduleJobScript();
job.execute();

 

Regards,

Shravan

Please mark this as helpful and correct answer, if this helps you

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Vinay T K
Tera Contributor

@Nani18 

Use below script in the condition section of your schedule job and add  Incident creation script in the Run script field. Once the condition make true, your incident creation script will run.

 

function checkmonday() {

var gdt = new GlideDateTime();

var day = gdt.getDayOfWeek();

var result = false;

 if (day == 1 && gdt.getDayOfMonth()<=7){

  result = true;

  }

 return result;

}

checkmonday();

 

Script for creating new incident

============================

var gr1 = new GlideRecord('incident');

gr1.initialize();
gr1.caller_id = 'xxxxx' // put sys_id

gr1.contact_type = "Self";

gr1.urgency = '2';

gr1.short_description = 'ABCD'
gr1.insert();

 

Regards,

Vinay T K.

Please mark this as helpful and correct answer, if this helps you

 

 

 

 

Nani18
Tera Contributor

Hello @Vinay T K 

How can I check whether it will work on that day or not...? 

How can I add today date in condition section..?

 

Best regards,

Nani