完整脚本如下。
即使保存新创建的发票后,当我尝试检索它时,文档编号仍显示“待生成”。记录肯定保存了,因为我可以看到发票。
我尝试使用此代码的变体加载它:
var recordId = context.objRecord.id;
var newRecord = record.load({
type: record.Type.INVOICE,
id: recordId,
isDynamic: true,
});
但这会导致错误
无法从未定义中读取属性“id”
我很感激还有其他关于此问题的帖子,但是建议的解决方案都不起作用,而且 Oracle 文档在这方面非常有限,他们给出的所有示例都包括硬编码的 tranid。
/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
*/
define(['N/runtime', 'N/record', 'N/search'], function(runtime, record, search) {
return {
execute: function(context) {
var objRecord = record.create({
type: record.Type.INVOICE,
isDynamic: true,
defaultValues: {
entity: 3413
}
});
var mySearch = search.load({
id: 'customsearch2318'
});
var mySearchItemFilter = search.createFilter({
name: 'item',
operator: search.Operator.ISNOTEMPTY,
summary: search.Summary.GROUP
});
var mySearchItemFilter = search.createFilter({
name: 'quantity',
operator: search.Operator.ISNOTEMPTY,
summary: search.Summary.SUM
});
var mySearchItemFilter = search.createFilter({
name: 'cost',
operator: search.Operator.ISNOTEMPTY,
summary: search.Summary.MAX
});
mySearch.run().each(function(result) {
var item = result.getValue({
name: 'item',
summary: search.Summary.GROUP
});
var quantity = result.getValue({
name: 'quantity',
summary: search.Summary.SUM
});
var cost = result.getValue({
name: 'custcol_location_sales_cost',
summary: search.Summary.MAX
});
log.debug({
title: 'items',
details: item + " " + quantity + " " + cost
});
objRecord.selectNewLine({
sublistId: 'item'
});
objRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'item',
value: item
});
objRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: quantity
});
objRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'rate',
value: 1
});
objRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_location_sales_cost',
value: cost
});
objRecord.commitLine({
sublistId: 'item'
});
return true;
});
objRecord.setValue({
fieldId: 'location',
value: 1
});
objRecord.save()
var docNum = objRecord.getValue ({
fieldId: 'tranid'
})
log.debug({
title: 'New Invoice',
details: 'Record creation progress' + docNum
});
}
};
});
您正在混合内容,正如我所看到的,您在计划的脚本中使用上下文,就像您在用户事件中一样。
你一开始就是对的。
当您保存发票时,它会返回一个 ID 并保持旧状态而不引用。
您需要将发票 ID 保存在变量中,并通过重新加载或使用 search.lookupfield 来检索发票(更好,因为它消耗的资源更少)
示例:
let saved_inv = objRecord.save();
let saved_inv_tranid = record.load({type: "invoice", id: saved_inv}).getValue ({fieldId: 'tranid'})
或
let saved_inv = objRecord.save();
let saved_inv_tranid = search.lookupFields({
type: search.Type.INVOICE,
id: saved_inv,
columns: ['tranid']
})[0].tranid