I have a suitelet whose form has a custom date fie...
# suitescript
k
I have a suitelet whose form has a custom date field. I am passing the entered date in the form to a saved search to reset its criteria, however, I keep on getting this error:
Copy code
INVALID_DATE_VALUE_1_MUST_MATCH_2
this is how the field is set up in the form:
Copy code
form.addField({
    id: 'custpage_enddate',
    type: ui.FieldType.DATE,
    label: 'End Date'
});
this is how I am passing the date to the search filter:
Copy code
var formated_date = format.format({
    value: moment(param.enddate),
    type: format.Type.DATE
});
var enddate_filter = search.createFilter({
    name: 'shipdate',
    operator: search.Operator.ON,
    values: formated_date
});
filters.push(enddate_filter);
what am I doing wrong?
b
a moment is not a Date nor a string
the values parameter should be either a Date or a string
k
I updated it to the following and still getting the same error
Copy code
log.debug('param.enddate type', typeof(param.enddate));
var formated_date = format.format({
    value: param.enddate,
    type: format.Type.DATE
});
var enddate_filter = search.createFilter({
    name: 'shipdate',
    operator: search.Operator.ON,
    values: formated_date
});
filters.push(enddate_filter);
b
undefined is still not a Date or a string
k
Copy code
log.debug('param.enddate type', typeof(param.enddate));
gives me
string
and the value looks like this:
Wed Aug 04 2021 00:00:00 GMT-0400 (Eastern Daylight Time)
b
that does look like a string
but thats not a string usable by N/format
nor does that match the id of your custpage_enddate field
k
param.enddate
is the parameter coming from context.request stored like this:
Copy code
var param = {
'enddate':context.request.parameters.enddate
}
b
still the same issue
Copy code
form.addField({
    id: 'custpage_enddate',
    type: ui.FieldType.DATE,
    label: 'End Date'
});
suggests that your fields id is custpage_enddate
k
sorry about the confusion, so
custpage_enddate
is displayed in the form, there is also a button in the form which when clicked triggers a function on client script module, the client script then resolves the script url and passes the value of
custpage_enddate
in the suitelet form in the varialble
enddate
b
your code messed up if
Wed Aug 04 2021 00:00:00 GMT-0400 (Eastern Daylight Time)
is the input you are getting
thats unusable with N/format
you would need to convert that to a Date first
k
so the following worked after converting parameters to date format:
Copy code
var raw_date = new Date(param.enddate);
                    var formated_date = format.format({
                        value: raw_date,
                        type: format.Type.DATE
                    });
                    var enddate_filter = search.createFilter({
                        name: 'shipdate',
                        operator: search.Operator.ON,
                        values: formated_date
                    });
                    filters.push(enddate_filter);
however, enddate input was
"enddate":"Thu Aug 05 2021 00:00:00 GMT-0400 (Eastern Daylight Time)"
but when the date was pushed to the search filter it was off by a day, and seems to be off by a day for any date input
Copy code
{
  "name": "shipdate",
  "operator": "on",
  "values": [
    "08/04/2021"
  ],
  "formula": null,
  "summarytype": null,
  "isor": false,
  "isnot": false,
  "leftparens": 0,
  "rightparens": 0
}
b
same problem, whatever is setting your enddate is wrong
creating Dates by the timestamp version of the Date constructor is unreliable
its much safer using the ISO format or the unix epoch
k
ahh I see
I just tried this
Copy code
var raw_date = new Date(param.enddate);
                    var raw_date_toISO = raw_date.toISOString();
                    log.debug('Raw date to ISO string',raw_date_toISO );
                    var formated_date = format.format({
                        value: raw_date_toISO,
                        type: format.Type.DATE
                    });
                    var enddate_filter = search.createFilter({
                        name: 'shipdate',
                        operator: search.Operator.ON,
                        values: formated_date
                    });
but I am still getting the same error
b
that wont help
whatever is setting the enddate needs to set the enddate with the correct value
k
I also printed the date on the client script on my browser console, and the date received as soon as its entered is the same format:
Thu Aug 05 2021 00:00:00 GMT-0400 (Eastern Daylight Time)
b
that doesnt sound right, usually a Date printed to the console will have an indication that it is a Date
there is a difference between a Date, and the output of the Date's toString method
k
date on the console printed by client script
this is kind what the suitelet looks like
Copy code
/*suitelet*/
form.addField({
    id: 'custpage_enddate',
    type: ui.FieldType.DATE,
    label: 'End Date'
});

form.addButton({
    id: 'search_btn',
    label: 'Search',
    functionName: 'onDemandSearch'
});

var param = {
    'enddate':context.request.parameters.enddate
}

var raw_date = new Date(param.enddate);
var raw_date_toISO = raw_date.toISOString();
var formated_date = format.format({
    value: raw_date_toISO,
    type: format.Type.DATE
});
var enddate_filter = search.createFilter({
    name: 'shipdate',
    operator: search.Operator.ON,
    values: formated_date
});
filters.push(enddate_filter);
and the client side looks like this:
Copy code
/*client script*/
function onDemandSearch(){
    var enddate = curRec.getValue({
        fieldId: 'custpage_enddate'
    });

    document.location = url.resolveScript({
        scriptId: getParameterFromURL('script'),
        deploymentId: getParameterFromURL('deploy'),
        params: {
            'enddate': enddate
        }
    });
}
function getParameterFromURL(param) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == param) {
            return decodeURIComponent(pair[1]);
        }
    }
    return (false);
}
b
enddate is a Date
when you use it as a pamater to url.resolveScript, netsuite will use .toString on it
that is much harder to use
instead, convert enddate to a iso 8601 string or a unix epoch and then use it
its important to know the difference between a string and a Date
k
I updated the client script function to this:
Copy code
function onDemandSearch(){
    var enddate = curRec.getValue({
        fieldId: 'custpage_enddate'
    });
    var enddate_raw = new Date(enddate);
    var enddateToISO = enddate_raw.toISOString();

    console.log(enddate),
    console.log(enddateToISO);
    var enddate
    document.location = url.resolveScript({
        scriptId: getParameterFromURL('script'),
        deploymentId: getParameterFromURL('deploy'),
        params: {
            'enddate': enddateToISO
        }
    });
}
although the console logs out the date in different formats, when its pushed to the criteria its is still a day off
message has been deleted
b
those should still represent the same Date
different formats, (and timezones)
k
I just noticed that the date is decreased by a day after format.format
b
what timezone is your user in netsuite using?
k
Central TimeZone
b
'Wed Aug 05 2021 000000 GMT-0400 (Eastern Daylight Time)' should be equivalent to 'Wed Aug 04 2021 230000 GMT-0500 (Central Daylight Time)'
the problem here being that the date in eastern is 1 day in the future compared to central
k
ahh I see
is it possible to override the timezone difference and just use whatever date is sourced?
b
use getText instead of getValue
should work in this particular instance since the timezone and date settings from the user should stay the same
k
do I need to format enddate before passing on as context parameter?
ahh that worked!
got it as Text, resolved url, extracted and converted it to Date, formatted it using format.format and passed it to the filters. Thank you so much for your help! 🙂
b
probably too many steps
you can probably go straight to the search filter now from the url parameters
your string now starts as a netsuite formatted date string
k
yeah actually that worked! 😄
179 Views