Code Templates

Introduction

Code Templates in Joget offer a variety of predefined template types for developers to streamline their coding tasks. These templates allow you to quickly generate code snippets in multiple formats, including Bean Shell, CSS, HTML, JavaScript, JSON, SQL, Text, and XML. Each template type is designed to address specific needs within a Joget app, from data binding and form validation to process logic and user permissions.

Plugin information

  • Template Types: Bean Shell, CSS, HTML, JavaScript, JSON, SQL, Text, XML
  • Enhanced Development Speed: Predefined templates help speed up development by providing a starting point for common coding tasks.
  • Versatile Use Cases: Templates cover a wide range of scenarios, from UI customization to backend processing.

Get started

Configure code template properties

Bean Shell templates

Customize logic for form loading, storing, validation, and process actions.

  • AJAX Options Data Store: To dynamically load available options based on dependency value. See Use as Form Ajax Options Data Binder.

    Template:

    import org.joget.apps.app.service.AppUtil;
    import org.joget.commons.util.LogUtil;
    import org.joget.apps.form.model.FormRowSet;
    import org.joget.apps.form.model.FormRow;
    import org.joget.apps.form.service.FormUtil;
     
    /**
    * @param values - Dependency values of the controlling field.
    *
    * @return FormRowSet object which contains one or more FormRow object.
    * All FormRow objects are expected to have "value" and "label" property.
    */
    public FormRowSet load(String[] values) {
        FormRowSet rows = new FormRowSet();
     
        if (values != null && values.length > 0) {
            String dependencyValue = values[0];
     
            // Continue your code here
     
            /* Sample code:
                FormRow option = new FormRow();
                option.setProperty(FormUtil.PROPERTY_VALUE, "sample-value");
                option.setProperty(FormUtil.PROPERTY_LABEL, "sample-label");
                rows.add(option);
            */
        }
          
        return rows;
    }
      
    return load(values);
  • Datalist Action: To execute logic as row action and/or bulk action in the datalist. See Use as Datalist Action
    Template:
    import org.joget.apps.app.service.AppUtil;
    import org.joget.commons.util.LogUtil;
    import javax.servlet.http.HttpServletRequest;
    import org.joget.apps.datalist.model.DataList;
    import org.joget.apps.datalist.model.DataListActionResult;
     
    /**
    * @param httpRequest - HTTP Request object of current HTTP Request.
    * @param dataList - Datalist object of the current datalist
    * @param rowKeys - A String array of record ID(s) for datalist row(s) that is selected for this action
    *
    * @return DataListActionResult object contains result for datalist to perform next
    */
    public DataListActionResult executeAction(HttpServletRequest httpRequest, DataList dataList, String[] rowKeys) {
        DataListActionResult result = new DataListActionResult();
        result.setType(DataListActionResult.TYPE_REDIRECT);
        result.setUrl("REFERER");
         
        if (rowKeys != null && rowKeys.length > 0) {
            for (String key : rowKeys) {
                 
                // Continue your code here
                 
                /* Sample code:
                    System.out.println(key);
                */
            }
        }
          
        return result;
    }
      
    return executeAction(httpRequest, datalist, rowKeys);
  • Datalist Formatter: To transform values in the datalist column. See Use as Datalist Formatter
    Template:
    import org.joget.apps.app.service.AppUtil;
    import org.joget.commons.util.LogUtil;
    import org.joget.apps.datalist.model.DataList;
    import org.joget.apps.datalist.model.DataListColumn;
    import org.joget.apps.datalist.service.DataListService;
     
    /**
    * @param dataList - Datalist object of the current datalist
    * @param column - The current datalist column object
    * @param row - The data object of current record row in the datalist.
    * @param value - The value of current cell in the datalist.
    *
    * @return String formatted string value to replace the original cell value
    */
    public String format(DataList dataList, DataListColumn column, Object row, Object value) {
        String formattedValue = "";
        if (value != null) {
            formattedValue = value.toString();
        }
     
        // Continue your code here
     
        /* Sample code:
            Object otherColumnValue = DataListService.evaluateColumnValueFromRow(row, "columnId");
        */
          
        return formattedValue;
    }
      
    return format(datalist, column, row, value);
  • Form Load Data Store: To dictate how form data should be loaded. See Use as Form Load Binder.
    Template:
    import org.joget.apps.app.service.AppUtil;
    import org.joget.commons.util.LogUtil;
    import org.joget.apps.form.model.Element;
    import org.joget.apps.form.model.FormData;
    import org.joget.apps.form.model.FormRow;
    import org.joget.apps.form.model.FormRowSet;
    import org.joget.apps.form.service.FormUtil;
        
    /**
    * @param element - Element that this binder is tie to
    * @param primaryKey - The primary key provided by the element to load data
    * @param formData - The data holder of the whole form
    *
    * @return FormRowSet object which contains one FormRow object.
    */ 
    public FormRowSet load(Element element, String primaryKey, FormData formData) {
        FormRowSet rows = new FormRowSet();
        if (primaryKey != null && !primaryKey.isEmpty()) {
            FormRow row = new FormRow();
            rows.add(row);
     
            // Continue your code here
     
            /* Sample code:
                row.setId(primaryKey);
                row.setProperty("fieldId", "field value");
            */
        }
        return rows;
    }
     
    return load(element, primaryKey, formData);
  • Form Options Data Store: To populate a form element's options based on their own requirements and sources. See Use as Form Options Binder. 
    Template:
    import org.joget.apps.app.service.AppUtil;
    import org.joget.commons.util.LogUtil;
    import org.joget.apps.form.model.Element;
    import org.joget.apps.form.model.FormData;
    import org.joget.apps.form.model.FormRowSet;
    import org.joget.apps.form.model.FormRow;
    import org.joget.apps.form.service.FormUtil;
     
    /**
    * @param element - Element that this binder is tie to
    * @param formData - The data holder of the whole form
    *
    * @return FormRowSet object which contains one or more FormRow object.
    * All FormRow objects are expected to have "value" and "label" property.
    */
    public FormRowSet load(Element element, FormData formData) {
        FormRowSet rows = new FormRowSet();
     
        // Continue your code here
     
        /* Sample code:
            FormRow option = new FormRow();
            option.setProperty(FormUtil.PROPERTY_VALUE, "sample-value");
            option.setProperty(FormUtil.PROPERTY_LABEL, "sample-label");
            rows.add(option);
        */
          
        return rows;
    }
      
    return load(element, formData);
  • Form Store Data Store: To dictate how form data should be stored. See Use as Form Store Binder.
  • Template:
    import org.joget.apps.app.service.AppUtil;
    import org.joget.commons.util.LogUtil;
    import org.joget.apps.form.model.Element;
    import org.joget.apps.form.model.FormData;
    import org.joget.apps.form.model.FormRow;
    import org.joget.apps.form.model.FormRowSet;
    import org.joget.apps.form.service.FormUtil;
    import org.joget.apps.form.model.FormStoreBinder;
    import org.joget.plugin.base.PluginManager;
        
    /**
    * @param element - Element that this binder is tie to
    * @param rows - Data to be store. Contains only one FormRow object
    * @param formData - The data holder of the whole form
    *
    * @return FormRowSet object which stored.
    */ 
    public FormRowSet store(Element element, String rows, FormData formData) {
         
        // Continue your code here
     
        /* Sample code:
            //Reuse Workflow Form Binder to store data
            PluginManager pluginManager = (PluginManager) AppUtil.getApplicationContext().getBean("pluginManager");
            FormStoreBinder binder = (FormStoreBinder) pluginManager.getPlugin("org.joget.apps.form.lib.WorkflowFormBinder");
            binder.store(element, rows, formData);
        */
     
        return rows;
    }
     
    return store(element, rows, formData);
  • Form Validator: To validate the value of a form element. See Use as Form Validator.
    Template:
    import org.joget.apps.app.service.AppUtil;
    import org.joget.commons.util.LogUtil;
    import org.joget.apps.form.model.Element;
    import org.joget.apps.form.model.Form;
    import org.joget.apps.form.model.FormData;
    import org.joget.apps.form.service.FormUtil;
     
    /**
    * @param element - Element that this validator is tie to.
    * @param formData - The data holder of the whole form.
    * @param values - The submitted values of the element.
    *
    * @return boolean value to indicate the validation pass or fail.
    */ 
    public boolean validate(Element element, FormData formData, String[] values) {
        boolean result = true;
      
        // Continue your code here
     
        /* Sample code:
            if (values == null || values.length == 0 || values[0].isEmpty()) {
                String id = FormUtil.getElementParameterName(element);
                formData.addFormError(id, "Value cannot be empty!!");
                result = false;
            }
        */
      
        return result;
    }
      
    return validate(element, formData, values);
  • Multirow Form Load Data Store: To dictate how form data in a grid-type element should be loaded. See Use as Form Multi Row Load Binder.
    Template:
    import org.joget.apps.app.service.AppUtil;
    import org.joget.commons.util.LogUtil;
    import org.joget.apps.form.model.Element;
    import org.joget.apps.form.model.FormData;
    import org.joget.apps.form.model.FormRow;
    import org.joget.apps.form.model.FormRowSet;
    import org.joget.apps.form.service.FormUtil;
        
    /**
    * @param element - Element that this binder is tie to
    * @param primaryKey - The primary key provided by the element to load data
    * @param formData - The data holder of the whole form
    *
    * @return FormRowSet object which contains one or more FormRow object.
    */ 
    public FormRowSet load(Element element, String primaryKey, FormData formData) {
        FormRowSet rows = new FormRowSet();
        rows.setMultiRow(true);
     
        if (primaryKey != null && !primaryKey.isEmpty()) {
            FormRow row = new FormRow();
            rows.add(row);
     
            // Continue your code here
     
            /* Sample code:
                row.setId(primaryKey);
                row.setProperty("fieldId", "field value");
            */
        }
        return rows;
    }
     
    return load(element, primaryKey, formData);
  • Multirow Form Store Data Store: To dictate how form data in a grid-type element should be stored. See Use as Form Multi Row Store Binder.
    Template:
    import org.joget.apps.app.service.AppUtil;
    import org.joget.commons.util.LogUtil;
    import org.joget.apps.form.model.Element;
    import org.joget.apps.form.model.FormData;
    import org.joget.apps.form.model.FormRow;
    import org.joget.apps.form.model.FormRowSet;
    import org.joget.apps.form.service.FormUtil;
    import org.joget.apps.form.model.FormStoreBinder;
    import org.joget.plugin.base.PluginManager;
        
    /**
    * @param element - Element that this binder is tie to
    * @param rows - Data to be store. Contains only one or more FormRow object
    * @param formData - The data holder of the whole form
    *
    * @return FormRowSet object which stored.
    */ 
    public FormRowSet store(Element element, String rows, FormData formData) {
         
        if (rows != null && !rows.isEmpty()) {
     
            // Continue your code here
     
        }
     
        return rows;
    }
     
    return store(element, rows, formData);
  • Multirow Form Validator: To validate the value of a grid-type form element. See Use as Form Multi Row Validator.
    Template:
    import org.joget.apps.app.service.AppUtil;
    import org.joget.commons.util.LogUtil;
    import org.joget.apps.form.model.Element;
    import org.joget.apps.form.model.Form;
    import org.joget.apps.form.model.FormData;
    import org.joget.apps.form.service.FormUtil;
     
    /**
    * @param element - Element that this validator is tie to.
    * @param rows - Submitted data. Contains one or more FormRow object.
    * @param formData - The data holder of the whole form.
    *
    * @return boolean value to indicate the validation pass or fail.
    */ 
    public boolean validate(Element element, FormRowSet rows, FormData formData) {
        boolean result = true;
      
        // Continue your code here
     
        /* Sample code:
            if (rows == null || rows.isEmpty()) {
                String id = FormUtil.getElementParameterName(element);
                formData.addFormError(id, "Value cannot be empty!!");
                result = false;
            }
        */
      
        return result;
    }
      
    return validate(element, rows, formData);
  • Permission: To define who is authorized to access a component of an app. See Use as Form Permission and Use as Userview Permission.
    Template:
    import org.joget.apps.app.service.AppUtil;
    import org.joget.commons.util.LogUtil;
    import java.util.Map;
    import org.joget.directory.model.User;
     
    /**
    * @param user - User object of current logged in user
    * @param requestParams - Request parameters map of current HTTP Request
    *
    * @return boolean value to indicate the user is authorized.
    */ 
    public boolean isAuthorized(User user, Map requestParams) {
        boolean isAuthorized = true;
     
        // Continue your code here
          
        return isAuthorized;
    }
     
    return isAuthorized(user, requestParams);
  • Process Participant: To assign Joget users to represent a participant in a process. See Use as Process Participant.
    Template:
    import org.joget.apps.app.service.AppUtil;
    import org.joget.commons.util.LogUtil;
    import org.joget.workflow.model.WorkflowActivity;
    import java.util.ArrayList;
    import java.util.Collection;
     
    /**
    * @param workflowActivity - Workflow Activity that trying to retrieves assignee.
    *
    * @return Collection of username in String to be assign to the Workflow Activity.
    */ 
    public Collection getAssignees(WorkflowActivity activity) {
        Collection assignees = new ArrayList();
     
        // Continue your code here
     
        /* Sample code:
            assignees.add("admin");
        */
          
        return assignees;
    }
     
    return getAssignees(workflowActivity);
  • Process Tool: To execute logic as a Process Tool in a process. See Use as Process Tool.
    Template:
    import org.joget.apps.app.service.AppUtil;
    import org.joget.commons.util.LogUtil;
    import org.joget.apps.app.model.AppDefinition;
    import javax.servlet.http.HttpServletRequest;
    import org.joget.workflow.model.WorkflowAssignment;
     
    /**
    * @param workflowAssignment - The workflow tool activity assignment object.
    * @param appDef - App definition of the process.
    * @param request - Http Request object of current HTTP Request. Not available if the tool is trigger by Deadline.
    *
    * @return null
    */ 
    public Object execute(WorkflowAssignment assignment, AppDefinition appDef, HttpServletRequest request) {
         
        // Continue your code here
          
        return null;
    }
     
    return execute(workflowAssignment, appDef, request);
  • Route Decision: To dictate the flow of the process. See Use as Process Route Decision.
    Template:
    import org.joget.apps.app.service.AppUtil;
    import org.joget.commons.util.LogUtil;
    import java.util.Map;
    import org.joget.workflow.model.DecisionResult
     
     
    /**
    * @param processDefId - The process definition id
    * @param processId - The process instance id
    * @param routeId - The current route id
    * @param variables - Workflow variables
    *
    * @return DecisionResult for the process to decide transition to flow next
    */ 
    public DecisionResult getDecision(String processDefId, String processId, String routeId, Map variables) {
        DecisionResult result = new DecisionResult();
         
        // Continue your code here
     
        /* Sample code:
            result.addTransition("approved");
        */
     
        return result;
    }
      
    return getDecision(processDefId, processId, routeId, variables);

