📑 In This Module
🎯 Learning Objectives
- Manage governance effectively
- Optimize script performance
- Implement proper error handling
- Apply debugging best practices
- Follow coding standards
1. Governance Management
Every script has governance limits. Running out means your script stops!
| Script Type | Governance Limit |
|---|---|
| Client Script | 1,000 units |
| User Event | 1,000 units |
| Scheduled | 10,000 units |
| Map/Reduce | Automatic (per stage) |
| Suitelet | 1,000 units |
| RESTlet | 5,000 units |
Common Operation Costs
| Operation | Units |
|---|---|
| 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 ofrecord.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
- Test with different user roles
- Test create, edit, view, copy, delete scenarios
- Test with empty/null field values
- Test edge cases (max values, special characters)
- Test in Sandbox before Production
Debugging Tools
- Execution Log: View log.debug/audit/error output
- Script Debugger: Step through server-side code
- Browser DevTools: Debug client scripts (F12)
- Governance Monitoring: Log remaining units
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
| Element | Convention | Example |
|---|---|---|
| Script Files | type_record_purpose.js | ue_salesorder_validation.js |
| Script IDs | customscript_company_purpose | customscript_acme_so_validation |
| Functions | camelCase | calculateDiscount() |
| Constants | UPPER_SNAKE | MAX_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