want to add last day of current month and first day of upcoming month in description

VaibhavM
Tera Contributor

I have requirement of raising catalog item request automatically using scheduled job. Other all fields are fixed and I hardcoded it but the issue is in description field

 

Description- "Bellow are the details to follow

 Stop: 31.07.2025, 5:00 PM EET
Start: 01.08.2025, 6:00 AM EET"

 

 

stop should be current months last date...time will be same

start should be upcoming months first date...time will be same

 

example

if current month is September then description should be

 

"Bellow are the details to follow

 Stop: 30.09.2025, 5:00 PM EET
Start: 01.10.2025, 6:00 AM EET"

 

I need to do this in scheduled job script.

 

screenshot is attached of existing script

 

VaibhavM_0-1753261413268.png

 

2 ACCEPTED SOLUTIONS

SANDEEP28
Mega Sage

@VaibhavM Here is the updated script

 

var todayDateTime = new GlideDateTime();
todayDateTime.setDayOfMonthUTC(35);
var lastDateCurrentMonth = todayDateTime.getDate();
todayDateTime.addDays(1);
var firstDateNextMonth = todayDateTime.getDate();

var lastDateTimeCurrentMonth = lastDateCurrentMonth + ', 5:00 PM EET';
var firstDateTimeCurrentMonth = firstDateNextMonth + ', 6:00 AM EET';


var description = "Bellow are the details to follow\n" +
                  "Stop:  " + lastDateTimeCurrentMonth + "\n" +
                  "Start:  " + firstDateTimeCurrentMonth;

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
 
// Add the catalog item (qty =1)
var item = cart.addItem('ef28326d1bd2c690257b4bcbfb', 1);

cart.setVariable(item, "requested_for", "381a5ff6db95bd506f39972619f3");
cart.setVariable(item, "user", "381a5ff6db95bd506f399f39619f3");
cart.setVariable(item, "configuration_item", "54ec805e1b50143e0edacd4bcb23");
cart.setVariable(item, "configuration_item", "54ec805e1bb1143e0edacd4bcb23");
cart.setVariable(item, "application_environment", "54ec805e1bb1c05014acd4bcb23");
cart.setVariable(item, "system_name_ci", "88b228f01b2780908e7f4d4bcb9f");
//cart.setVariable(item, "u_environment", "Environment");

cart.setVariable(item, "short_description", "Stopping YOP PREPROC application on one node");
cart.setVariable(item, "description",  description );


var req = cart.placeOrder();
 
// Update RITM
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request', req.sys_id);
ritm.query();
if (ritm.next()) {
  ritm.work_notes = "Auto-created for month‑end.";
  ritm.update();
}

 

  If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

View solution in original post

@VaibhavM 

then this sample script you can use and enhance in your logic

// Helper function to pad zeros
function pad2(n) {
  return n < 10 ? '0' + n : n;
}

// Get current date in JS Date for easy date math
var now = new Date();

// Calculate last day of the current month
var lastDayOfCurrentMonth = new Date(now.getFullYear(), now.getMonth() + 1, 0); 
// Explanation: Passing day=0 returns last day of previous month (here: next month zero-th day = last day of current month)

// Calculate first day of next month
var firstDayOfNextMonth = new Date(now.getFullYear(), now.getMonth() + 1, 1);

// Format to your required format: DD.MM.YYYY
// Your example uses day.month.year - but your example text shows "31.07.2025", so:
// Format: DD.MM.YYYY for clarity
function formatDate(d) {
  return pad2(d.getDate()) + "." + pad2(d.getMonth() + 1) + "." + d.getFullYear();
}

var stopDateString = formatDate(lastDayOfCurrentMonth);
var startDateString = formatDate(firstDayOfNextMonth);

// Compose description
var description = "Bellow are the details to follow\n" +
    "Stop: " + stopDateString + ", 5:00 PM EET\n" +
    "Start: " + startDateString + ", 6:00 AM EET";

gs.info(description);

// Use the 'description' variable to set your catalog item field

Output:

AnkurBawiskar_0-1753264125373.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

@VaibhavM 

There is some issue going on in community that images are not visible.

please add it using browse feature and add as attachment to post.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

VaibhavM
Tera Contributor
 

Ankur Bawiskar
Tera Patron
Tera Patron

@VaibhavM 

Please share your script here.

also how can START be after END?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

VaibhavM
Tera Contributor

stop and start is regarding server so don't confuse with stop and start

 

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
 
// Add the catalog item (qty =1)
var item = cart.addItem('ef28326d1bd2c690257b4bcbfb', 1);




cart.setVariable(item, "requested_for", "381a5ff6db95bd506f39972619f3");
cart.setVariable(item, "user", "381a5ff6db95bd506f399f39619f3");
cart.setVariable(item, "configuration_item", "54ec805e1b50143e0edacd4bcb23");
cart.setVariable(item, "configuration_item", "54ec805e1bb1143e0edacd4bcb23");
cart.setVariable(item, "application_environment", "54ec805e1bb1c05014acd4bcb23");
cart.setVariable(item, "system_name_ci", "88b228f01b2780908e7f4d4bcb9f");
//cart.setVariable(item, "u_environment", "Environment");

cart.setVariable(item, "short_description", "Stopping YOP PREPROC application on one node");
cart.setVariable(item, "description", "-----------");


var req = cart.placeOrder();
 
// Update RITM
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request', req.sys_id);
ritm.query();
if (ritm.next()) {
  ritm.work_notes = "Auto-created for month‑end.";
  ritm.update();
}