The Ultimate Guide to NetSuite SuiteScript

suitescript guide

In today’s fast-evolving technological landscape, businesses are constantly looking for ways to gain a competitive edge by moving beyond the constraints of traditional software systems. SuiteScript stands out as a transformative framework, enabling organizations to redefine their NetSuite experience with unparalleled customization and operational flexibility.

More than just a scripting tool, SuiteScript is a strategic enabler that empowers developers to extend, modify, and automate complex business processes. Offering deep control over NetSuite’s core functionality, SuiteScript bridges the gap between standard ERP capabilities and the advanced, often unique, requirements of modern businesses.

In this blog we’ll explore SuiteScript, its features, components, and how it can enhance NetSuite. We’ll cover examples, best practices, and resources to help you customize workflows, automate processes, and integrate with other systems.

What is SuiteScript in NetSuite?

SuiteScript is a JavaScript-based scripting language designed to interact with NetSuite’s records, data, and workflows. It allows developers to customize NetSuite to meet specific business requirements, enabling the creation of tailored solutions such as custom forms, automation routines, and data integrations. SuiteScript provides deep access to NetSuite’s underlying platform, offering flexible tools to adapt the system to an organization’s unique needs.

How Does SuiteScript Work?

SuiteScript allows developers to write custom JavaScript scripts that interact directly with the NetSuite platform’s APIs. These scripts run on NetSuite’s server and are designed to automate complex business processes, modify records, and create custom workflows. SuiteScript enables tasks such as creating or updating records, sending emails, generating reports, and triggering other scripts, all tailored to the specific needs of the business.

Scripts can be triggered by various events, such as the creation of a sales order or the submission of an invoice, as well as on a scheduled basis. This level of flexibility allows businesses to optimize NetSuite for their operations, reducing manual effort and improving overall efficiency. Furthermore, SuiteScript integrates seamlessly with NetSuite’s native modules and third-party systems via APIs and RESTlets, enabling deeper customization and extended functionality.

Key Features of SuiteScript

SuiteScript offers powerful features to enhance the customization and automation of NetSuite. Here are the key features:

Script Types: SuiteScript supports various script types

  • User Event Scripts: Triggered by user actions like creating, editing, or deleting records.
  • Scheduled Scripts: Run on a scheduled basis for tasks like data imports or batch processes.
  • Suitelets: Create custom pages and UIs within NetSuite for flexible data presentation.
  • RESTlets: Expose NetSuite data via RESTful APIs for integration with external systems.
  • Map/Reduce Scripts: Handle large datasets with parallel processing, ideal for data migration or complex reporting.

Modular Architecture (SuiteScript 2.0 and 2.1):

SuiteScript 2.0/2.1 uses a modular programming approach, enhancing maintainability and reducing redundancy through RequireJS for clean dependency management.

Asynchronous Programming (SuiteScript 2.1)

SuiteScript 2.1 supports async/await, enabling asynchronous operations that improve script performance, especially in scenarios with external integrations or large data volumes.

Advanced Data Manipulation

SuiteScript provides APIs for creating, updating, deleting, and searching records in real time, enabling complex data handling through the Record and Search APIs.

Error Handling and Debugging

SuiteScript features robust error handling using try/catch blocks and logging functions like nlapiLogExecution to help troubleshoot issues during development and production.

Integration Capabilities

SuiteScript allows integration with external APIs, databases, and services, using modern JavaScript features such as Promise and fetch() to enhance data exchange.

Real-Time Execution

Scripts execute in real time, immediately reflecting changes in the NetSuite interface, which is valuable for automating processes like record updates.

Customization of User Interfaces

Suitelets allow for creating customized forms, dashboards, and applications, offering a tailored user experience within the NetSuite interface.

These features empower developers to optimize workflows, integrate systems, and create highly customized solutions within NetSuite.

Getting Started with SuiteScript: Creating Script and Deployment Records

1
Step 1: Create the Script Record

Navigate to the Script Record Page

  1. Go to: Customization > Scripting > Scripts > New.

Select the Script File

  1. In the Script File dropdown list, select helloWorld.js.
  2. If you haven’t uploaded the file yet, click the plus icon next to the dropdown to open a window to upload it.

Create the Script Record

  1. After selecting the script file, click the Create Script Record button. This will display a new script record page with the helloWorld.js file listed under the Scripts subtab.

Fill Out the Required Fields

  1. In the Name field, enter Hello World Client Script.
  2. In the ID field, enter _cs_helloworld.

Save the Script Record

  1. Click Save to create the script record.
2
Step 2: Create the Script Deployment Record

Navigate to the Deployments Subtab

  1. Click the Deployments subtab to create the deployment record.

