Hello! I am loading a saved search in a M/R script...
# suitescript
k
Hello! I am loading a saved search in a M/R script, adding in a filter to the search obj, saving it and downloading the saved search as a csv. Once the csv is downloaded, is it possible to remove the filter that I just added?
n
Do you need to save the saved search each time?
k
Yes, I am passing in the saved search as a script parameter, and every time I run the script I would like to add a new filter to the saved search. I am thinking I can possibly achieve this by creating the search in the script itself, but was wondering if removing filter was possible to a saved search object.
n
Pretty sure you can remove the search filters much as you added them, you just might struggle to pull out the correct filter. Probably better having a base search in code and adding in the filter on the fly.
k
yeah, I will probably end up doing that.
m
You can just dynamically add the filter just for your M/R search. What you do is store the filters into a variable, push the new filter into the filters array and assign the array back to the search and run it.
Copy code
const search_filters = mysearch.filters;
search_filters.push({name: 'field', operator: search.Operator.ANYOF, values: ['filter_value']});
mysearch.filters = search_filters;
mysearch.run().each(result => {
// parse into csv.
});
k
this is what I ended up doing. I was looking into the option of downloading a csvfile from a search after adding a filter without having to parse the data, but the direct download does not seem to work for what is asked for. thanks for sharing!
m
Honestly the query module makes it easier to parse into a CSV
.asMappedResults()
will put into an array that you can directly put into a parser like Papa parse.
n
you could also use the task module to create your CSV if you don't mind waiting for it to complete.
Copy code
let myTask = task.create({
  taskType: task.TaskType.SEARCH
});
Just throwing that out there in case that approach works for you.