Module 14: Best Practices & Considerations

Week 12 • NetSuite SuiteScript 2.0 Training • ~60 minutes

🎯 Learning Objectives

1. Governance Management

Every script has governance limits. Running out means your script stops!

Script TypeGovernance Limit
Client Script1,000 units
User Event1,000 units
Scheduled10,000 units
Map/ReduceAutomatic (per stage)
Suitelet1,000 units
RESTlet5,000 units

Common Operation Costs

OperationUnits
record.load()10
record.save()20
search.create()5
search.lookupFields()1
email.send()20
http.get/post()10
✅ Governance Tips
  • Use search.lookupFields() instead of record.load() when only reading
  • Monitor with runtime.getCurrentScript().getRemainingUsage()
  • Offload heavy processing to Scheduled/Map Reduce scripts
  • Use Map/Reduce for bulk operations (automatic governance)

2. Performance Optimization

✅ Do This

// Lookup specific fields
search.lookupFields({
    type: 'customer',
    id: 123,
    columns: ['email']
});

🚫 Not This

// Load entire record
var rec = record.load({
    type: 'customer',
    id: 123
});
var email = rec.getValue('email');

✅ Do This

// Run search ONCE, process results
var results = [];
mySearch.run().each(function(r) {
    results.push(r.getValue('name'));
    return true;
});

🚫 Not This

// Running search inside a loop
for (var i = 0; i < ids.length; i++) {
    var search = search.create({...});
    // Creates new search each iteration!
}

3. Error Handling

try {
    var rec = record.load({
        type: record.Type.CUSTOMER,
        id: customerId
    });
    rec.setValue('email', newEmail);
    rec.save();
    
} catch (e) {
    // Log the error
    log.error({
        title: 'Error updating customer',
        details: e.message + ' | Stack: ' + e.stack
    });
    
    // Determine error type
    if (e.name === 'RCRD_DSNT_EXIST') {
        // Record doesn't exist
    } else if (e.name === 'INSUFFICIENT_PERMISSION') {
        // Permission issue
    }
    
    // Re-throw if needed
    throw e;
}
⚠️ Always Log Errors

Include: error message, stack trace, record ID, user context. Future you will thank present you!

4. Testing & Debugging

Testing Checklist

Debugging Tools

5. Coding Standards

Script File Headers

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 * 
 * @description Sets default values on Sales Orders
 * @author Your Name
 * @since 2024.1
 */

Naming Conventions

ElementConventionExample
Script Filestype_record_purpose.jsue_salesorder_validation.js
Script IDscustomscript_company_purposecustomscript_acme_so_validation
FunctionscamelCasecalculateDiscount()
ConstantsUPPER_SNAKEMAX_DISCOUNT_PCT

🎯 Key Takeaways

  • Monitor governance - use getRemainingUsage()
  • Use lookupFields() over load() when just reading
  • Never run searches inside loops
  • Always implement try/catch with detailed logging
  • Test all scenarios including edge cases
  • Follow consistent naming conventions
  • Test in Sandbox before deploying to Production