Add a Deployment Line

  1. Add a new line to the sublist with the following details:
    • Applies to: Set this to Task (or another record type if desired).
    • ID: Enter _cs_helloworld.
    • Leave the other fields with default values. The Status will be set to Testing, meaning the script is not deployed for other users. You can change this later to Released to deploy the script to all users.

Save the Deployment Record

  1. Click Save to create the script deployment record.
3
Step 3: Test the Script

Open a Task Record

  1. Go to: Activities > Scheduling > Tasks > New to create a new task record.

Verify the Dialog Alert

  1. When the task record loads, the system should display a dialog box with the “Hello, World!” message. Confirm and close the dialog by clicking OK.

Check the Execution Log

  1. To verify the script’s execution:
    • Go to: Customization > Scripting > Script Deployments.
    • Locate the deployment and click View.
    • Click the Execution Log subtab to check for the entry generated by the script execution.
    • If there was an error during execution, it will show up in the Execution Log instead of the debug entry.

SuiteScript Versioning

SuiteScript Versions: 1.0, 2.0, and 2.1

SuiteScript has evolved over time, with each version offering improved features and functionality:

  • SuiteScript 1.0: The original version with a procedural programming model. It is now legacy and is not recommended for new development.
  • SuiteScript 2.0: Introduced object-oriented programming (OOP) principles, offering better performance, error handling, and modularity. It is the standard for most customizations and integrates well with NetSuite.
  • SuiteScript 2.1: The latest version that builds on SuiteScript 2.1 with generative AI capabilities, incorporating modern JavaScript features from ECMAScript 2019 (ES2019), such as async/await. SuiteScript 2.1 is fully compatible with SuiteScript 2.0, allowing both versions to run side-by-side.

Example Code of SuiteScript 1.0, 2.0 & 2.1

Simple SuiteScript 1.0 script to create a customer record:


function createCustomer() {
  try {
    var customer = nlapiCreateRecord('customer');
    customer.setFieldValue('companyname', 'Acme Corporation');
    customer.setFieldValue('email', 'contact@acme.com');
    var customerId = nlapiSubmitRecord(customer);
    nlapiLogExecution('DEBUG', 'Customer Created', 'Customer ID: ' + customerId);
  } catch (e) {
    nlapiLogExecution('ERROR', 'Error Creating Customer', e.message);
  }
}

SuiteScript 2.0 – Creating a Customer Record

In SuiteScript 2.0, the same task would be done with the N/record module:


define(['N/record', 'N/log'], function(record, log) {

  function createCustomer() {
    try {
      // Create a new customer record
      var customerRecord = record.create({
        type: record.Type.CUSTOMER,
        isDynamic: true
      });

      // Set customer fields
      customerRecord.setValue({
        fieldId: 'companyname',
        value: 'Acme Corporation'
      });
      customerRecord.setValue({
        fieldId: 'email',
        value: 'contact@acme.com'
      });

      // Save the customer record
      var customerId = customerRecord.save();
      log.debug('Customer Created', 'Customer ID: ' + customerId);

    } catch (e) {
      log.error('Error Creating Customer', e.message);
    }
  }

  return {
    execute: createCustomer
  };

});

SuiteScript 2.1 – Creating a Customer Record

In SuiteScript 2.1, new features such as ES2019 (ES6) language capabilities come into play. Here’s an example of creating a customer record:


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

  const createCustomer = () => {
    try {
      // Create a new customer record
      const customerRecord = record.create({
        type: record.Type.CUSTOMER,
        isDynamic: true
      });

      // Set customer fields
      customerRecord.setValue({
        fieldId: 'companyname',
        value: 'Acme Corporation'
      });
      customerRecord.setValue({
        fieldId: 'email',
        value: 'contact@acme.com'
      });

      // Save the customer record
      const customerId = customerRecord.save();
      log.debug('Customer Created', `Customer ID: ${customerId}`);

    } catch (e) {
      log.error('Error Creating Customer', e.message);
    }
  };

  return { execute: createCustomer };
});

Comparison of SuiteScript 1.0 vs. 2.x:

Feature SuiteScript 1.0 SuiteScript 2.x
Programming Style Procedural programming Object-oriented programming
JavaScript Version Based on older JavaScript standards Based on ECMAScript 5 (modern JavaScript)
Script Structure Functions are defined globally Modular architecture with custom modules
Error Handling Limited error handling mechanisms Enhanced error handling with try-catch blocks
Asynchronous Support No native support for asynchronous operations Support for asynchronous functions (Promises, callbacks)
Code Maintainability Difficult to maintain due to procedural nature Easier to maintain with organized, modular code
Compatibility Not compatible with ECMAScript 5 features Fully compatible with ECMAScript 5 and later features
Integration Flexibility Limited integration capabilities with external systems Improved integration with external applications and APIs
Backward Compatibility N/A Compatible with SuiteScript 1.0 scripts in the same account

 

