How to Reschedule Scheduled Scripts in NetSuite Using SuiteScript 2.x
Tags: netsuite, suitescript, javascript
Published: 2024-03-27 20:53:24

**Introduction**
Bulk data manipulation in NetSuite can get tricky when you're dealing with massive datasets or complex logic . Scheduled scripts are like your trusty tools , but governance limits can sometimes throw a wrench in the works  (we've all been there!). This guide explores a time-saving technique using SuiteScript 2.x to overcome these limitations and keep your data updates running smoothly ✅.

**Challenges of Governance Limits **

- Large datasets and complex logic can quickly max out a script's usage units, leaving the job unfinished .
- Re-running the script from scratch after each limit hit is inefficient and can be a real drag .

**Current Inefficient Options: A Tale of Two Inefficiencies **

**Re-running for Disappearing Saved Search Results 🪄 (Not Really Magic)
**
If updated records vanish from saved search results (like a disappearing act!), you'd need to repeatedly execute the script until completion. This can be extremely time-consuming and frustrating ⏳.

**Re-running with Index Tracking for Persistent Results ️‍♀️
**
For results that remain after updates (like a persistent detective!), you'd track the last processed index and pass it as a script parameter to pick up where you left off. Although better, this still requires multiple script runs, which can be tedious .

**Proposed Solution: Dynamic Rescheduling for Efficiency ⏱️ (The Hero We Need!)**

This approach is your time-saving hero, streamlining the process and minimizing manual intervention . Within your update loop, you'll check the script's remaining usage units. If it falls below a specific threshold (think of it as a fuel gauge for your script ⛽), you'll automatically reschedule the script using the `N/task` module:

```javascript
for (let i = 0; i < invoices.length; i++) {
  if (runtime.getCurrentScript().getRemainingUsage() < 200) {
    log.audit({
      title: executeID.toString() + "|RESUME",
      details: "Resuming from " + i.toString() + " of " + invoices.length.toString(),
    });

    let reschTask = task.create({
      taskType: task.TaskType.SCHEDULED_SCRIPT,
      scriptId: runtime.getCurrentScript().id,
      deploymentId: runtime.getCurrentScript().deploymentId,
      params: {
        custscript_start_instms_from: i,
      },
    });
    reschTask.submit();
    return;
  }
  // Your update logic here
}
```

Explanation: Under the Hood of Dynamic Rescheduling ⚙️

- **Usage Threshold Check**: The script constantly monitors its remaining units, like a fuel gauge keeping you informed ⛽.
- **Rescheduling Trigger**: If the units dip below the threshold (your fuel gauge nearing empty!), rescheduling kicks in to avoid running out of steam ➡️⏱️.
- **Reschedule Task Creation**: A new task is created using the N/task module, essentially sending a "refuel" message to the system ⛽.
- **Reschedule Details**: The task specifies the script ID, deployment ID, and any necessary parameters (e.g., `custscript_start_instms_from` to indicate the starting index for persistent results). This ensures the script picks up exactly where it left off, just like resuming a movie .
- **Task Submission**: The rescheduling task is submitted, guaranteeing that the script can continue its work seamlessly without interruption .

**Benefits of Dynamic Rescheduling: A Win-Win Situation! **

- Reduced Manual Intervention: No more manually restarting the script after each limit reach, freeing you from repetitive tasks ➡️✅.
- Improved Efficiency: Streamlines mass data updates by avoiding unnecessary re-runs, saving you valuable time ⏱️.
- Optimized Workflows: Scripts can handle larger datasets and complex logic more effectively, allowing you to tackle bigger challenges with confidence .
**Remember: Setting the Right Threshold **

- Set an appropriate usage threshold based on your script's typical unit consumption. This is like finding the sweet spot for your fuel gauge to avoid unnecessary rescheduling or running out of units ⛽️.
- This technique is particularly useful for scenarios where script execution might vary depending on the data being processed. Think of it as adapting your driving style based on road conditions