因为Netsuite的本地CPN不允许人们在CPN中使用空格,所以我的公司为CPN制作了一个定制的文本来使用自定义记录类型。下面的脚本用于交叉引用客户和项目,以生成一个可能的CPN列表,然后在该列表中选择第一个也是唯一的选项。起初,我们把它路由到了儿童公司,但现在我们认为,如果一家公司有12家儿童公司,我们只需上传一次CPN,那么让它们与母公司company.This连接可能是一个更好的主意。有人会检查下面的代码并让我知道为什么我不能让代码使用父客户而不是子客户吗?或者更确切地说,它根本不人口。
define(["N/search", "N/log"], function(Search, _Log) {
var TAG = "pi_cs_so_v2";
function Log() {};
Log.log = function (tag, msg) {
console.log(tag + " : " + msg);
};
Log.debug = function(tag, msg) {
_Log.debug(tag, msg);
Log.log(tag, msg);
};
Log.error = function(tag, msg) {
_Log.error(tag, msg);
Log.log(tag, msg);
};
function fieldChanged(context) {
PartNumber.fieldChanged(context);
}
/**
* Static object, contains customizations relevant to EURO-3
* @constructor
*/
function PartNumber () {}
function cusParent (){}
/**
* Handle the native field changed NetSuite call
* @param context
* @returns {boolean} True if CPN is updated, else false
*/
PartNumber.fieldChanged = function(context) {
var nr = context.currentRecord;
if (context.sublistId !== "item" || context.fieldId !== "item") return false;
Log.debug(TAG, "fieldChanged, executing CPN extension");
var item = nr.getCurrentSublistValue({sublistId: context.sublistId, fieldId: "item"});
var customer = nr.getValue("entity");
var parent = nr.getValue({
join: "entity",
fieldId: "parent",
name: "name"
});
Log.debug(TAG, "Item, customer: " + JSON.stringify([item, parent]));
if (!parent || parent === "" || !item || item === "") return false;
var cpn = PartNumber.find(parent, item);
if (!cpn) return false;
nr.setCurrentSublistValue({sublistId: context.sublistId, fieldId: "custcol_cpn_transaction", value: cpn});
nr.setCurrentSublistValue({sublistId: context.sublistId, fieldId: "custcol24", value: parent});
Log.debug(TAG, "Found CPN: " + cpn);
return true;
};
/**
* Search for the customer part number, assumes there is only ever a single result
* @param customer
* @param item
* @returns {number | undefined} InternalID of the True Customer Part Number record
*/
PartNumber.find = function(customer, item) {
var searchObj = Search.create({
type: "customrecord_true_cpn",
filters:
[
["custrecord_cpn_customer","anyof",customer],
"AND",
["custrecord_cpn_item","anyof",item]
],
columns: []
});
var ans = -1;
searchObj.run().each(function(result){
// .run().each has a limit of 4,000 results
ans = result.id;
return false;
});
return ans !== -1 ? ans : undefined;
};
return {
postSourcing: fieldChanged,
};
});发布于 2022-07-22 19:15:23
假设一家具有层次结构的公司可能有一棵树,而不仅仅是一条直线,那么您需要一种方法来有效地查询高层到当前客户的高效率,并获得最佳匹配的CPN。
我们可以利用Netsuite如何更改名称,并推断具有匹配CPN的最长、完全合格的客户名称是使用的最佳名称。
虽然下面的代码是未经测试的,但它是基于我在其他上下文中所做的分级搜索。请注意,我发现您的许多伪对象样式非常模糊,没有为代码的可读性或类型安全性添加任何内容。这只是一个独立的脚本。
define(["N/search", "N/log"], function(Search, _Log) {
var TAG = "pi_cs_so_v2";
function hasConsole(){
return typeof window == 'object' && window.console && window.console.log;
}
var Log = {
debug : function(tag, msg) {
hasConsole ? window.console.log(tag, msg) : _Log.debug(tag, msg);
},
error : function(tag, msg) {
hasConsole ? window.console.error(tag, msg) : _Log.error(tag, msg);
}
};
function fieldChanged(context) {
var nr = context.currentRecord;
if (context.sublistId !== "item" || context.fieldId !== "item") return; //return false <- fieldChanged is void
Log.debug(TAG, "fieldChanged, executing CPN extension");
var item = nr.getCurrentSublistValue({sublistId: context.sublistId, fieldId: "item"});
var customer = nr.getValue("entity");
if(!customer || !item) return;
//if (!parent || parent === "" || !item || item === "") return false; the === "" will never be evaluated
// var parent = nr.getValue({ // this call doesn't exist
// join: "entity",
// fieldId: "parent",
// name: "name" // where did you get this field id from?
// });
const custInfo = Search.lookupFields({
type:'customer',
id:customer,
columns:['internalid', 'entityid', 'parent']
});
// should have fully qualified customer name parent : sub-parent : customer
var cpn = findPartNumber(custInfo.entityid, item);
if (!cpn) return;
nr.setCurrentSublistValue({sublistId: context.sublistId, fieldId: "custcol_cpn_transaction", value: cpn.id});
nr.setCurrentSublistValue({sublistId: context.sublistId, fieldId: "custcol24", value: cpn.customer});
Log.debug(TAG, "Found CPN: " + JSON.stringify(cpn));
return; // field changed is void; no return value
}
/**
* Search for the customer part number, assumes there is only ever a single result
* @param customer
* @param item
* @returns {id: cpn record id, customer: customerId owning the cpn} | null
*/
function findPartNumber(custInfo, item) {
var cpnFilters = null;
var commonColumns = [
Search.createColumn({'name': 'entityid', join:'custrecord_cpn_customer'}),
Search.createColumn({'name': 'custrecord_cpn_customer'})
];
if(custInfo.parent && custInfo.parent.length){
cpnFilters = [
["custrecord_cpn_item","anyof",item], 'AND',
[
["custrecord_cpn_customer","anyof",custInfo.parent[0].value], 'OR', // the top level
getCustHierarcyClauses(custInfo)
]
];
}else{
cpnFilters = [
["custrecord_cpn_customer","anyof",custInfo.internalid],
"AND",
["custrecord_cpn_item","anyof",item]
];
}
var bestCPN = null;
Search.create({
type: "customrecord_true_cpn",
filters:cpnFilters,
columns: commonColumns
}).run().each(function(result){
if(!bestCPN) {
bestCPN = {
id:result.id,
entity: result.getValue({name:'entityid', join:'custrecord_cpn_customer'}),
customer:result.getValue({name:'custrecord_cpn_customer'})
};
} else{ // need to get the closest defined CPN; assumes lower level of company could have their own preferred CPN.
var testCPN = {
id: result.id,
entity: result.getValue({name:'entityid', join:'custrecord_cpn_customer'}),
customer:result.getValue({name:'custrecord_cpn_customer'})
};
if(testCPN.entity.length > bestCPN.entity.length) bestCPN = testCPN;
}
return true;
});
return bestCPN;
}
function getCustHierarcyClauses(custInfo){
var fullNames = custInfo.entityid.split(' : ').slice(0, -1); // last name is the direct company name and no inference needed
var filters = ["custrecord_cpn_customer","anyof",custInfo.internalid];
var topParentId = custInfo.parent[0].value;
if(fullNames.length == 1){ // shouldn't have gotten here if only 1
return filters;
}
for(var i = 1; i< fullNames.length; i++){
filters.push('OR', [
["custrecord_cpn_customer.parent","anyof",topParentId], 'AND',
["custrecord_cpn_customer.entityid","is",fullNames.slice(0,i).join(' : ')] // need to qualify by name because we only want direct line of hierarchy
]);
}
return filters;
}
return {
postSourcing: fieldChanged
};
});https://stackoverflow.com/questions/73069322
复制相似问题