Business Script Execution and Exit Code Control


Please refer to getExitCode for more information.


How to Use

Introduction:

This example demonstrates a complete business script execution flow, including step execution, condition checks, and returning explicit business exit codes via exit(code).
Using “Login → Validate → Return Business Code” as an example, the script shows how to exit at different stages based on execution results, providing external tasks or terminals with a clear and actionable execution status.

Before Running:

  1. Download and install Total Control 11.0 (Update 20) or later (Download)
  2. Save the example code as a .js file (e.g., TestGetExitCode.js) and place it in the Total Control default script directory
  3. Open the Script Terminal (Main Panel → Script → Terminal) and execute:
    >> sigmaLoad("TestGetExitCode.js");
    


Source Code

/**
 * business_login_demo.js
 * Objective: Demonstrate how to set exit(code) in a business script
 */

// Business Exit Codes (Example)
var EXIT_OK = 0;
var EXIT_BAD_ARGS = 10;
var EXIT_ENV_NOT_READY = 20;
var EXIT_VALIDATE_FAIL = 30;
var EXIT_DEP_FAILED = 40;
var EXIT_UNKNOWN = 50;

function fail(code, msg) {
	print("[FAIL] code=" + code + " msg=" + msg);
	exit(code);
}

function main() {
	// 1) Parameter validation (example)
	var user = "demo_user"; // Typically from parameters or config
	if (!user) return fail(EXIT_BAD_ARGS, "missing user");

	// 2) Environment check (example: network/device/preconditions)
	var envOk = true; // Replace with actual checks in real scenarios
	if (!envOk) return fail(EXIT_ENV_NOT_READY, "env not ready");

	// 3) Business step (example: login)
	print("[STEP] login start...");
	var loginOk = true; // Replace with the actual login result
	if (!loginOk) return fail(EXIT_DEP_FAILED, "login api failed");

	// 4) Business validation (example: check login status)
	print("[STEP] validate session...");
	var validateOk = true; // Replace with actual validation
	if (!validateOk) return fail(EXIT_VALIDATE_FAIL, "session invalid");

	// 5) Success
	print("[OK] business done");
	exit(EXIT_OK);
}

try {
	main();
} catch (e) {
	print("[EXCEPTION] " + e);
	exit(EXIT_UNKNOWN);
}

Execution Result

[STEP] login start...
[STEP] validate session...
[OK] business done

Continue by executing the following code in Terminal

>> getExitCode()

Execution Result

0

TCHelp