Adding dynamic columns and filters to the saved search

Requirement

We need to add some columns and filters to the saved search created in UI and get results based on the newly added filters and columns

Solution

We can load the saved search in code by using the id. Then we can add the filters and columns to the loaded saved search and run the search. Following is the code snippet.

  var customerSearchObj = search.load({
                    id: savedSearchId  // Provide the id of the saved search you want to load.
                });


                //Get filters from the search loaded
                var srchFilters = customerSearchObj.filters;

                //Push the required filters to the filters from the search loaded
                srchFilters.push(search.createFilter({
                    name: "custentity_emial_type",
                    operator: "anyof",
                    values: [
                        "4"
                      
                    ],
                    isor: false,
                    isnot: false,
                    leftparens: 0,
                    rightparens: 0
                }))
               
                log.debug("customerSearchObj.filters", customerSearchObj.filters)

                //Get the internalid , email and name to add to the search columns
                var internalId = search.createColumn({name: "internalid", label: "internal Id of Customer"})
                var emailAddr = search.createColumn({name: "email", label: "Email"})
                var name = search.createColumn({name: "altname", label: "Name"})


                //Add the columns as internal id, email and name to the search loaded
                customerSearchObj.columns.push(internalId);
                customerSearchObj.columns.push(emailAddr);
                customerSearchObj.columns.push(name);


                try {
                    var searchResult = customerSearchObj.run().getRange({
                        start: 0,
                        end: 1000
                    });
                    log.debug('search reusult of search loaded', searchResult)
                

Leave a comment

Your email address will not be published. Required fields are marked *