This topic contains 12 replies, has 0 voices, and was last updated by stephen.gray 8 years, 2 months ago.

  • Author
    Posts
  • #6168

    stephen.gray

    Hi All

    I’m new to scripting and have written a couple to compare the expected close date and line item dates on an opportunity record

    One of the scripts are below which fires on line valiation.

    The issue i’m having is it works great in Chrome and only alerts me if the expected close date is after the earliest line date. In Firefox and Edge it alerts me no matter what – grrr!

    Any ideas whats wrong?

    function ValidateLineDate()

    {

    var lineDate = Date.parse(nlapiGetCurrentLineItemValue(‘item’,’cu stcol1′));

    var expectedClose = Date.parse(nlapiGetFieldValue(‘expectedclosedate’) );

    if(lineDate >= expectedClose)

    {return true;}

    else

    if(confirm(“The date on this line is earlier than the Expected Close Date. nIs this ok?”)== true)

    {return true;}

    else

    {return false;}

    }

    thanks

    Stephen
    This is a cached copy. Click here to see the original post.

  • #6169

    khultquist

    A couple of things…

    Are both fields ‘Date’ types in NetSuite, or is either of them ‘Date/Time’?

    Try using the NetSuite function nlapiStringToDate() instead of Date.parse()

    Code:
    function ValidateLineDate() {
    var lineDate = nlapiStringToDate(nlapiGetCurrentLineItemValue(‘item’, ‘custcol1’));
    var expectedClose = nlapiStringToDate(nlapiGetFieldValue(‘expectedclosedate’));

    if (lineDate >= expectedClose) {
    return true;
    } else
    if (confirm(“The date on this line is earlier than the Expected Close Date. nIs this ok?”) == true) {
    return true;
    } else return false;
    }

  • #6170

    stephen.gray

    Hi

    The custom date field is definitely a “Date” type field. Not sure about the expected close date as its a standard NetSuite field – any way for me to check the type of standard fields?

    Anyway the nlapiStringToDate worked a treat – very helpful!!

    I had actually tried using that function before but there must’ve been an error somewhere else in the code at that stage as it hadnt worked. Its working now so thats all that matters!

    Many thanks again for the quick response!!

    thanks

    Stephen

  • #6171

    david.smith

    It’s actually good practice to use the nlapiStringToDate and the nlapiDateToString because NetSuite uses the user’s personal preferences to format the dates. If someone has a format not supported by your script it can fail.

  • #6172

    khultquist

    Glad that fixed it! Also, you can clean up the code quite a bit like this

    Code:
    function ValidateLineDate() {
    var lineDate = nlapiStringToDate(nlapiGetCurrentLineItemValue(‘item’, ‘custcol1’));
    var expectedClose = nlapiStringToDate(nlapiGetFieldValue(‘expectedclosedate’));
    var msg = “The date on this line is earlier than the Expected Close Date. nIs this ok?”;

    if (lineDate < expectedClose && !confirm(msg)) return false; // lineDate earlier and user clicked Cancel
    return true;
    }

  • #6173

    egrubaugh

    Code:
    function ValidateLineDate() {
    var lineDate = nlapiStringToDate(nlapiGetCurrentLineItemValue(‘item’, ‘custcol1’));
    var expectedClose = nlapiStringToDate(nlapiGetFieldValue(‘expectedclosedate’));
    var msg = “The date on this line is earlier than the Expected Close Date. nIs this ok?”;

    if (lineDate = expectedClose) || confirm(msg));
    }

  • #6174

    stephen.gray

    cool thanks for the tips!!

  • #6175

    stephen.gray

    Hi Guys

    Just found that this script runs when i try and add a line item in a different tab i.e. the sales team tab when i add a sales rep to an opportunity. Its only supposed to run when i add an item for sale. Its strange as the only place where there is a field called custcol1 is on the items tab.

    any advice?

    thanks

    Stephen

  • #6176

    david.smith

    Filter your code with the type parameter in your code.

    validateLine
    type : the sublist internal ID
    Boolean
    This client event occurs prior to a line being added to a sublist (inlineeditor or editor sublists only). It can be thought of as the saveRecord equivalent for sublist line items (inlineeditor and editor).

    Returns false to reject the operation. References to fields should be done using nlapiGetCurrent*** functions.

    This function is automatically passed the type argument from the system.

    https://netsuite.custhelp.com/app/an…w/validateline

  • #6177

    khultquist

    Yep, what David says. The type of your sublist should be ‘item’ so something like this

    Code:
    function ValidateLineDate(type) {
    if (type == ‘item’) {
    var lineDate = nlapiStringToDate(nlapiGetCurrentLineItemValue(‘item’, ‘custcol1’));
    var expectedClose = nlapiStringToDate(nlapiGetFieldValue(‘expectedclosedate’));
    var msg = “The date on this line is earlier than the Expected Close Date. nIs this ok?”;
    if (lineDate < expectedClose && !confirm(msg)) return false; // lineDate earlier and user clicked Cancel
    }
    return true;
    }

  • #6178

    stephen.gray

    thanks Guys that worked.

    I have another strange one i’m hoping you can help with

    First of all my guess is that there is a much easier way to write the below code which checks on save of an opportunity if the expected close date is later than the earliest date on the line items. This is to alert the user if they’ve edited an opportunity close date and not updated the dates on the line items to be after the expected close (which they should be)

    The strange thing is this works in the sandbox on chrome and firefox but it doesnt work in the production system.

    Any help would be much appreciated

    function EarliestLineDateVExpectedClose()

    {

    var dates = [

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 1))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 2))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 3))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 4))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 5))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 6))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 7))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 8))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 9))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 10))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 11))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 12))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 13))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 14))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 15))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 16))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 17))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 18))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 19))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 20))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 21))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 22))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 23))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 24))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 25))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 26))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 27))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 28))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 29))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 30))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 31))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 32))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 33))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 34))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 35))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 36))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 37))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 38))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 39))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 40))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 41))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 42))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 43))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 44))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 45))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 46))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 47))),

    (nlapiStringToDate(nlapiGetLineItemValue(‘item’, ‘custcol1’, 48))),

    ],earliestLineDate = dates.reduce(function (pre, cur) {

    return nlapiStringToDate(pre) > nlapiStringToDate(cur) ? cur : pre;

    });

    var expectedClose = nlapiStringToDate(nlapiGetFieldValue(‘expectedclos edate’));

    var msg = “The Expected Close Date is later than the earliest line date. nIs this ok?”

    if(earliestLineDate < expectedClose && !confirm(msg)) return false; // earliestLineDateSG earlier and user clicked cancel

    return true;

    }

  • #6179

    khultquist

    You should use a for loop instead of hard coding. I’m not sure why it would work in sandbox but I would suspect it’s in your dates.reduce function.

    Code:
    function EarliestLineDateVExpectedClose() {

    var numLines = nlapiGetLineItemCount(‘item’);
    var expectedClose = nlapiStringToDate(nlapiGetFieldValue(‘expectedclosedate’));
    var earliestLineDate = expectedClose;
    var msg = “The Expected Close Date is later than the earliest line date. nIs this ok?”

    for (var i = 1; numLines && i <= numLines; i++) {
    var thisDate = nlapiStringToDate(nlapiGetLineItemValue('item', 'custcol1', i));
    if (thisDate < earliestLineDate) earliestLineDate = thisDate;
    }

    if (earliestLineDate < expectedClose && !confirm(msg)) return false; // earliestLineDate earlier and user clicked cancel
    return true;

    }

  • #6180

    stephen.gray

    I recopied the old code into the production and its now working so i mustn’t have copied it properly.

    I also tried your code in the sandbox and it works well and its obviously much cleaner – thanks for your help again!!

You must be logged in to reply to this topic.