Customer Inactivity Detection and Status Change via SuiteScript

Keeping your Customer database current is essential for maintaining accurate records and supporting efficient business operations. Over time, some Customers may become inactive, no longer placing orders, or engaging with your company. Instead of manually reviewing each record, you can automate this process using a Scheduled Script.

This script uses the N/search and N/record modules to identify customers who have not created a Sales Order within the past year and automatically update their status to inactive. By implementing this automation, Administrators can save time, reduce manual work, and ensure that customer data remains clean and up to date.

Sample Scheduled Script Code

/**
 * @NApiVersion 2 .x
 * @NScriptType ScheduledScript 
 */
define(['N/search', 'N/record', 'N/log'], function(search, record, log) {
    function execute(context) {
        try {
            // Search for customers who haven't created a sales order in the last 365 days
            var customerSearch = search.create({
                type: search.Type.CUSTOMER,
                filters: [
                    ["lastorderdate","onorbefore","daysago365"]
                ],
                columns: ["entityid", "internalid"]
            });

            customerSearch.run().each(function(result) {
                var customerId = result.getValue("internalid");

                // Load customer record
                var customer = record.load({
                    type: record.Type.CUSTOMER,
                    id: customerId,
                    isDynamic: false
                });

                // Set status to inactive (adjust fieldId as per your setup)
                customer.setValue({
                    fieldId: 'entitystatus', // May vary based on your configuration
                    value: 14 // Replace with appropriate status internal ID
                });

                customer.save();
                log.audit('Customer Flagged Inactive', 'Customer ID: ' + customerId);
                return true;
            });
        } catch (e) {
            log.error('Error updating customer status', e.message);
        }
    }
    return {
        execute: execute
    };
});

Code Segment / Line(s)

Purpose / Explanation

/** … */

Script header: defines API version and script type as Scheduled Script.

define([‘n/search’, ‘n/record’, ‘n/log’], function(search, record, log)

Imports required modules for search, record manipulation, and logging.

function execute(context) { … }

Main function that is executed according to the deployment schedule.

var customersearch = search.create({…});

Creates a Saved Search for customers whose last Sales Order was over 365 days ago.

filters: [[“lastorderdate”,”onorbefore”,”daysago365″]]

Search filter: finds Customers with no order for at least one year.

columns: [“entityid”, “internalid”]

Search columns: retrieves Customer name and Internal ID.

customersearch.run().each(function(result) { … });

Iterates over each search result (each inactive Customer).

var customerid = result.getvalue(“internalid”);

Gets the internal id of the Customer from the search result.

var customer = record.load({ … });

Loads the Customer record into memory by Internal ID.

customer.setvalue({ fieldid: ‘entitystatus’, value: 14 });

Sets the Customer’s status to a new value (here, 14 = inactive; verify status id in your account).

customer.save();

Saves the updated Customer record back to netsuite.

log.audit(‘customer flagged inactive’, …);

Logs an audit entry indicating the Customer record was updated.

return true;

Continues processing to the next search result.

catch (e) { log.error(…); }

Catches and logs any error that occurs during the process.

return { execute: execute };

Exposes the execute function, so this can call it as a Scheduled script.

Leave a comment

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