This topic contains 5 replies, has 0 voices, and was last updated by david.smith 7 years, 8 months ago.
-
AuthorPosts
-
March 17, 2017 at 10:25 am #1049
peterbellHi all
I have a bit of javascript to calculate the number of working hours between dates that I want to re-use on multiple scripts.
What I need to do is pass a start date/time value and an end date/time value to the library script. This is where it goes wrong.
The library script runs but it doesnt quite work as expected – the dates that I pass seem to get muddled at the point they move to the library. It appears that the hours and minutes of the date-time become the same for both the start and end dates,
important bits of my code:
Code:
define([‘N/record’,
‘./lib/workingdays’], function(record, workingDays) { etc etcCode:
function saveRecord(scriptContext) {
var preQuote = scriptContext.currentRecord;
var dateAssigned = new Date(preQuote.getValue(‘custrecord_cs15_datetime_assigned’));
var dateClosed = new Date(preQuote.getValue(‘custrecord_cs15_datetime_closed’));
alert(‘before passing to Library, dateAssigned = ‘+dateAssigned+’, dateClosed = ‘+dateClosed)if (dateAssigned && dateClosed){
var closeTime = workingDays.workingHours(dateAssigned, dateClosed);
preQuote.setValue(‘custrecord_time_to_complete’, closeTime);
}
return true;
}
and then my library:Code:
define([],function() {
function workingHours(startDate, endDate) {
alert(‘Working Hours Library Start ‘+startDate+’ , ‘+endDate)
var minutesWorked = 0;if (endDate = workHoursStart && current.getHours() This is a cached copy. Click here to see the original post.
-
March 17, 2017 at 10:40 am #1050
erictgrubaughAt what point are they wrong? What error(s) are you seeing?
It looks like you are expecting `startDate` and `endDate` to be Date objects, but `getValue` will only return them as Strings. You will likely need to cast them to Dates before you can call methods like `getHours` on them:
Code:
var dateAssigned = new Date(preQuote.getValue(‘custrecord_cs15_datetime_assigned’));
var dateClosed = new Date(preQuote.getValue(‘custrecord_cs15_datetime_closed’));
peterbell replied on 03/20/2017, 02:44 AM: Aaah, I will give this a go – thank you.
In terms of errors, I wasnt getting an error per se, but the dates were different after I sent them to the library.
-
March 17, 2017 at 11:15 am #1051
david.smithI think what Eric said is correct. Try doing a Date.parse(dateAssigned) on your dates before you send them to the library.
peterbell replied on 03/20/2017, 02:45 AM: Thank you – I’ll give it a go and report back.
-
March 20, 2017 at 3:47 am #1052
peterbellHmmm so I have added in the ‘New date’, and also tried the Date.parse method – both of which still seem to change when passed into the library script.
This is an alert that I ran just after grabbing the field values “new Date(preQuote.getValue(‘custrecord_cs15_datetime_a ssigned’));” and “new Date(preQuote.getValue(‘custrecord_cs15_datetime_c losed’));”
And then an alert when the values are passed into the library script:
It is like it i s taking the hours and minutes from the dateClosed and putting it in the date Assigned…!
-
March 20, 2017 at 8:00 am #1053
erictgrubaughCan you post your updated code with the alerts (or just edit the previous post)?
I don’t know *exactly* what’s going on; if that second alert is popping immediately inside `workingHours`, the dates should not have changed at all, especially just the time. I *do* see that you are setting `current` as a reference to `startDate` inside of `workingHours`, then modifying the time of `current`. Because JS copies Objects by reference, when you modify `current`, you are also modifying `startDate`, which in turn is just a reference to `dateAssigned`. Thus, by modifying `current`, you are modifying `dateAssigned`, so be aware of that.
peterbell replied on 03/20/2017, 08:39 AM: Thanks Eric
My first alert was triggered just after the variables were listed, and in the library it comes just after “function workingHours(startDate, endDate) { ”
I have updated the code in my first post for reference.
-
March 20, 2017 at 2:21 pm #1054
david.smithpeterbell You can’t pass in the date/time like you are to the JavaScript Date object. This doesn’t work: new Date(preQuote.getValue(‘custrecord_cs15_datetime_a ssigned’));
You need to use the N/format module with the format.parse to convert the string value into a date object.
peterbell replied on 03/21/2017, 02:03 AM: Thanks David, I’ll look into that
-
AuthorPosts
You must be logged in to reply to this topic.