Checking the Status of Background Scripts – SuiteScript Example

I was running into governance issues with long-running tasks. My workaround: launch a map/reduce script in the background to perform the lengthy task. The problem: How to let a non-Administrator know when their background script had completed. This turned out to be easy, but undocumented. 

Check NetSuite’s documentation for “search.Type.” You’ll find a long list of record types searchable by the ‘N/search’ module. Unfortunately, there is at least one missing entry: “scriptexecutionlog”.

Here is a working example in SuiteScript 2.0. Notice, I’m using require() and not define(), so this is ready to paste into debugger. Also, don’t forget to comment out a couple of my filters.

Script Execution Log Code

One more note. I looked long and hard to find a way other than reading server logs to see the status of my background scripts. However, I control both Title and Detail fields written by the map() function in the map/reduce scripts in the background. So I can tell my user whatever I choose as they read their log files. The map reduce script can calculate “Percent complete”, or I can just show “Running” until the job ends. Until I find a better way… that’s the plan.

Here is the code in format you can more easily copy.

require([‘N/search’, ‘N/log’],
    function (search, log) {
        var scriptLog = new Array();
         var mySearch = search.create({
            type: ‘scriptexecutionlog’,
            columns: [
                search.createColumn({ name: ‘date’, sort: ‘DESC’ }),
                search.createColumn({ name: ‘detail’ }),
                search.createColumn({ name: ‘internalid’ }),
                search.createColumn({ name: ‘name’, join: ‘script’ }),
                search.createColumn({ name: ‘scriptfile’, join: ‘script’ }),
                search.createColumn({ name: ‘scriptid’, join: ‘script’ }),
                search.createColumn({ name: ‘scripttype’, join: ‘script’}),
                search.createColumn({ name: ‘time’ }),
                search.createColumn({ name: ‘title’ }),
                search.createColumn({ name: ‘user’ }),
            ],
            filters: [
                [‘script.name’, ‘startswith’, ‘Logic’],
                ‘and’,
                [‘user’, ‘is’, ‘Kevin McCracken’],
                ‘and’,
                [‘date’, ‘onorafter’, ‘yesterday’],
                ‘and’,
                [‘script.scripttype’, ‘is’, ‘MAPREDUCE’],
            ]
         });
         var myPages = mySearch.runPaged({ pageSize: 1000 });
         debugger;
         for (var i = 0; i < myPages.pageRanges.length; i++) {
            var myPage = myPages.fetch({ index: i });
            myPage.data.forEach(
               function (result) {
                var date = result.getValue({ name: ‘date’ });
                var detail = result.getValue({ name: ‘detail’ });
                var internalid = result.getValue({ name: ‘internalid’ });
                var name = result.getValue({ name: ‘name’, join: ‘script’ });
                var scriptfile = result.getValue({ name: ‘scriptfile’, join: ‘script’ });
                var scriptid = result.getValue({ name: ‘scriptid’, join: ‘script’ });
                var scripttype = result.getValue({ name: ‘scripttype’, join: ‘script’ });
                var time = result.getValue({ name: ‘time’ });
                var title = result.getValue({ name: ‘title’ });
                var user = result.getValue({ name: ‘user’ });
                objEntry = {
                    date: date,
                    detail: detail,
                    internalid: internalid,
                    name: name,
                    scriptfile: scriptfile,
                    scriptid: scriptid,
                    scripttype: scripttype,
                    time: time,
                    title: title,
                    user: user
                };
                log.debug(JSON.stringify(objEntry));
                debugger;
                scriptLog.push(objEntry);
               }
            )
         }
    }
)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s