How to write code for Server Scripts and Jobs?
Check for the following while writing a script: - Scripts are written in JavaScript.
- All code must be contained within a main method. Anything outside this method will not execute.
- The main method is invoked automatically and you do not need to call it.
- You can utilize basic JavaScript methods like Math functions and JSON methods.
- You can also use Helper methods like HTTP request methods, Vtiger’s REST API or Webservice methods, and log methods for debugging.
Following are a few restrictions: - Certain JavaScript methods like setTimeout, setInterval, exec, and eval are restricted due to potential security risks or delays in execution.
- Importing external modules using require or import is restricted for security reasons.
- You cannot define additional functions but can use arrow functions as sub-functions.
Limitations
Following are the limitation for writing server scripts: - Code must be within the main() method; anything outside will not be executed.
- Server script execution has a limit of 15 sec and 32 MB of memory.
- Hourly server jobs can run for up to 90 sec with 128 MB of memory.
- Other server jobs can run for up to 4 mins with 128 MB of memory.
- Scheduled workflows or processes cannot use server scripts.
- Daily execution limits apply to server scripts and jobs. If exceeded, they run the next day or are skipped.
- Avoid using JavaScript functions like require, import, eval, setTimeout, and setInterval.
- Use await instead of .then() to handle promises from request and webservice methods.
- Use try-catch to handle errors from request and webservice methods.
- HTTP requests are only allowed for secure websites (https) with valid SSL certificates.
- Direct IPs, localhost, and Vtiger domains are not allowed for HTTP requests, except the CRM instance URL where the script is invoked.