What is it for?

What is the code syntax?

Usages

Use as Form Load Binder

Injected Variables:

Expected Outcome:

Samples:

Load user data using jdbc.

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.joget.apps.app.service.AppUtil;
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.commons.util.LogUtil;
public FormRowSet load(Element element, String username, FormData formData) { 
    FormRowSet rows = new FormRowSet();
    if (username != null && !username.isEmpty()) {
        Connection con = null;
        try {
            // retrieve connection from the default datasource
            DataSource ds = (DataSource)AppUtil.getApplicationContext().getBean("setupDataSource");
            con = ds.getConnection();
         
            // execute SQL query
            if(!con.isClosed()) {
                PreparedStatement stmt = con.prepareStatement("SELECT username, firstName, lastName, email from dir_user where username=?");
                stmt.setObject(1, username);
                ResultSet rs = stmt.executeQuery();
                while (rs.next()) {
                    FormRow row = new FormRow();
                    System.out.println(rs.getObject("username") );
                    row.setProperty("username", (rs.getObject("username") != null)?rs.getObject("username").toString():"");
                    row.setProperty("firstName", (rs.getObject("firstName") != null)?rs.getObject("firstName").toString():"");
                    row.setProperty("lastName", (rs.getObject("lastName") != null)?rs.getObject("lastName").toString():"");
                    row.setProperty("email", (rs.getObject("email") != null)?rs.getObject("email").toString():"");
                    
                    rows.add(row);
                    break;
                }
            }
        } catch(Exception e) {
            LogUtil.error("Sample app - Form 1", e, "Error loading user data in load binder");
        } finally {
            //always close the connection after used
            try {
                if(con != null) {
                    con.close();
                }
            } catch(SQLException e) {/* ignored */}
        }
    }
    return rows;
}
//call load method with injected variable
return load(element, primaryKey, formData);

Use as Form Options Binder

Injected Variables:

Expected Outcome:

Samples:

Load time zone as the select box options.

import java.util.Map;
import org.joget.apps.app.service.AppUtil;
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.commons.util.TimeZoneUtil;

public FormRowSet load(Element element, String username, FormData formData) { 
    FormRowSet rows = new FormRowSet();
    
    //Get timezones using timezone util
    for(Map.Entry entry : TimeZoneUtil.getList().entrySet()){
        FormRow option = new FormRow();
        option.setProperty(FormUtil.PROPERTY_VALUE, (String) entry.getKey());
        option.setProperty(FormUtil.PROPERTY_LABEL, (String) entry.getValue());
        rows.add(option);
    }
    
    return rows;
}

//call load method with injected variable
return load(element, primaryKey, formData);

Use as Form Ajax Options Binder

Injected Variables:

Expected Outcome:

Samples:

Use as Form Store Binder

Injected Variables:

Expected Outcome:

Samples:

Use as Form Validator

Injected Variables:

Expected Outcome:

Samples:

Use as Form Multi Row Load Binder

Injected Variables:

Expected Return Object:

Samples:

Use as Form Multi Row Store Binder

Injected Variables:

Expected Return Object:

Samples:

Use as Form Multi Row Validator

Injected Variables:

Expected Return Object:

Samples:

Use as Form Permission

Injected Variables:

Expected Return Object:

Samples:

Use as Form Post Submission Processing Tool

Injected Variables:

Expected Return Object:

Samples:

Use as Process Participant

Injected Variables:

Expected Return Object:

Samples:

Use as Process Tool 

Injected Variables:

Expected Return Object:

Samples:

Use as Userview Permission

Injected Variables:

Expected Return Object:

Samples:

 

Best Practices

 

1. Only import classes that are needed

Do not use wildcard in import statement. It giving a very bad performance in Bean Shell interpreter to search the whole package and loads it in memory.

Don't:

import java.util.*;

Do:

import java.util.Collection;

2. Do not need to mention the type of element of a collections

Bean Shell interpreter cannot recognise the element type syntax of collections class like Collection, Map, List, Set and etc.

Don't:

Map<String, String> map = new HashMap<String, String>();

Do:

Map map = new HashMap();

3. Indents your script nicely and follows the Java Code Conventions

 

It will make yours and others life easier to maintain and review the script whenever necessary as Joget Workflow provided flexibility to adapt change quickly.

4. Write your script in functions

If your script is pretty long and some parts of the script are reusable, please make use of function to write your script. It provided better reading and performance.

5. Remember to write some comments 

It will helps you and others to understand what is the purpose for the script quickly. 

6. Catch the exceptions and give a meaningful message in log.

If you are using a lot of Bean Shell Scripting in your app, a meaningful log message can help you to locate your issue quickly.

Don't

try {
    //do something
} catch (Exception e) {
    LogUtil.error("BeanShell", e, "Error executing script");
}

Do:

try {
    //do something
} catch (Exception e) {
    LogUtil.error("CRM app - Backend userview", e, "Error retrieving user department in Report category permission");
}

More samples