Turning off Emails After a Sandbox Refresh

I went looking for a way to turn off emails generated by saved searches and workflows. After a sandbox refresh, all saved searches and workflows which send to customers, employees, or other outside entities continue to run. Instead of emailing the original recipient, they get redirected to the owner of the saved search or workflow. In my case, this meant lots of emails.

What I found… there appears to be only one option. Setup >> Company >> Email Preferences

I’m posting this as a reminder to myself, as well as admitting defeat. I’m asking for help if anyone reading this knows a better way. During my research, I found an article that stated “Programmatic updates to saved searches” is a pending enhancement. I looked at Mass Update, CSV Imports and SuiteScript.

I went as far as writing a program (this runs in debugger) which reads a list of scheduled saved searches and tries to update them. It’s close, but no cigar! If anyone wants to continue with this, go for it! However, if you make progress, please share with the rest of us.

require(['N/search'],
    function (search) {

        /**
         * This example loads a saved search that includes other saved searches that run on a schedule.
         * The important thing is the fields: id and recordtype.
         * 
         */
        var mySearch = search.create({
            type: search.Type.SAVED_SEARCH,
            columns: [
                search.createColumn({ name: 'internalid' }),
                search.createColumn({ name: 'id' }),
                search.createColumn({ name: 'recordtype' })
            ],
            filters: [
                ['sendscheduledemails', 'is', true],
                'and',
                ['isinactive', 'is', false]
            ]
        });

        var results = mySearch.run().getRange(0, 1000);
        results.forEach(
            function (result) {
                var scriptid = result.getValue('id');
                var type = result.getValue('recordtype');
                var searchType = search.Type[type.toUpperCase().replace(' ', '_')];
                log.debug('type: ' + type + ', searchType: ' + searchType);

                /**
                 * In order to load a saved search, you must specify a search.Type.
                 * For "standard" types, like "customer", "Item", "transaction", you can translate
                 * the recordtype to an instance of search.Type. This will not work for saved searches
                 * against custom record types. 
                 */

                try {
                    if (searchType != undefined) {
                        /**
                         * Once you correct identify and translate the search.Type, you can load the saved search.
                         */
                        log.debug('Attempting to modify: ' + scriptid + ' (' + type.toUpperCase().replace(' ', '_') + ')');
                        
                        var mySearch = search.load({
                            id: scriptid,
                            type: searchType
                        });

                        /**
                         * Unfortunatley, you can make changes to settings without throwing an error.
                         */
                        mySearch.settings.scheduledalert = false;
                        mySearch.settings.triggeredalert = false;

                        /**
                         * AND... you can save the search without throwing an error.
                         */
                        mySearch.save()
                    }
                }
                catch (err) {
                    log.debug('error: ' + err);
                }
            }
        );
    }
);

Leave a comment