Hi, can someone help me on search I have a functi...
# suitescript
t
Hi, can someone help me on search I have a function that load a saved search and doing fine. But whenever I call the first function and try to add another filter it shows error "Cannot read property 'push' of undefined. Here's my code
var activeContractsObj = *getListOfActiveContracts*(); // this is the function that calls the saved search
      
console.*log*('parameter:' + cfg.CONTRACT.SALES_ORDER_NAME + 'customer:' + customer);
      
var srchFilters = activeContractsObj.filters;
      
var customFilters = [];
      
customFilters = [cfg.CONTRACT.SALES_ORDER_NAME, search.Operator.ANYOF, customer];
      
srchFilters.*push*(customFilters);
      
activeContractsObj = srchFilters;
b
Last line of code is probably not what you want
t
ok ill try it @battk
Im still seeing error "Cannot read property 'push' of undefined"
I also tried this code _`activeContractsObj.filters.push(search.createFilter({ name: cfg.CONTRACT.SALES_ORDER_NAME, operator: search.Operator.ANYOF, values: customer }));`_
same error
b
Did you remove the code that changes activeContractsObj
t
yes I remove it
function *getListOfActiveContractPerCustomer*(customer) {
        
try {
            
var activeContractsObj = *getListOfActiveContracts*();
            
console.*log*('parameter:' + cfg.CONTRACT.SALES_ORDER_NAME + 'customer:' + customer);
            
var srchFilters = activeContractsObj.filters;
            
var customFilters = [];
            
customFilters = [cfg.CONTRACT.SALES_ORDER_NAME, search.Operator.ANYOF, customer];
            
srchFilters.*push*(customFilters);
_// activeContractsObj = srchFilters;_
_// activeContractsObj.filters.push(search.createFilter({ name: cfg.CONTRACT.SALES_ORDER_NAME, operator: search.Operator.ANYOF, values: customer }));_
            
return activeContractsObj.*run*().*getRange*(0, 1); _// get only the active contract with max subscription end date_
        
} catch (error) {
            
console.*log*('getListOfActiveContractPerCustomer:' + error);
        
}
    
}
for the reference of the other function here is the ss
b
It returns an array
Arrays dont have filters
t
so do i need to remove the .run() part on my first function?
b
Depends on what you want to do
But you cant change the filters of a search after you run it
t
ah I see.. I thought if I can put it on the object I can filter it whenever I needed it. i was planning to use same searchObject all throughout the code to lessen the load of the system..
b
you can copy its filters and columns to make new searches if you want
👍 1
you dont need to load it multiple times
as a warning, i've never really had a reason to reuse a search, usually its better to run one search that gets everything
running a search to get one result at a time is a waste
t
"you can copy its filters and columns to make new searches if you want" - this is what I'm trying to do hehe
b
you use search.create to make a new search
t
thanks @battk for pointing out my problem. I think what will I do is to search on my object. Maybe I'll use array.find on this 🙂