Tip 20: How can I use a search on Document(file) type in Netsuite SuiteScript (1.0 & 2.0)

Hello Friend,

Its always advised to avoid using saved searches  in scripting as there may be a chance of getting deleted either knowingly/unknowingly.

But there are certain searches which are not included in Search.Type like below example:

While working on a requirement, I realized to add a search on File. As I suggested above I always prefer to use search created in script rather than using a saved search.

But unfortunately I didn’t find any Search.Type for File/Document Type.

So below code was showing error Type Document is not a Valid type in Search.

var mySearch = search.create({
                type: Search.Type.DOCUMENT,
                filters : myFilters,
                columns : myColumns
            });

 

Then I checked in Saved Search and Document type is available there.

Not sure why Netsuite has that difference in Search.Type and Saved Search Type.

Then I created a saved search using Document type and loaded that search in my script as I have no other option.

saved_seach
Fig 1 – Saved Search On Document Record

I recommend to use word like “(Used By Script)” or “(DO NOT DELETE)” in saved search name, so that if somebody will see that saved search, he will be able to understand, this saved search is used for some script. So risk of getting deleted will be reduced to somewhat.

So, here a simple suggestion to all who has similar issues:

If something is not happening using search.create(), try creating saved search on same search type and use search.load() and use that as search.load() doesn’t need any type parameter.

           //Load Saved Search created already with selected id as filter
            var mySearch = search.load({
                id: ‘customsearch_search_file’
            });

I have used SuietScript 2.0 in my sample code. You can refer to SuiteScript 1.0 accordingly if you need.

Hope that will help you.

Happy Coding 🙂

Thanks,

Asha

Tip 19: Add Filter to search.load in Netsuite Suitescript 2.0

Hello Friends,

We all know how to add filters while creating a search in Suitescript, as search.create() has a parameter called filters.

Still I am adding it here for anybody who finds difficult to create a search:

var mySearch= search.create({
type: “folder”,
columns: [
“name”,
“internalid”,
“numfiles”
],
filters: [
search.createFilter({
name: ‘name’,
operator: search.Operator.IS,
values: [folderName]
})
]
});

 

Now let’s see how can we add filter to an already existing saved search created either in UI or by search.create() & then search.save().

    var mySearch = search.load({
id: ‘customsearch_test_search’
});
//Add Filter to search
var filter1 = search.createFilter({
name: ‘name’,
operator: search.Operator.IS,
values: [selectedName]
});
mySearch.filters.push(filter1);

I hope this small tip will help you.

Happy Coding 🙂