SuiteScript Debugging and Testing

  • Use the SuiteScript Debugger: Identify issues with real-time code inspection and variable monitoring. Access the debugger through the SuiteCloud Development Framework (SDF) or within NetSuite.
  • Add Log Statements : Track execution flow and key variables using log statements like log.debug('Title', value) (SuiteScript 2.x) or nlapiLogExecution('DEBUG', 'Title', value) (SuiteScript 1.0).
  • Test in Sandbox Environment : Validate scripts in a Sandbox to ensure they work as expected without affecting live data.
  • Leverage Browser Tools : For Suitelets and client scripts, use browser tools (e.g., Chrome DevTools) to inspect network requests and monitor client-side execution.
  • Apply Error Handling: Use try/catch blocks to manage exceptions and notify the team of errors using nlapiSendEmail or email.send.

SuiteScript Best Practices for Efficient and Scalable Development

To ensure your SuiteScripts are efficient, maintainable, and scalable, following best practices is crucial. Here are some key tips:

  1. Optimize API Calls: Minimize API calls to improve performance. Use searches like nlapiSearchRecord or search.create to retrieve multiple records at once instead of calling them individually.
  2. Leverage Caching: Use SuiteScript 2.x’s Cache API to store frequently accessed data and reduce redundant system calls.
  3. Use Asynchronous Scripts: Prefer asynchronous scripts (e.g., Scheduled Scripts, Map/Reduce Scripts) for large processes to prevent timeouts and improve system resource usage.
  4. Error Handling: Implement try-catch blocks and log errors using log.debug in SuiteScript 2.x to make troubleshooting easier.
  5. Modularize Code: Organize your code into reusable modules for better maintainability and easier debugging.
  6. Choose the Right Script Type: Select the appropriate script type based on the use case—User Event Scripts for record operations, Suitelets for custom UIs, and Scheduled Scripts for background tasks.
  7. Keep Scripts Modular and Reusable: Break down large scripts into smaller, reusable modules to improve readability and maintainability.
  8. Document Your Code: Provide clear documentation and comments for complex logic and parameters to help future developers.
  9. Minimize Script Execution Time: Optimize scripts to keep execution times short, as NetSuite imposes limits on how long scripts can run.
  10. Test in a Sandbox: Always test scripts in a sandbox environment to avoid issues in your live account.

Resources and Support for SuiteScript Developers

As a SuiteScript developer, reliable resources are key to writing, debugging, and optimizing your scripts. Here’s a concise list of support channels:

  1. NetSuite Help Center: The main resource for SuiteScript developers, offering documentation, tutorials, and sample code.
  2. SuiteScript API Documentation: Essential for understanding SuiteScript 1.0 and 2.x, with details on objects, methods, and common use cases.
  3. SuiteCloud Developer Network (SDN): A community platform for collaboration, knowledge-sharing, and accessing technical resources.
  4. NetSuite User Group & Community Forums: Connect with developers and users to troubleshoot and share solutions.
  5. Training & NetSuite Certification Programs: Enroll in NetSuite’s SuiteScript courses and pursue developer certification to enhance your skills.
  6. SuiteScript Blogs & Tutorials: Stay updated with blogs from SuiteWorld, Celigo, and ERP Peers for expert insights and tips.
  7. NetSuite Developer Tools: Utilize tools like the SuiteScript Debugger and SuiteCloud IDE to optimize coding and deployment.
  8. Professional Support: Contact NetSuite Support or professional NetSuite developers for assistance with complex SuiteScript issues.

Conclusion

SuiteScript is a powerful tool for customizing and automating NetSuite processes. Understanding the differences between versions 1.0, 2.0, and 2.1 is crucial for leveraging the right features for your business needs. SuiteScript 2.0 and 2.1 offer enhanced functionality, modularity, and modern JavaScript support, providing developers with greater flexibility and efficiency. By following best practices and staying updated with the latest version, you can create more effective and scalable solutions for your NetSuite environment.

FAQs

While SuiteScript 2.0 is still widely used, SuiteScript 2.1 introduces modern JavaScript features like async/await and improved performance, making it the ideal choice for new projects and complex customizations.

SuiteScript 2.0 and SuiteScript 2.1 are the preferred versions for new development, offering better functionality, modularity, and modern JavaScript features. SuiteScript 1.0 is the legacy version, and while it can still be used, it is recommended to migrate to 2.0 or 2.1 for new projects.

While SuiteScript 1.0 and 2.0 cannot share the same API in one script, you can use them in the same NetSuite account. For example, a SuiteScript 1.0 script can call a SuiteScript 2.x script via RESTlets or external calls.

SuiteScript is commonly used for automating workflows, customizing NetSuite forms and records, creating custom reports, integrating with external systems, and building tailored user interfaces.

Our Blogs

Get In Touch

Our customer support team is available for help.

Let's Talk Business!