Trying to load a search and add filter with OR but...
# suitescript
n
Trying to load a search and add filter with OR but getting the following error: • Cannot find function _clone in object AND.
Copy code
var childCustomerSearch = search.load({
						id: 'customsearch_kescp_so_child_customer_sea'
					});

					if (childCustomerSearch.filters.length > 0) {
						childCustomerSearch.filters.push('AND');
						childCustomerSearch.filters.push([["internalid","anyof","578029"],"OR",["parentcustomer.internalid","anyof","578029"]]);
					} else {
						childCustomerSearch.filters.push([["internalid","anyof","578029"],"OR",["parentcustomer.internalid","anyof","578029"]]);
					}
s
You need to actually use the search.createFilter call when pushing new filters into a loaded search.
b
use Search.filterExpression for filter expressions
n
So would I do search.createFilter then search.createFilterExpression then search.createFilter?? @battk @Sandii
b
There is a difference between a filter and a filter expression
s
My guess would be you need to rebuild the filters by reading from is already there, adding your stuff, and set the
search.filterExpression
property, there is no such thing as search.createFilterExpression. I did not not fully read what you were doing, you can ignore my original comment unless building simple filters that are all
AND
b
Learn the difference and pick one or the other
n
Well I need to add two filters (the internalid or parentcustomer.internal) in my suitlet and allow a user to add more filters via the UI in netsuite.
s
Right, so load the search, get the filters from the search, add your stuff, and set the search.filterExpression. You cannot just push stuff into search.filters when you want it to be a filter expression.
n
@Sandii @battk I got it mostly working. What I was trying to do now was get the current filters and add them in. However, when I did this:
for (var i = 0; i < customSearchFilters.length; i++) {
                        
newFilters.push([customSearchFilters[i].name, customSearchFilters[i].operator, customSearchFilters[i].values])
                    
}
It returned Null for values. But this is what the filters logged: [{"name":"custentity_customer_ship_to","operator":"anyof","values":["1"],"isor":false,"isnot":false,"leftparens":0,"rightparens":0}]
s
I do not see a search.createFilter call or anything setting search.filterExpression (I know its only a snippet), so it's hard to tell if you are doing anything much diffrent than originally.
n
var childCustomerSearch = search.load({
                        
id: 'customsearch_kescp_so_child_customer_sea'
                    
});
                    
var customSearchFilters = childCustomerSearch.filters;
                    
var newFilters = [];
                    
log.debug({'title':'customSearchFilters', 'details': customSearchFilters});
                    
for (var i = 0; i < customSearchFilters.length; i++) {
                        
newFilters.push([customSearchFilters[i].name, customSearchFilters[i].operator, customSearchFilters[i].values])
                    
}
                    
if (newFilters.length > 0) {
                        
newFilters.push('AND');
                        
newFilters.push([["internalid","anyof", cacheObj.companyid],"OR",["parentcustomer.internalid","anyof", cacheObj.companyid]]);
                    
} else {
                        
newFilters.push([["internalid","anyof", cacheObj.companyid],"OR",["parentcustomer.internalid","anyof", cacheObj.companyid]]);
                    
}
                    
childCustomerSearch.filterExpression = newFilters;
s
Use 3 ticks for a code block, instead of single tick
n
Copy code
var childCustomerSearch = search.load({
						id: 'customsearch_kescp_so_child_customer_sea'
					});
					var customSearchFilters = childCustomerSearch.filters;

					var newFilters = [];
					log.debug({'title':'customSearchFilters', 'details': customSearchFilters});

					for (var i = 0; i < customSearchFilters.length; i++) {
						newFilters.push([customSearchFilters[i].name, customSearchFilters[i].operator, customSearchFilters[i].values])
					}
					
					if (newFilters.length > 0) {
						newFilters.push('AND');
						newFilters.push([["internalid","anyof", cacheObj.companyid],"OR",["parentcustomer.internalid","anyof", cacheObj.companyid]]);
					} else {
						newFilters.push([["internalid","anyof", cacheObj.companyid],"OR",["parentcustomer.internalid","anyof", cacheObj.companyid]]);
					}
					childCustomerSearch.filterExpression = newFilters;
s
not sure if causing the problem, but the for loop is missing
AND
in between each of them if there are more than one
n
Well, I tried just logging customSearchFilters[i].values in teh for loop and it returned nothing
s
log
newFilters
before you set it as the filterExpression and make sure the array makes sense would be where I would start.
n
This is what the newFilters outputs at the end:
Copy code
[["custentity_customer_ship_to","anyof",null],"AND",[["internalid","anyof","578029"],"OR",["parentcustomer.internalid","anyof","578029"]]]
s
yeah that null is likely the problem? the rest looks built properly
n
I just can't understand why I am getting null when that filter has a value for the values key
s
try
_rawFilters
instead of
filters
I dont see a
values
property associated with
.filters
when I am loading a search in the console
n
like childCustomerSearch._rawFilters or remove the period
s
Change
var customSearchFilters = childCustomerSearch.filters;
to
._rawFilters
, or just read directly from the original
.filterExpression
and modify that.
Copy code
var searchObj = search.load(); //load search
var newFilters = searchObj.filterExpression; //comes back as array
newFilters.push(
    'AND',
    [[], 'OR', []]//your new stuff here
)
searchObj.filterExpression = newFilters;
something like this should work
🍻 1
n
@Sandii Thanks!! this last comment of yours worked, thanks!
r
@Sandii Having all the messages available is super handy, that last piece of code you posted was very helpful, thanks!