This topic contains 5 replies, has 0 voices, and was last updated by david.smith 7 years, 8 months ago.

  • Author
    Posts
  • #1049

    peterbell

    Hi 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 etc

    Code:
    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.

  • #1050

    erictgrubaugh

    At 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.

  • #1051

    david.smith

    I 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.

  • #1052

    peterbell

    Hmmm 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…!

  • #1053

    erictgrubaugh

    Can 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.

  • #1054

    david.smith

    peterbell 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

You must be logged in to reply to this topic.