Learn how to close sales orders using a saved search and Suitescript

Introduction

A common customer ask is to close sales orders.  In order to change the status of a sales order to “Closed” you must close all the lines.  In this article I will show you how to create a map/reduce script to close sales orders based on a saved search.

Step 1: Create a saved search of sales orders that you want to close.

Make sure to mark it as “Public”.  The reason we want it Public is so it can be used when selecting a saved search for a script parameter (shown later).  Here is an example saved search:

 

Step 2: Add and deploy map/reduce script

This script will close all the lines of the sales orders which will cause their status to change to “Closed”.

Add and deploy the script (code shown below).  Reference my article “Quick Guide to Adding and Deploying a Script in NetSuite” if needed.  Add a script parameter for the saved search.  Refer to my article “Load and Save Records” to see how to create the script parameter as it is the same.

 

/**
 * @NApiVersion 2.1
 * @NScriptType MapReduceScript
 * @NModuleScope SameAccount

*/

define([ 'N/runtime', 'N/record', 'N/search'],
(runtime, record, search) => {

	getInputData = () => {
        log.debug('===START===');
        const scriptObj = runtime.getCurrentScript();
        const savedSearch = scriptObj.getParameter({name: 'custscript_saved_search'});
        const searchObj = search.load({
            id: savedSearch
        });
        return searchObj;
    }

    map = (context) => {
        log.debug('map context', context);
        try {
            const salesOrderRecord = record.load({
                type: record.Type.SALES_ORDER,
                id: context.key,
                isDynamic: true
            });
            const lineCount = salesOrderRecord.getLineCount({sublistId: 'item'});
            for (let i = 0; i < lineCount; i++) {
                salesOrderRecord.selectLine({sublistId: 'item',line: i});
                salesOrderRecord.setCurrentSublistValue({sublistId: 'item',fieldId: 'isclosed',value: true});
                salesOrderRecord.commitLine({sublistId: 'item'});
            }
            salesOrderRecord.save();
        }
        catch(e) {
            let message = e.message + ' : ' + JSON.stringify(e);
            log.error('Error', message);
        }
    }

    summarize = (summary) => {
        log.debug('==END==','==END==');
    }

    return {
        getInputData: getInputData,
        map: map,
        summarize: summarize
    };

});

Step 3: Run the Map/Reduce Script

On the script deployment, select “Save and Execute”.

Once the script has finished processing, you will see that all the sales orders in your saved search have been closed.

 

Conclusion

This relatively simple script can also be changed to close Purchase Orders.  The logic is the same.  You close the lines which will change the status to “Closed”.   If you need help scripting or customizing NetSuite please contact Suite Tooth consulting here to set up a free consultation.

If you liked this article, please sign up for my newsletter to get these delivered to your inbox here.

Follow on
Jaime Requena Chief Executive Officer

Jaime Requena is a seasoned NetSuite Consultant and Solutions Architect, known for delivering WHITE GLOVE service to businesses. With 15+ years of experience and 3x certifications in ERP, Developer, and Admin, Jaime specializes in highly customized NetSuite accounts, transforming operations for 200+ satisfied customers all across the globe.

Get Connected

How can we help?


    Stay in the loop with Suite Tooth Consulting!

    We aim to bring unmatched expertise and professionalism to your NetSuite initiatives. Let’s talk about how our NetSuite consultancy can make a difference!

    Global Client Satisfaction

    Client Testimonials

    It’s Been 4+ Years Now And We Have Worked With Hundreds Of Clients, Building Our Way To The Top, One Happy Client After Another! Their Voices Of Satisfaction Serve As A Testament To Our Success –