如标题所示,我正在尝试创建一个工作流程操作脚本,以通过创建时的自定义记录类型来设置销售订单上的地址。
所以本质上,我已经准备好以下几部分:
本质上,我有一个搜索,它运行并从自定义记录类型中提取值,获取地址的子记录值,然后尝试设置它们并提交该行。该脚本一直运行,直到行提交,我收到以下错误:“子记录行已被提交或取消。先前的子记录引用不再有效。您必须获取对该子记录的另一个引用才能执行这次操作。”
我有以下代码:
/**
* @NApiVersion 2.x
* @NScriptType WorkflowActionScript
*/
define(['N/record', 'N/search'], function(record, search) {
function onAction(context) {
var salesOrder = context.newRecord;
isDynamic: true
log.debug({
title: 'Start Script'
});
// Get the value of the custom field on the Sales Order
var warehouseCode = salesOrder.getValue({ fieldId: 'custbody_nsc_amz_warehouse_cod' });
log.debug({
title: 'Custom Field Value',
details: warehouseCode
});
if (warehouseCode) {
// Search for the custom record with the matching name
var warehouseSearch = search.create({
type: 'customrecord_amazon_warehouse_codes',
filters: [
['name', 'is', warehouseCode]
],
columns: [
'custrecord_amz_addr1',
'custrecord_amz_addr2',
'custrecord_amz_city',
'custrecord_amz_state',
'custrecord_amz_zip',
'custrecord_amz_country'
]
});
var searchResult = warehouseSearch.run().getRange({ start: 0, end: 1 });
if (searchResult && searchResult.length > 0) {
var result = searchResult[0];
log.debug({
title: 'Search Results Found',
details: result
});
// Set the shipping address fields on the Sales Order
var shipaddr = salesOrder.getSubrecord({fieldId: 'shippingaddress'});
log.debug({
title: 'Shipping Address',
details: shipaddr
});
salesOrder.setValue({
fieldId: 'shipaddresslist',
value: null // To override default address
});
log.debug({
title: 'Unset Shipping Address List',
details: 'Address should be unset'
});
// Set country field first when using dynamic mode
shipaddr.setValue({
fieldId: 'country',
value: result.getValue('custrecord_amz_country') || 'US' // Default to 'US' if not available
});
shipaddr.setValue({
fieldId: 'addr1',
value: result.getValue('custrecord_amz_addr1')
});
shipaddr.setValue({
fieldId: 'addr2',
value: result.getValue('custrecord_amz_addr2')
});
shipaddr.setValue({
fieldId: 'city',
value: result.getValue('custrecord_amz_city')
});
shipaddr.setValue({
fieldId: 'state',
value: result.getValue('custrecord_amz_state')
});
shipaddr.setValue({
fieldId: 'zip',
value: result.getValue('custrecord_amz_zip')
});
log.debug({
title: 'Address Line Line Fields Complete',
details: 'Address lines have been set'
});
salesOrder.commitLine({ sublistId: 'shippingaddress' });
log.debug({
title: 'Shipping Address Set Successfully'
});
} else {
log.debug({
title: 'No Matching Warehouse Record Found'
});
}
} else {
log.debug({
title: 'No Warehouse Code Specified'
});
}
}
return {
onAction: onAction
};
});
对此有任何帮助,我们将不胜感激。
您不需要
commit
,这已在 SS2 中删除。
考虑到您的代码顺序,您可能正在更新错误的地址或至少丢弃您的编辑。 改成这样:
salesOrder.setValue({
fieldId: 'shipaddresslist',
value: null // To override default address
});
// Set the shipping address fields on the Sales Order
var shipaddr = salesOrder.getSubrecord({fieldId: 'shippingaddress'});
注意 事实上,您可能不需要清除shipaddresslist,因为我相信更新地址子记录的过程现在会在交易上创建一个自定义地址。我知道我必须从几个实时脚本中删除清除过程。