简体   繁体   English

如何使用 suitescript 在.netsuite 中设置日期/时间字段

[英]How to set date/time field in netsuite with suitescript

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.您好,我尝试设置日期/时间类型的 custrecord_effective_date 字段。 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 INVALID_FLD_VALUE 您为以下字段输入了无效字段值 09/25/2013 06:00:01 am: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.虽然这是一篇旧帖子,并且最初的问题是关于 SuiteScript 1.0,但我遇到了这个页面,寻找与日期/时间字段相同的 INVALID_FLD_VALUE 错误,但在 SuiteScript 2.0 中,并且由于解决方案最终不同,我想分享它对于未来的读者。

In SuiteScript 2.0, setting a date/time field simply requires a JavaScript date object.在 SuiteScript 2.0 中,设置日期/时间字段只需要一个 JavaScript 日期对象。 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.用于使用 SuiteScript 2.O 设置日期/时间字段值。

  1. First Use 'N/format' module and 'N/Record' Module.首先使用“N/format”模块和“N/Record”模块。

  2. Create a normal Javascript Date Object.创建一个普通的 Javascript 日期对象。

  3. Format the Date Object to DateTime Object.将日期对象格式化为日期时间对象。

  4. And use Record.setValue() method for setting the datetime value.并使用 Record.setValue() 方法设置日期时间值。

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.对于任何可能被 SuiteScript 1.0 脚本试图填充日期/时间字段而现有解决方案不适合您的人,您可以尝试使用 NetSuites nlapiDateToString方法。 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:有关更多详细信息,请参阅 SuiteScript 1.0 参考:

在此处输入图片说明

嗨,感谢您在发布问题后回答我检查了记录类型的表格,当我检查字段“custrecord_effective_date”时,我注意到日期格式必须是“DD/MM/YYYY HH:MM:SS”,所以当我更改它时工作。

If you look at the NetSuite WSDL file, you will find the dateTime declaration:如果您查看 NetSuite WSDL 文件,您会发现 dateTime 声明:

<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:转到 URL 并查看源:

3.2.7 dateTime 3.2.7 日期时间

[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. [定义:] dateTime 值可以被视为具有整数值的年、月、日、小时和分钟属性、十进制值的秒属性和布尔时区属性的对象。 Each such object also has one decimal-valued method or computed property, timeOnTimeline, whose value is always a decimal number;每个这样的对象还有一个十进制值的方法或计算属性,timeOnTimeline,它的值总是一个十进制数; the values are dimensioned in seconds, the integer 0 is 0001-01-01T00:00:00 ...这些值以秒为单位,整数 0 是 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.我只是将 dateTime 推送到 NetSuite 并且它起作用了。

Import N/format to your script and use one of the date format types:将 N/format 导入您的脚本并使用其中一种日期格式类型:

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.此外,您的 serverDt 与 Netsuite 帮助的值相同。 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.对现有 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.该值需要设置为 java 脚本 date() 对象。 Even the simple same string of js date will throw error.即使是简单的 js 日期字符串也会抛出错误。 If you are using moment then below code will work perfectly.如果您正在使用 moment,那么下面的代码将完美运行。 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.这是相关的,其他 2.0 答案对于日期/时间字段是正确的。 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.注意 - 它仍然是一个日期 object,但 Y、M、D 值必须归零才能工作。

Ahh Netsuite - you couldn't just do that behind the scenes yourself..啊 Netsuite - 你不能自己在幕后做......

Please let me know if someone has a different solution.如果有人有不同的解决方案,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM