Don’t Miss it: Top Strategies for Driving Diversity

days / hours / minutes / seconds

Register Here »
NetSuite logo
NETSUITE RESOURCES
workplace by meta logo
FUTURE
OF WORK WORKPLACE BY META
University icon
CUSTOMER EDUCATION blog
Atlassian Logo
Adoption blog
How-to
December 28, 2023
|
3.5 min
reading time

SuiteScript 2.0: Using N/task.SearchTask to Save Hours of Coding Time

Share this article
/*SoMe buttons*/ <-- Facebook Share Button Code --> <-- Facebook Share Button Code --> <-- Twitter Share Button Code --> <-- Twitter Share Button Code --> <-- Linkedin Share Button Code --> <-- Linkedin Share Button Code -->

For NetSuite Developers, there’s one NetSuite module member you should familiarize yourself with: N/task.SearchTask. It is a favorite feature of mine in NetSuite’s SuiteScript 2.0 next to Map/Reduce. Thanks to @erictgrubaugh for mentioning it in the Slack community.

If this is your first time hearing about it, now is the time to check it out.

What N/task.SearchTask does is fetch saved search results and export them to a CSV file. It is a great module member because it can help you save hours of coding time due to its unique features. Here are just a few reasons you should include it in your SuiteScript vocabulary:

1. It’s Straightforward

This is the most obvious advantage. You just need some parameters like the file ID, the saved search ID and the script, and you’re ready to go.

2. It Doesn’t Require Endless Lines of Code

If you’re not using N/task.SearchTask you would have to use N/search and N/file methods, run the search, display the data, save it in the file cabinet and so on. But with N/task.SearchTask, it’s as short as seven lines of code.

3. There’s No Need for Additional Character Manipulation

Gone are the days of using native Javascript methods for special characters, especially the comma. With N/task.SearchTask, there’s no need to go through that hassle.

4. It’s been proven to be faster than its integration counterpart.

As an example, say you have a project where you have an integration that needs to fetch saved search results and build a CSV file out of it. Using other methods, it could take 15 minutes to pull thousands of records. But with N/task.SearchTask, if you remove that integration and let this script do a similar job, it will only take around one minute.

Pre-requisites

Now that you know the benefits of using N/task.SearchTask, it’s important to know what you’ll need to get started. The list is pretty short:

1. Empty CSV File

You will need to open an empty CSV file and save it in the File Cabinet to get started.

2. Saved Search

Before starting, prepare the saved search you will use in your script. Below you will find a screenshot of a sample saved search result. Notice that it intentionally uses special characters because when doing this process without N/task.SearchTask, you would need some character formatting to avoid output errors.

3. The Script

The last piece you need is the script itself. You can use the below sample code in a scheduled script format:



/**
 * @NApiVersion 2.x
 * @NScriptType ScheduledScript
 * @NModuleScope SameAccount
 */
define(['N/task'],

function(task) {

    const FILE_ID = 2411;
    const SEARCH_ID = 148;

    /**
     * Definition of the Scheduled script trigger point.
     *
     * @param {Object} scriptContext
     * @param {string} scriptContext.type - The context in which the script is executed. It is one of the values from the scriptContext.InvocationType enum.
     * @Since 2015.2
     */
    function execute(scriptContext) {
        var searchTask = task.create({
            taskType: task.TaskType.SEARCH
        });
        searchTask.savedSearchId = SEARCH_ID;
        searchTask.fileId = FILE_ID;
        var searchTaskId = searchTask.submit();
    }

    return {
        execute: execute
    };

});

Sample Output

Once you run the script, you will see an output like the sample screenshot below. In this, you’ll see that the header names and search results are displayed just how they should be and the special characters appear normally.

Find Your Next NetSuite Partner

With ServiceRocket, you’ll get certainty, expertise and success built into your NetSuite implementation, configuration and custom development. When you work with us, you get access to NetSuite-trained Certified Developers with over 400+ NetSuite scripts and 50+ Suitelet workflows combined. Rest assured that ServiceRocket will help you find success and provide you with the opportunities you need to transform your business.

Do you have NetSuite license management, implementation, administration, support or custom development needs? We’ve got your back! Contact our NetSuite Certified Experts today.

Ensure the highest return on your NetSuite investment with our custom development or administrative services.

Learn MoreServiceRocket Backed - We've got your back badge