0

My Requirement:

I'm having to fields Start Date and End Date, If the End Date is left empty while saving the record, the End Date Field value is populated with plus 1 year based on the entered from date.

My Issue:

If the Start Date is "9/1/2016" and the End Date is Left Empty means it should automatically populate the End Date value as "8/31/2016" but it returning the End Date value as "9/0/2016" and also i'm getting the following ERROR MESSAGE

Error: JS_EXCEPTION INVALID_FLD_VALUE You have entered an Invalid Field Value Invalid Date for the following field: custrecord_end_date

CODE: SCRIPT : CLIENT SCRIPT, EVENT :SaveRecord

function saveRecord(scriptContext) {
  var newRecord= scriptContext.currentRecord;
 var fromDate = new Date(newRecord.getValue('custrecord_created_date'));
 var endDate = newRecord.getValue('custrecord_end_date');
   if (endDate == null || endDate == '') {
    //getting plus 1 year based on the From Date
    tempEndDate = addingPlusYearOfTheCurrentDate(fromDate);

   //setting the value to the End Date Field
     newRecord.setValue('custrecord_end_date', tempEndDate);

}
}
  // Add Plus Year from the Start Date when the End Date is Empty
        function addingPlusYearOfTheCurrentDate(fromDate ) {
            var date = new Date();
            var Month = (fromDate.getMonth() + 1);
            var Dates = (fromDate.getDate() - 1);
            var Year = (fromDate.getFullYear() + 1);
            var last_Day = new Date(Month + '/' + Dates + '/' + Year);


            log.debug('last_Day:', last_Day);
            return last_Day;
        }
Deepan Murugan
  • 721
  • 2
  • 19
  • 41

4 Answers4

3

Not sure why you expected to be able to subtract 1 from 1 and get anything other than 0, but you can solve this problem by using the Date object's setFullYear() and setDate().

function addingPlusYearOfTheCurrentDate(fromDate) {
  var date = new Date(fromDate);

  date.setFullYear(date.getFullYear() + 1);
  date.setDate(date.getDate() - 1);

  return date;
}

console.log(addingPlusYearOfTheCurrentDate(new Date(2015, 10, 1)));
JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • @RobG You're right. I hadn't looked carefully enough and thought OP had the function returning a string. Didn't notice he was parsing the date all over again. I've changed it to return `date`. – JLRishe Sep 07 '16 at 04:57
1

Parsing strings with the Date constructor (and Date.parse, they are equivalent for parsing) is strongly recommended against since parsing is almost entirely implementation dependent and inconsistent. Manually parse strings with a custom function or use a library.

Adding a year to a Date is fairly simple, but it seems you want the date that is one day prior to the same date next year. So add one year then subtract one day.

// Parse m/d/y format string to a Date and validate the result
function parseMDY(s) {
  var b = s.split(/\D/);
  var d = new Date(b[2], --b[0], b[1]);
  return d && d.getMonth() == b[0]? d : new Date(NaN);
}
   
// Add 1 year to a Date
function addYear(d) {
  if (Object.prototype.toString.call(d) != '[object Date]') return;
  d.setFullYear(d.getFullYear() + 1);
  d.setDate(d.getDate() -1);
  return d;
}

var d = parseMDY('9/1/2016');
console.log(d.toLocaleString())
addYear(d);
console.log(d.toLocaleString())

Note that for 29 February, adding one year gives 1 May, then subtracting one day will give 28 February.

RobG
  • 142,382
  • 31
  • 172
  • 209
1

You should use the method nlapiStringToDate() for string to date conversions, as NetSuite gives date field value as string, which you must convert to date, and before you set back date, you must use nlapiSetFieldValue(YOUR_FIELD_ID, nlapiStringToDate(dateObject))

Please see below on suggested usage on reading and setting date fields.

function saveRecord(scriptContext) {
  var newRecord = scriptContext.currentRecord;
  var fromDate = nlapiStringToDate(newRecord.getValue('custrecord_created_date'));
  var endDate = nlapiStringToDate(newRecord.getValue('custrecord_end_date'));
  if (endDate == null || endDate == '') {
    //getting plus 1 year based on the From Date
    tempEndDate = addingPlusYearOfTheCurrentDate(fromDate);

   //setting the value to the End Date Field
     newRecord.setValue('custrecord_end_date', nlapDateToString(tempEndDate));
}
prasun
  • 7,073
  • 9
  • 41
  • 59
1

Is this a 1.0 or 2.0 script?

NetSuite's 1.0 API offers a couple date manipulation methods that might be helpful to you here: nlapiAddMonths and nlapiAddDays, as well as the Date-String conversion methods.

Here's an example of what you could do in 1.0

// 1.0 API does not pass scriptContext to saveRecord
function saveRecord() {

    // Use nlapiStringToDate instead of raw Date constructor
    var fromDate = nlapiStringToDate(nlapiGetFieldValue('custrecord_created_date'));

    // Instead of the full extra conditional, just use || as fallback
    var endDate = nlapiStringToDate(nlapiGetFieldValue('custrecord_end_date')) ||
            calculateEndDate(fromDate);

    // setting the value to the End Date Field
    nlapiSetFieldValue('custrecord_end_date', nlapiDateToString(endDate));
}

/** @param fromDate {Date} */
function addYear(fromDate) {
    return nlapiAddMonths(fromDate, 12);
}

/** @param fromDate {Date} */
function dayBefore(fromDate) {
    return nlapiAddDays(fromDate, -1);
}

/** @param startDate {Date} */
function calculateEndDate(startDate) {

    // add 1 year first, then subtract one day
    return dayBefore(addYear(startDate));
}

If you're using 2.0 just add a comment, and I will try to update the example if I can. If you've got any questions about how this works, feel free to let me know as well.

erictgrubaugh
  • 8,519
  • 1
  • 20
  • 28
  • Yes Erict, It is SuiteScript 2.0 version. – Deepan Murugan Sep 07 '16 at 17:27
  • Ah; unfortunately those date APIs don't yet exist in 2.0. You could consider adding [`moment`](http://momentjs.com/) and using its date manipulation APIs, which are incredibly powerful. You will also want to look at the `N/format` module for transforming `String`s to `Date`s and vice-versa – erictgrubaugh Sep 07 '16 at 20:15