In some scenarios, you may find a search does throw an Invalid Search Operator error but NetSuite executes the search and returns the result. You may just want to let it go as long as it works, but we can make it work flawlessly.

有时候视图会返回一个nlobjSearchFilter包含无效操作者,或语法不正确错误,但是视图还是正常返回了结果。你可能觉得能用就行,但是我们还是可以做更好一点。

//Code snippet example
let searchFilters = []; //start a search filters array
let possibleValues = []; //empty array, maybe due to neglect 
searchFilters.push(search.createFilter({
				name : "custrecord_somelistrecordfield",
				operator : 'anyof',
				values : possibleValues 
			}));
let columns = ["custrecord_field1", "custrecord_field2"]
let searchObj = search.create({
			type : 'customrecord_invoice',
			filters: searchFilters,
			columns : columns
		});
let resultSets = searchObj.run(); //see the error SSS_INVALID_SRCH_OPERATOR but still get result.

It’s very likely that an anyof operator is used but the value is an empty array.

In this case you verify the array before using it as a search filter. It depends on how the variable is initiated. Maybe try to exclude the empty/undefined before assume it’s an array and get the length

这种错误情况很可能来自你使用了anyof的搜索条件但是条件的值是空数组。

这种情况你应该使用之前验证一下数组,具体取决于数组的值是如何确定的,可能需要你多加一层考虑,排除未定义的情况,然后再验证数组的长度。

let searchFilters = []; //start a search filters array
let possibleValues;
//#1 messy solution, you know the data is undefined, an empty array, or an array with elements
if (possibleValues) {
   if (possibleValues.length > 0) {
      searchFilters.push(search.createFilter({
				name : "custrecord_somelistrecordfield",
				operator : 'anyof',
				values : possibleValues 
			}));
   }
}
//#2 better solution, it handles all possible situations
if (Array.isArray(possibleValues)) {
   if (possibleValues.length > 0) {
      searchFilters.push(search.createFilter({
				name : "custrecord_somelistrecordfield",
				operator : 'anyof',
				values : possibleValues 
			}));
   }
}
let columns = ["custrecord_field1", "custrecord_field2"]
let searchObj = search.create({
			type : 'customrecord_invoice',
			filters: searchFilters,
			columns : columns
		});
let resultSets = searchObj.run(); //error is gone.

Last modified: 15/04/2023

Author

Comments

Write a Reply or Comment

Your email address will not be published.