Understanding CLI in custom functions

Understand Command Line Interface

Table of Contents

1. In a nutshell
2. Availability
3. Overview
    3.1. How CLI is useful
    3.2. Syntax
    3.3. Examples
    3.4. Prerequisites
4. Use cases
5. Related topics

1. In a nutshell

A function is a set of statements grouped together, which can be invoked within your application whenever required. Commands and scripts are customizable sets of instructions that you can create to execute specific tasks on your machines. They can vary from simple single-line commands to complex scripts written in Python, Bash, PowerShell, and other scripting languages. 
In ME AppCreator, you can use CLI (Command Line Interface) functions in Deluge scripts to trigger custom functions written in other languages (Java, Python).

2. Availability

Using CLI in custom functions:

  • Is available only in the On-Premise version of AppCreator
  • Can only be used by the super admin, admins, and developers, while other users invoke them as configured

3. Overview

CLI can be leveraged to use cloud functions to structure Deluge scripts in a modular way and perform tasks that are not possible or difficult to achieve using Deluge. Here, you can directly trigger Java or Python functions from a shell/batch script. The script communicates with the Java or Python interpreter to run specific functionality or programs.

Note

  • It is recommended to avoid executing high-processing or time-consuming functions in CLI scripts.
  • Only alphanumerics and "." are supported in arguments.

3.1 How CLI is useful?

  • Automates running code from the command line
  • Enables integration into other programming languages
  • Simplifies deployment by combining language-specific logic with shell commands

3.2 Syntax

response = executeShellScript
[
    file:"script.sh"
    arguments :"arg1, arg2, arg3"
];
info response.get("responseText");

Here,

  • script.sh is the file to be executed and must be stored in appcreator/custom_scripts.

    Note: Linux installation supports only .sh files and windows installation will support only .bat files.
  • The argument parameter supports only strings that can contain comma separated entities. The custom script will be executed based on the arguments passed to the script.

Note

  • Users can run scripts of any languages (Java, Python, Ruby, etc.,) from a .bat/.sh file.
  • You need to replace 'script.sh' with the filename of the shell or batch script you want to execute.

Syntax for executing the shell script from Linux machine:

1) Without passing any arguments to the script file:

void deluge()
{
    response = executeShellScript
    [
        file:"script.sh"
    ];
    info response.get("responseText");
}


2) By passing arguments to the script file:

void deluge()
{
    response = executeShellScript
    [
        file:"script.sh"
        arguments :"arg1, arg2, arg3"
    ];
    info response.get("responseText");
}

Syntax for executing the batch script from a Windows machine:

1) Without passing any arguments to the script file:

void deluge()
{
    response = executeShellScript
    [
        file:"script.bat"
    ];
    info response.get("responseText");
}


2) By passing arguments to the script file:

void deluge()
{
    response = executeShellScript
    [
        file:"script.bat"
        arguments :"arg1, arg2, arg3"
    ];
    info response.get("responseText");
}

3.3 Examples

The below example shows how a Deluge script runs a batch script (logFormSubmission.bat) that, in turn, triggers a Java function (FormSubmissionLogger) to execute successfully. The Java function then adds new records to the form_submission.txt file, with the records being passed as arguments.

Deluge script

The below Deluge function executes the batch script: logFormSubmission.bat and passes the arguments to the batch script.

void delugescript()
{
response =
executeShellScript
file:"logFormSubmission.bat"
arguments : "18793, Steve, Completed"
];
info response;
}
Batch script

The batch script triggers the Java function: FormSubmissionLogger and passes the arguments to the Java function.

@echo off
cd ../custom_scripts
java FormSubmissionLogger %1 %2 %3
Java function

This java function logs the records in the txt file (form_submissions.txt).

import java.io.*;
import java.util.*;
- logFormSubmission.bat
> custom_scripts › FormSubmissionLogger.java
FormSubmissionLogger.class
public class FormSubmissionLogger {
public static void main (String[] args) {
if (args.length < 3) {
System.out.println("Invalid input, please try again");
return;
}
String Employeeld = args [0];
String EmployeeName = args [1];
String Status = args [2];
String logEntry = "Employee ID: " + EmployeeId +", Employee Name: " + EmployeeName +
", Status:" + Status + "
try (FileWriter writer = new FileWriter("C: \\Program Files\\
writer.write(logEntry + "\n");
System.out println("Form submission logged successfully: " + logEntry);
} catch (IOException e) {
System.out println("Error writing to log file: " + e.getMessage());
}
}
}
Result

You can now see that the arguments are added as a record in the form_submission.txt file.

Employee project status:
Employee ID: 18793, Employee Name: Steve, Status: Completed

3.4 Prerequisites

Add the below entries to the configuration.properties file in the conf folder inside the AppCreator directory and then restart the server.

com.zoho.dre.statement.executeshellscript.enable=true 
com.zoho.dre.statement.executeshellscript.userName=<systemusername> 
com.zoho.dre.statement.executeshellscript.password=<password>

Note:

  • The username and password are mandatory only for Linux based systems.
  • The password would be encrypted automatically after server restart and would not be displayed as plain text.

4. Use cases

  • Database sync: To sync AppCreator forms with an external databas  for legacy application access.
  • Executing advanced functions - Run custom scripts written in Java, Python, or other languages to extend functionality.
  • Server automation - Automate privileged tasks via command-line operations on the hosted server.
  • Zipping files - Use shell or batch scripts to zip multiple uploaded files in AppCreator.

5. Related Topics

Share this post : FacebookTwitter

Still can't find what you're looking for?

Write to us: appcreator-support@manageengine.com

Back to Top