问题描述
是否可以使用商品的外部ID而不是使用Restlet的内部ID创建发票?我可以这样在邮递员中完成此任务:
"item": {
"items": [
{
"amount": 120.0,"item": {
"externalid": "12878"
},"taxCode": {
"id": "13"
}
}
]
}
但是当我尝试在Restlet中设置相同时,会引发错误。
var currentItem = items[i];
record.selectNewLineItem('item');
record.setCurrentLineItemValue('item','externalid',currentItem["exId"]);
record.setCurrentLineItemValue('item','quantity',currentItem["quantity"]);
record.setCurrentLineItemValue('item','rate',currentItem["rate"]);
record.setCurrentLineItemValue('item','taxcode',currentItem["taxcode"]);
record.commitLineItem('item');
{"error":{"code":"INVALID_KEY_OR_REF","message":"Invalid item reference key 12878 for subsidiary 1."}}
解决方法
您需要做的是通过外部ID搜索项目。
var extIds = items.map(function(it){ return it.exId});
var items = nlapiSearchRecord('item',null,[
new nlobjSearchFilter('externalid','anyof',extIds)
],[
new nlobjSearchColumn('externalid')
]);
// check for null return and throw error
var extIdMap = {};
items.forEach(function(ref){
extIdMap[ref.getValue('externalid')] = ref.getId();
});
//then instead of record.setCurrentLineItemValue('item','externalid',currentItem["exId"]);
record.setCurrentLineItemValue('item','item',extIdMap[currentItem["exId"]]);