How to set date/time field in netsuite with suitescript - javascript

var curRate = nlapiCreateRecord("customrecord_currency_exchange_rates",{recordmode: 'dynamic'});
serverDt = "09/25/2013 06:00:01 am"
var timezone = compConf.getFieldText('timezone');
curRate.setDateTimeValue('custrecord_effective_date' ,serverDt ,timezone);
nlapiSubmitRecord(curRate);
Hello I try to set custrecord_effective_date field which is a date/time type. But after the execution it gives an error below.
How can I set this field?
thanks for help.
INVALID_FLD_VALUE You have entered an Invalid Field Value 09/25/2013 06:00:01 am for the following field: custrecord_effective_date

Although this is an old post and the original question is about SuiteScript 1.0, I came across this page looking for the same INVALID_FLD_VALUE error with the date/time field but in SuiteScript 2.0, and since the solution ended up being different I wanted to share it for future readers.
In SuiteScript 2.0, setting a date/time field simply requires a JavaScript date object. Below is an example within a before submit user event context.
context.newRecord.setValue({
fieldId : 'custrecord_myfield',
value : new Date()
});

For setting Date/Time Field values using SuiteScript 2.O.
First Use 'N/format' module and 'N/Record' Module.
Create a normal Javascript Date Object.
Format the Date Object to DateTime Object.
And use Record.setValue() method for setting the datetime value.
Example:
var d = new Date();
var formattedDateString = format.format({
value: d,
type: format.Type.DATETIMETZ
});
record.setValue('yourDateTimeFieldId',formattedDateString );

For anyone that may be stuck with a SuiteScript 1.0 script trying to populate a date/time field and the existing solutions don't work for you, you can try using NetSuites nlapiDateToString method. Apparently the date/time field wants a string.
Example:
var record = nlapiCreateRecord('your record type');
record .setFieldValue('your date field id', nlapiDateToString(new Date(), 'datetimetz'));
nlapiSubmitRecord(record, false, true);
The SuiteScript 1.0 reference for more detail:

Hi thanks for answers after posting the question i checked the Form of record type and when i checked for the field 'custrecord_effective_date' I noticed that the date format must be 'DD/MM/YYYY HH:MM:SS' so when i changed it worked.

If you look at the NetSuite WSDL file, you will find the dateTime declaration:
<xs:simpleType name="dateTime" id="dateTime">
<xs:documentation
source="http://www.w3.org/TR/xmlschema-2/#dateTime"/>
Go to the URL and look at the source:
3.2.7 dateTime
[Definition:] dateTime values may be viewed as objects with integer-valued year, month, day, hour and minute properties, a decimal-valued second property, and a boolean timezoned property. Each such object also has one decimal-valued method or computed property, timeOnTimeline, whose value is always a decimal number; the values are dimensioned in seconds, the integer 0 is 0001-01-01T00:00:00 ...
So the format you are looking for is:
2013-09-25T06:00:01
I just pushed a dateTime to NetSuite and it worked.

Import N/format to your script and use one of the date format types:
DATE
DATETIME
DATETIMETZ
var parsedDate = format.parse({
value: '23/12/2010',
type: format.Type.DATE
});

The code looks ok
Are there any restrictions on the date field such that you can't set a date in the past?
Also your serverDt is the same value as the Netsuite help. Did it copy cleanly? What happens if you cut that string and type it directly?
And finally are you sure you have a datetime field and not a date field. If I apply your code to a date field I get the same error.

Little addition on the existing ans.
Error type -
type: "error.SuiteScriptError",
name: "INVALID_FLD_VALUE",
message: "You have entered an Invalid Field Value 2017-07-13T08:21:12.411Z for the following field: custrecord_lastrundate",
The value need to be set as a java script date() object. Even the simple same string of js date will throw error. If you are using moment then below code will work perfectly. Sample for adding next days.
var now = moment();
now.add(1,'days')
fetchRecord.setValue({
fieldId : 'custrecord_lastrundate' ,
value : new Date(now),
ignoreFieldChange : true
});

