Need to subtract one month from a Date field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2014 02:38 PM
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
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2014 02:57 PM
You commented out the insert() method, but then want to use the sys_id. I think you have to have a record created before you can get the sys_id from it:
// newChange.insert();
var myParent = newChange.sys_id;
So your myParent variable may be undefined.
Are your gs.log() statements showing in the system log? Are they showing the values you expect?
Is u_associate_detail_entry.u_entry_month a string or an integer? If it is an int, instead of:
tasks.addQuery('u_entry_month', last_entry_month); | |
tasks.query(); |
Try:
tasks.addQuery('u_entry_month', parseInt(last_entry_month, 10)); | |
tasks.query(); |
Or do a quick type conversion:
tasks.addQuery('u_entry_month', +last_entry_month); | |
tasks.query(); |
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2014 04:42 PM
No they are sending the full month. I need to subtract one month from current month from the field, and then I need ALL of the child records. Update is commented out so I don't add a bunch of duplicate child tasks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2014 04:43 PM
Or do a quick type conversion:
tasks.addQuery('u_entry_month', +last_entry_month); | |
tasks.query(); |
I am querying for the tasks with the same dates as the ones in my field already. Matching child tasks based on it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2014 06:30 PM
I haven't dived super deep into the code, but is there any particular reason you're not using gs.MonthsAgo()?