CSS template

Style your Joget app's interface using custom CSS rules.

div#container {
    display: block;
    color: red;
}
 
p.container-text {
    color: blue;
}

HTML template

Structure your app's content with custom HTML markup.

<div id="container">
    <p class="container-text">I am a sample text inside a container.</p>
</div>

JavaScript template

Add dynamic behavior to your app with jQuery or plain JavaScript.

$(function(){
    // Continue your code here
 
    /* Sample code:
        $("span").css("color", "red");
    */
});

JSON template

Define data structures and API responses using JSON.

{
   "eBooks":[
      {
         "language":"Python",
         "edition":"two"
      },
      {
         "language":"SQL",
         "edition":"three"
      },
      {
         "language":"Java",
         "edition":"four"
      }
   ]
}

SQL templates

Write and execute SQL queries, including these statements:

  • SELECT: Write SELECT statement
    Template:
    SELECT column1, column2, ...
    FROM table_name
    WHERE condition
  • INSERT: Write INSERT statement
    Template:
    INSERT INTO table_name (column1, column2, ...)
    VALUES (value1, value2, ...)
  • UPDATE: Write UPDATE statement
    Template:
    UPDATE table_name
    SET column1 = value1, column2 = value2, ...
    WHERE condition
  • DELETE: Write DELETE statement
    Template:
    DELETE FROM table_name WHERE condition

Text template

Use plain text for notes or documentation within your app.

XML template

Structure complex data using XML.

<eBooks>
    <element>
       <language>Python</language>
       <edition>two</edition>
    </element>
    <element>
       <language>SQL</language>
       <edition>three</edition>
    </element>
    <element>
       <language>Java</language>
       <edition>four</edition>
    </element>
</eBooks>

Related documentation

Created by Julieth Last modified by Aadrian on Nov 19, 2024