How to Use the Same Script for Different Records with Different Parameters in NetSuite

To customize the behavior of a NetSuite script for different records, use the getParameter() function. This function returns the value of a script parameter for the currently executing script, allowing you to pass different parameters to the same script. Here’s how to use it in SuiteScript 2.0 and set the parameter value at deployment.

define([‘N/runtime’, ‘N/log’], function(runtime, log) {

  function executeScript(context) {

    // Retrieve the script parameter

    var paramValue = runtime.getCurrentScript().getParameter({

      name: ‘custscript_my_parameter’

    });

     

    // Log the parameter value for debugging

    log.debug(‘Script Parameter’, paramValue);

    // Use the parameter value to customize the script’s behavior

    if (paramValue === ‘value1’) {

      // Logic for handling value1

      log.debug(‘Handling’, ‘Logic for value1’);

    } else if (paramValue === ‘value2’) {

      // Logic for handling value2

      log.debug(‘Handling’, ‘Logic for value2’);

    } else {

      // Default logic

      log.debug(‘Handling’, ‘Default logic’);

    }

  }

  return {

    execute: executeScript

  };

});

Setting the Parameter Value at Deployment

Navigate to Script Deployment:

  1. Go to Customization > Scripting > Script Deployments.
  2. Edit or Create a Deployment:
  3. Edit an existing deployment or create a new one for your script.
  4. Set the Parameter Value:
  5. In the Parameters section, find the custom parameter (e.g., custscript_my_parameter).
  6. Enter the desired value for the parameter (e.g., value1 or value2).
  7. Save the Deployment: Save your changes to apply the parameter value to the script deployment.

Script:

Deployment:

By using getParameter() and setting parameter values at deployment, you can make a single NetSuite script versatile enough to handle different records and scenarios based on the provided parameters. This approach allows for greater flexibility and reusability of your scripts in NetSuite.

Leave a comment

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