How to set time in Netsuite using suitescript 2.0? - netsuite

I can set time for timeofday type fields both UI/API.
I can set time for the time type field INITIAL TIME BUDGET as 16:55 using colon(:) separator in UI for Task.
I could not set time for that field as below using suitescript,
function load(recordType, id) {
return record.load({
type: recordType,
id: id
});
}
var recordType = "task"
var id = "123"
var objectRecord = load(recordType, id);
objectRecord.setValue("estimatedtime", "16:55");
var updatedId = objectRecord.save({});
I get this error
You have entered an Invalid Field Value 16:55 for the following field: estimatedtime
I tried the following cases,
"16:55" - Invalid Field Value
16:55 - UNEXPECTED_ERROR
16.55 - No error, but set as "estimatedtime":"16:33"
How to set time for time type field?

You need to create a Date object. For example, say you pass "13:00", you'll need to do something like:
dateStr = "13:00";
d = new Date();
dateParts = dateStr.split(":");
d.setHours(+dateParts[0]);
d.setMinutes(+dateParts[1]);
And then:
objectRecord.setValue("estimatedtime", d);

you have to set only hour value.
Format has to 24 hours.
objectRecord.setValue("estimatedtime", 23);

Related

Error message "cannot find function getFullYear(...)" when entering date and trying to save the record

