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

ksmithdev
Mega Expert

I am using it, its just commented out. For some reason I need to figure out why it is pulling 190+ child tasks associated to a entry record, aka all of them, rather than the last record and only one.


Community Alums
Not applicable

It doesn't show up anywhere in the code you posted.


copyChange();




function copyChange() {


  current.update();


  copyTask(current);


      action.setRedirectURL(current);


  }




function copyTask(myParent) {


  gs.log("Function start");




  var last_month = new GlideDateTime(myParent.u_entry_month);


  last_month.addMonths(-1);




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



  var copiedtask = new GlideRecord('u_associate_detail_entry');


  var tasks = new GlideRecord('u_associate_detail_entry');


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


  tasks.addQuery('u_associate_entry.u_entry_month', last_month.getDate());


  tasks.query();


  while(tasks.next()){


        copiedtask = new GlideRecord('u_associate_detail_entry');


  copiedtask.u_associate_entry = myParent.sys_id.toString();


  copiedtask.u_number = tasks.getNextObjNumberPadded();


  copiedtask.u_assoc = tasks.u_assoc;


  copiedtask.u_project_number = tasks.u_project_number;


  copiedtask.u_enter_additional_time = tasks.u_enter_additional_time;


  copiedtask.u_units = tasks.u_units;


  copiedtask.u_iscap = tasks.u_iscap;




  gs.addInfoMessage('Detail Entry Found for Last Month and Copied to Current Timesheet: ' + copiedtask.u_project_number);




  copiedtask.insert();


  //Copy attachments for this task


  }




  gs.log("End and Project number is : " + tasks.u_project_number);





}


I italicized the area that does the math.


Inactive_Us2053
Giga Contributor

Could you use something like:



seconds_in_month = gs.dateDiff(gs.beginningOfThisMonth(), gs.endOfThisMonth(), true);


milliseconds_in_month = parseFloat(seconds_in_month) * 1000;



var last_month = current.u_entry_month.subtract(milliseconds_in_month);