运行 SuiteScripts 在导入的 Web 服务事务上的推荐方法是什么?
What is the recommended method for running SuiteScripts on imported Web Services transactions?
我目前 运行 一个用户事件脚本,它带有一个提交前功能,可以为每笔交易填充自定义列字段。 因为它是一个用户事件脚本,所以它不会触发通过 Web 服务导入的交易,例如 POS 发票。
编辑: 根据下面的评论,这是我的误解。 Web 服务上的用户事件脚本 do 运行。
如果我想修改每个导入交易的自定义字段,最好使用哪种脚本?在记录提交后触发的工作流操作脚本会起作用吗?如果我一次导入多个交易,会不会有问题?
如果在导入交易时没有好的方法来执行此操作,我可能会 运行 计划或 Map/Reduce 脚本来在事后更新这些记录。
下面是UE脚本。它查看交易中的每一行,从底部开始,将折扣信息添加到它上面的行(如果适用)。
/**
*@NApiVersion 2.0
*@NScriptType UserEventScript
*/
define([], function() {
return {
beforeSubmit : function(context) {
// get record and line count
var currentRecord = context.newRecord;
var count = currentRecord.getLineCount({
sublistId:'item'
});
// init discount amount
var discountAmount = 0;
// for each line, starting from the bottom
for(var i = (count-1); i >= 0; i--) {
// fetch data
var type = currentRecord.getSublistValue({
sublistId: 'item',
fieldId: 'itemtype',
line: i
});
var amount = currentRecord.getSublistValue({
sublistId: 'item',
fieldId: 'amount',
line: i
});
var quantity = currentRecord.getSublistValue({
sublistId: 'item',
fieldId: 'quantity',
line: i
});
if (type == 'Discount') {
// add to current discount amount if discount
discountAmount += amount;
} else {
// parse data
amount = parseFloat(amount);
quantity = parseInt(quantity);
discountAmount = parseFloat(discountAmount);
// set variables
calculatedAmount = (amount + discountAmount).toFixed(2);
calculatedRate = ((amount + discountAmount) / quantity).toFixed(2);
calculatedDiscount = (discountAmount).toFixed(2);
// update sublist
currentRecord.setSublistValue({
sublistId: 'item',
fieldId: 'custcol_calculated_amount',
line: i,
value: calculatedAmount
});
currentRecord.setSublistValue({
sublistId: 'item',
fieldId: 'custcol_calculated_rate',
line: i,
value: calculatedRate
});
currentRecord.setSublistValue({
sublistId: 'item',
fieldId: 'custcol_calculated_discount',
line: i,
value: calculatedDiscount
});
log.debug('discountAmount', discountAmount);
// reset current discount amount
discountAmount = 0;
}
}
}
}
})
Because it's a User Event script, it doesn't fire on transactions imported through Web Services
这不是它的工作原理。您上面的假设可能是基于令人困惑的脚本类型名称做出的,该名称可能会被意外识别,因为它仅在 User Interface 由 User[=21] 创建记录时触发=].但是,实际上,文档概述了用户事件脚本在以下时间触发:
...The client request can come from the user interface, SOAP web services, server–side SuiteScript calls, CSV imports, or XML. ....
因此,您的脚本中可能有一些东西只是过滤掉来自 Web 服务的请求。您能否分享代码以获取更多详细信息?
我目前 运行 一个用户事件脚本,它带有一个提交前功能,可以为每笔交易填充自定义列字段。 因为它是一个用户事件脚本,所以它不会触发通过 Web 服务导入的交易,例如 POS 发票。
编辑: 根据下面的评论,这是我的误解。 Web 服务上的用户事件脚本 do 运行。
如果我想修改每个导入交易的自定义字段,最好使用哪种脚本?在记录提交后触发的工作流操作脚本会起作用吗?如果我一次导入多个交易,会不会有问题?
如果在导入交易时没有好的方法来执行此操作,我可能会 运行 计划或 Map/Reduce 脚本来在事后更新这些记录。
下面是UE脚本。它查看交易中的每一行,从底部开始,将折扣信息添加到它上面的行(如果适用)。
/**
*@NApiVersion 2.0
*@NScriptType UserEventScript
*/
define([], function() {
return {
beforeSubmit : function(context) {
// get record and line count
var currentRecord = context.newRecord;
var count = currentRecord.getLineCount({
sublistId:'item'
});
// init discount amount
var discountAmount = 0;
// for each line, starting from the bottom
for(var i = (count-1); i >= 0; i--) {
// fetch data
var type = currentRecord.getSublistValue({
sublistId: 'item',
fieldId: 'itemtype',
line: i
});
var amount = currentRecord.getSublistValue({
sublistId: 'item',
fieldId: 'amount',
line: i
});
var quantity = currentRecord.getSublistValue({
sublistId: 'item',
fieldId: 'quantity',
line: i
});
if (type == 'Discount') {
// add to current discount amount if discount
discountAmount += amount;
} else {
// parse data
amount = parseFloat(amount);
quantity = parseInt(quantity);
discountAmount = parseFloat(discountAmount);
// set variables
calculatedAmount = (amount + discountAmount).toFixed(2);
calculatedRate = ((amount + discountAmount) / quantity).toFixed(2);
calculatedDiscount = (discountAmount).toFixed(2);
// update sublist
currentRecord.setSublistValue({
sublistId: 'item',
fieldId: 'custcol_calculated_amount',
line: i,
value: calculatedAmount
});
currentRecord.setSublistValue({
sublistId: 'item',
fieldId: 'custcol_calculated_rate',
line: i,
value: calculatedRate
});
currentRecord.setSublistValue({
sublistId: 'item',
fieldId: 'custcol_calculated_discount',
line: i,
value: calculatedDiscount
});
log.debug('discountAmount', discountAmount);
// reset current discount amount
discountAmount = 0;
}
}
}
}
})
Because it's a User Event script, it doesn't fire on transactions imported through Web Services
这不是它的工作原理。您上面的假设可能是基于令人困惑的脚本类型名称做出的,该名称可能会被意外识别,因为它仅在 User Interface 由 User[=21] 创建记录时触发=].但是,实际上,文档概述了用户事件脚本在以下时间触发:
...The client request can come from the user interface, SOAP web services, server–side SuiteScript calls, CSV imports, or XML. ....
因此,您的脚本中可能有一些东西只是过滤掉来自 Web 服务的请求。您能否分享代码以获取更多详细信息?