We are trying in a RESTLet to access the sublist "demandplandetail" from a NetSuite Item Demand Plan. Everything goes fine until a certain point. We are able to load it and process the demandplan for 2020. However, here it gets frustrating.
We know (can see from NetSuite) that there is data also for 2021. However, to access that from SuiteScript seems very difficult.
1st solution) The item demand plan has the field "year". OK, just set that to 2021, save and reload the record. Result: saving ignored, year still is 2020.
2nd solution) Set the year using a Date object as in:
var demandPlan = record.load(...)
var d = new Date();
demandPlan.setValue({
fieldId: 'year',
value: d
});
Gives the following:
:"TypeError: Cannot find function getFullYear in object NaN. (NLRecordScripting.scriptInit$lib#59)","stack":["setDatesForMonthAndYear(NLRecordScripting.scriptInit:108)","anonymous(N/serverRecordService)"
on saving the record. I also get the same using (various) strings adhering to acceptable date formats (as in '1/1/2021'). I have also tried the format package giving me a date string -> the same result.
Also read somewhere that you may need to set the start date (field 'startdate') in the record. Tried several variations but it stubbornly refuses :(.
Wonder if anyone has seen anything similar?
Best Regards,
Toni
Hi Please try the below code also check if you're passing date object to the field not the date string.
function formatDate() {
var dateROBD = format.parse({
value: new Date(),
type: format.Type.DATE
});
// this line optional if you want to try with or else ignore this
dateROBD = convertUTCDateToLocalDate(new Date(dateROBD));
return dateROBD;
}
function convertUTCDateToLocalDate(date) {
var newDate = new Date(date.getTime() + date.getTimezoneOffset() * 60 * 1000);
var offset = date.getTimezoneOffset() / 60;
var hours = date.getHours();
newDate.setHours(hours - offset);
return newDate;
}
OK, mystery solved. Turned out that this is not supported in SuiteScript 2.0 but you need to use 1.0.

How to set DateTime Field Value without TimeZone in XPages ServerSide JS

When I set a value of Date/Time field in XPages It's value is like below.
I would like to set value without TimeZone (in this samples without ZE3).
Field Name: dtField1
Data Type: Time/Date
17.11.2016 13:13:31 ZE3
I tried #Today() and these 2 lines code below but I could not be succeded. is there any way to do it?
var now:NotesDateTime = session.createDateTime(#Now());
document1.replaceItemValue("dtField1", now);
Regard
C.A.
Time zone is always part of NotesDateTime field if field contains date and time no matter how you created it.
If the field contains date only or time only then time zone is omitted.
Set date only with
var now:NotesDateTime = session.createDateTime(#Now());
now.setAnyTime();
document1.getDocument().replaceItemValue("dtField1", now);
and time only with
var now:NotesDateTime = session.createDateTime(#Now());
now.setAnyDate();
document1.getDocument().replaceItemValue("dtField1", now);

How to set timezone with moment?

i am using moment for getting server time .
moment.tz.setDefault("Asia/Kolkata");
var now = new Date();
var _p_date = moment.tz(now, zone).format();
time when inserting _p_date = 2016-01-05T18:32:00+05:30
But in database date variable is type of DATETIME. and time is saved as 2016-01-05 18:32:00.
and after that when i comparing with this to get time_ago funcionality. providing me wrong estimation.
using time ago = moment("2016-01-05T18:32:00.000Z").fromNow(); // is showing In 5 hours
Since your initial timezone is lost you have to create moment.tz object with selected timezone. Try this plunker
var date = moment.tz(moment("2016-01-05T18:32:00.000Z", "YYYY-MM-DDTHH:mm")
.format('YYYY-MM-DD HH:mm'), 'Asia/Kolkata');
console.log(date.fromNow());

Mongo Conditional Condition in Node

I want to be able to have an API that can filter based on a date. So you pass in something like the following:
dob: {
date: new Date("5/22/1955"),
filter: 'lt'
}
This should return all records with DOB less than the date, but if "gte" was passed in the filter, than it should return all records with DOB greater than or equal to the date.
Obviously, the Mongo queries would be
dobQuery = {dob: {gte: data.dob.date}};
dobQuery = {dob: {lt: data.dob.date}};
But how could I make this conditional? I want to write something like this, but of course this doesn't work:
dobQuery = {dob : {"$" + data.dob.filter: data.dob.date}};
How can I write that out?
Build dobQuery up programmatically using bracket notation:
dobQuery = {dob: {}};
dobQuery.dob['$' + data.dob.filter] = data.dob.date;

mutivalue date field search not working

I have a multivalue field called freeDaysPool which has multiple dates as strings. With the following code, the search does not return anything. If I leave that field out, the search works just fine with the two other fields. I read that I should use CONTAINS with multivalue fields but then I got query not understandable.
I've tried the back-end field as a date field and as a text field and tested all kinds of query combinations and date formats but no luck. Any help is really appreciated.
This is the search button code:
var query = new Array("");
var cTerms = 0;
// Field 1
var search01 = getComponent("searchcustomReservationField01").getValue();
if (#Contains(#Text(search01),"any city")){"";}
else {query[cTerms++] = '[customReservationField01]="' + search01 +'"'};
// Field 2
var search02 = getComponent("searchcustomReservationField02").getValue();
if (#Contains(#Text(search02),"any city")){"";}
else {query[cTerms++] = '[customReservationField02]="' + search02 + '"'};
// Date 1
var formatter = new java.text.SimpleDateFormat("d.M.yyyy");
query[cTerms++] = 'FIELD freeDaysPool = ' + formatter.format(getComponent("searchcustomDateField01").getValue());
// if query is still empty, we fill it with asterisk
if(query == "" || query == null){
query[cTerms++] = "*";
}
// make all as string
qstring = query.join(" AND ").trim();
sessionScope.searchString = qstring;
It will return query as:
[customReservationField01]="Oslo" AND [customReservationField02]="Oslo" AND FIELD freeDaysPool = 6.2.2015
AFAIK date values in formulas (and a query is a formula) have to be noted like
[06.02.2015]
to compare them. Just try to use your formular in the Notes Client to do a fulltext search. If you get results and no errors you found the correct format. That's at least the way I test queries as I'm not able to remind the syntax for years :-D
Thank you for all the help! Seems that Domino keeps the field type as date field even if you change it back to text field (noticed that from the notes FTsearch). I created completely new text field and added the days as strings in dd.MM.yyyy format. I also search them as strings and it works fine.
The changed code bit now looks like this:
// Date 1
var formatter = new java.text.SimpleDateFormat("dd.MM.yyyy");
query[cTerms++] = '[freeDays] CONTAINS "' + formatter.format(getComponent("searchcustomDateField01").getValue())+'"';

Resources