Need to subtract one month from a Date field.

ksmithdev
Mega Expert

Hi guys!

 

I need to subtract one month from my date field, which is formatting as such: 2014-07-2014

 

Here is what I am running as a UI Action, the problem is that it is failing the query time and time again, pulling a bunch of child records.

Essentially this UI action needs to from a "New Timesheet Card" form, with a click of this button, pull Last Months time card and its child tasks. The query is commented out below. I am wondering if anyone else can help me with a easier solution than what I have put together below with Parsing ints.

 

function copyChange() {

  //Get the current sys_id value for querying

  //Initialize new change for insertion

  var newChange = new GlideRecord('u_associate_entry');

  newChange.u_number = getNextObjNumberPadded();

  newChange.u_entry_month = '';

  newChange.u_assoc = current.u_assoc;

  // newChange.insert();

  var myParent = newChange.sys_id;

 

  //Copy associated tasks and CIs

  copyTask(myParent);

  gs.addInfoMessage('Entry ' + newChange.u_number + ' created.');

  action.setRedirectURL('u_associate_entry.do?sys_id=' + myParent);

}

 

 

 

 

function copyTask(myParent) {

  //Find the current change tasks and copy them

  var arr = [];

  var last_month = new GlideDateTime(current.u_entry_month);

// last_month.addMonths(-1);

  var month = last_month.getDisplayValue().toString();

  arr.push(month);

  arr.split('-');

  var last_entry_month = arr[0];

  // if {

/* month = parseInt(arr[1] - parseInt[1]);

 

  //} else {

  year = parseInt(arr[0] - parseInt[1]);

  month = parseInt(arr[1] - parseInt[1]);*/

 

 

  // }

 

 

  gs.log("Last : " + last_entry_month);

  gs.log("Month : " + month);

 

 

  gs.log("Last Month is : " + last_month);

  var copiedtask = new GlideRecord('u_associate_detail_entry');

  var tasks = new GlideRecord('u_associate_detail_entry');

  tasks.addQuery('u_assoc', current.u_assoc);

  tasks.addQuery('u_entry_month', last_entry_month);

  tasks.query();

  while(tasks.next()){

  copiedtask.u_associate_entry = myParent;

  copiedtask.u_entry_month = tasks.u_entry_month;

  copiedtask.u_assoc = tasks.u_assoc;

  copiedtask.u_project_number = tasks.u_project_number;

  copiedtask.u_units = tasks.u_units;

  copiedtask.u_iscap = tasks.u_iscap;

  gs.addInfoMessage('Task found : ' + copiedtask.u_project_number);

  // copiedtask.insert();

  //Copy attachments for this task

  }

}

11 REPLIES 11

I tried this, and it blew up grabbing ALL of my child tasks.


garyopela
ServiceNow Employee
ServiceNow Employee

var gdt = new GlideDateTime(myParent.u_entry_month.getGlideObject());


gdt.addMonths(-1);



Try that.