This is related and the other 2.0 answers are correct for Date/Time fields. However I was trying to set the incident time on the case record which is just the time field, and you have to do something like this:
let incidentTime = new Date(0, 0, 0, incidentDate.getHours(), incidentDate.getMinutes());
Notice - its still a Date object, but the Y, M, D values have to be zeroed for it to work.
Ahh Netsuite - you couldn't just do that behind the scenes yourself..
Please let me know if someone has a different solution.

Related

Mongoose Change Schema Field Type

How to change the type of one field in Mongoose?
For example:
var FooSchema = new Schema({
fooDate: {
type: String,
unique: true,
required: true,
trim: true
}
}
fooDate is a string type, I want to change it to Date type?
If I just change it, what will happen to the existing data? What's the proper way to migrate the old data?
Thanks.
Well it would depend on how do you have your date stored as string. Is it a valid date? Lets assume that it is either in valid ISO format or something like this:
"2019-03-20" or "2019-03-20T09:15:37.220Z"
If you simply change from String to Date mongoose would convert that for you and would treat that as the new type. So you would not need to migrate per se your date. When you get a model its fooDate would now be Date vs String before.
However the main issue you are facing now is querying data that is in two formats. Because any new records now would be saved as dates vs strings.
So you would be able to get by with the type change but while that would buy you some time you would need to migrate your data to reduce the friction during querying later. Migrating a single field would be a trivial migration script so there is nothing scary there.
PS. You can always test this btw. I just did with a simple schema and a saved record which had date as String then I updated the schema and did a simple Model.find to get that record and validate that now the date is an actual date and there was no issue with the model from mongoose. As long as the date string was in the correct date format.
You can change the data type any time .e.g
var FooSchema = new Schema({
fooDate: {
type: Date,
unique: true,
required: true,
trim: true
}
}
Now the new document records will be enter according to new data type. for existing or previous one it depends how you store a string you should make a script either in python or nodejs it depends upon you. first fetching the all records and make a helper function that take string and convert the string into data of that time you could easily do in javascript using Date Object as Date('2014-08-12')
Then you update all the previous records that you fetched in this way you can do consistency

AngularJS/HTML input week format error

I'm using AngularJS to for user to choose week from a input week tag, but there is something bizarre qbout the format.
I've read that the format should be yyyy-W##, and I do this already, but angularJS still gives me this error:
Error: [ngModel:datefmt] Expected `2015-W51` to be a date
http://errors.angularjs.org/1.4.8/ngModel/datefmt?p0=2015-W51
and this is my tag:
<input type="week" ng-if="header.type==='week'" ng-model="entry[header.className]" ng-change="vos.fieldSelectionChanged(field_id,entry.record_id)"/>
So as you see, the input type is week, here, I'm using this tag inside of a ng-repeat, so I'm loading the data from the entry[] array, and the week loaded is 2015-W51.
So please tell me what I'm doing wrong , is there a best practice using this tag with AngularJS?
Thanks !
Edit 1 - more code
I've found this:
http://codepen.io/empirefox/pen/MYyoao
It demonstrates how to use input week with a date, but the problem is that I don't have the data as dates.
And at the right part of the page, you have this :
$scope.value = new Date(2013, 0, 3);
Change it to this :
$scope.value = "2015-W20";
And it gives me the same error. So if we can solve this one, I think we can also solve the problem on my page.
Angular uses native Date object to handle all its datetime-related inputs, but unfortunately Date instances does not have any methods to handle week numbers. It would be the best solution to stick with moment.js.
Coercion from string to ng-model-enabled value (in controller etc.):
$scope.value = moment("2015-W20").toDate();
Coercion from ng-model-enabled value to string (in form submit handler etc.):
moment($scope.value).format("YYYY-[W]WW"); // returns "2015-W20"

Using same parsing format for output format

With libary moment there is option to bring a array of formating options and momentjs use the best match for parsing the input.
For Example:
var date = moment("29-06-1995", ["MM-DD-YYYY", "DD-MM", "DD-MM-YYYY"]);
but what if I want the take the same format that using in parsing for output formating.
var dateText = date.format('selected parse')
How do I know which format moment choose to use?
Currently there is no exposed function for getting the chosen format, however there is a "private" field named _f that contains this information.
var m = moment("29-06-1995", ["MM-DD-YYYY", "DD-MM", "DD-MM-YYYY"]);
m._f // "DD-MM-YYYY"
If you use this, be careful when updating versions of moment. Private fields are not guaranteed to be maintained, and could break between versions.
I've logged this as a feature request for future moment.js functionality.

JavaScript to update End Date when Start Date is changed

For the site I am developing, I use a form to update the different fields of a model.
Some of those fields need to be updated according to the values of others.
For example, each instance of a Task, has a start date, an end date, and a length which have to verify the following : End = Start + Length (where weekends are not counted, but that is a secondary issue).
So one of the functions I am trying to write would do :
When the user changes the value of the input Start_date
Get that date from the input field
Get the value of the field Length
Update the value of the input "End date" with (Start_date + Length)
The three inputs concerned by my example have the following IDs : id_start, id_length, id_finish.
Here is what I have written, but which doesn't work at all (nothing visible happens...) :
<script>
$( "#id_start" ).change(function() {
var start = new Date();
var finish = new Date();
var numberOfDays = document.getElementById('id_length').value;
start = document.getElementById('id_start').value;
finish.setdate(start.getDate()+document.getElementById('id_length'))
$('#id_finish').val(finish);
});
</script>
Any hint at a solution to make it work would be hugely appreciated.
Let's look at what your code is actually doing.
var start = new Date();
Here you create a new Date object.
start = document.getElementById('id_start').value;
Here, you change the value of start to the value of the DOM element with the id = 'id_start'. The problem is, this value isn't a Date object. Even the new HTML Date input type (which isn't supported by Firefox or IE) will only give you a string when you pull it's value. So you've created a new Date object, and then you throw it out immediately.
So now start isn't a Date, but a string. In this line:
finish.setdate(start.getDate()+document.getElementById('id_lenght'))
You're attempting to call the undefined .getDate() on a string object, so you'll get an error.
Here's what you need to do. That string (assuming it's in ISO format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS) can be used to create a new Date.
start = new Date(document.getElementById('id_start').value);
Now start is actually a date object, and you can add the length in days to get your end date.
Of course, you'll want to make sure the input is in an acceptable format! There's a lot of 'date picker' options (jQueryUi, for example, has a datepicker), and if you're only interested in supporting Chrome, the works well.

DateTime from Javascript post to Controller in ASP.NET results in null (01/01/0001)

This is driving me crazy, similar to this. If I use a standard form and no javascript the controller correctly binds the datetime. However when I'm posting from a form it always binds as null:
"MyObject.Name": "Test name",
"MyObject.Date": "5/1/2001"
I've tried a couple of variations, 5-1-2001, etc. but cannot seem to get it to take. I can confirm that it is being passed to the server as it shows up in the Request.Form string. My culture is Gregorian and I've set:
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
In Application_BeginRequest(). What gives?
Did you try using leading zeros, like "05/01/2001"?
The invariant culture's standard date formats include "MM/dd/yyyy" but not "M/d/yyyy", so if it works, that's why. Ideally you would use one of the non-culture-specific formats like the 'O' roundtrip pattern. Then it wouldn't matter what culture you were using on the server:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx#Roundtrip
Could it be a localization issue? This blog post describes a common localization pitfall with DateTime.
DateTime is value type and not reference type and it takes DateTime.MinValue if you don't initialize it.
you have to declare it as:
DateTime? dt;
Or
Nullable<DateTime> dt;
DateTime? is shorthand for Nullable<DateTime>

Categories