Best Practices to Avoid Recursion
Check the Execution Context
- Use
context.executionContextto determine the source of the script trigger. This allows you to skip execution in unwanted contexts like scheduled scripts or CSV imports.
Use Flag Fields to Prevent Re-entry
- Implement a custom checkbox or hidden field that acts as a flag to indicate that certain logic has already run. This can prevent the same logic from executing again on the same record.
Avoid Unnecessary Record Saves Inside UES
- Avoid calling
record.save()within a User Event script unless absolutely necessary, especially inafterSubmit. Instead, usesubmitFields()for lightweight updates.
Delegate Complex Logic to Scheduled Scripts
- If the script logic involves creating or updating other records, consider offloading that logic to a scheduled or Map/Reduce script to decouple processing from the transaction flow.
Use Clear Conditional Logic
- Write explicit conditions to control when parts of your script should execute, based on the event type (
create,edit,delete) or field values
Best Practices for Performance Optimization
Avoid Full Record Loads When Possible
- Use
submitFields()orgetValue()to read or update only specific fields. Full record loads (record.load()) are costly in terms of performance and governance.
Minimize Search Operations Inside Scripts
- Avoid executing searches inside User Event scripts unless absolutely required. Use efficient search filters and pagination methods (
runPaged) to handle large datasets.
Avoid Long Processing Times
- Time-intensive logic should be moved to deferred processes like scheduled or Map/Reduce scripts to avoid timeout issues.
Split Logic into Functions for Clarity and Reuse
- Modularizing your logic into smaller functions improves maintainability and performance tracking.