Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents
maxLevel3

What is it for?

Thai

มีไว้เพื่ออะไร?

  • Joget Workflow provided Bean Shell implementation as several Plugin Types. Please refer to usages section.

    Thai

    Joget Workflow ให้การใช้งาน Bean Shell เป็นปลั๊กอินหลายประเภท โปรดดู usages

  • BeanShell is a small, embeddable Java source interpreter with object scripting language features written in Java.

    Thai

    BeanShell เป็นล่าม Java ขนาดเล็กที่สามารถฝังตัวได้พร้อมคุณสมบัติภาษาสคริปต์วัตถุที่เขียนด้วยภาษาจาวา

  • BeanShell dynamically executes standard Java syntax in runtime.

    Thai

    BeanShell เรียกใช้งานไวยากรณ์ Java มาตรฐานแบบไดนามิกในรันไทม์

  • By using BeanShell Plugin, you can type in valid Java codes in plugin configuration and the statements will be executed when the plugin is triggered.

    Thai

    โดยใช้ BeanShell Plugin คุณสามารถพิมพ์รหัส Java ที่ถูกต้องในการกำหนดค่าปลั๊กอินและคำสั่งจะถูกดำเนินการเมื่อมีการเรียกใช้ปลั๊กอิน

  • No compilation cycle is needed.

    Thai

    ไม่จำเป็นต้องมีวงจรการรวบรวม

What is the code syntax?

Thai

ไวยากรณ์ของรหัสคืออะไร

  • Java syntax supported by the version of JDK used

    Thai

    ไวยากรณ์ Java รองรับโดยรุ่นของ JDK ที่ใช้

  • Usage of all libraries that Joget used. For details please refer to NOTICE.txt in the release installer/bundle.

    Thai

    การใช้งานไลบรารีทั้งหมดที่ Joget ใช้ สำหรับรายละเอียดโปรดดูที่ NOTICE.txt ในโปรแกรมติดตั้ง / ชุดรวม

  • Usage of all Joget Workflow Utility and Service Methods.

    Thai

    การใช้ยูทิลิตี้ Joget Workflow Utility and Service Methods

  • Usage of Hash Variable.

    Thai

    การใช้ Hash Variable

Usages

Thai

ตามธรรมเนียม

Use as Form Load Binder
Anchor
Form Load Binder
Form Load Binder

Thai

ใช้เป็นโหลดแบบฟอร์ม Binder

Injected Variables:

Thai

ตัวแปรที่ฉีด:

    • element - Element that this binder is tie to. (org.joget.apps.form.model.Element)

      Thai

      องค์ประกอบ - องค์ประกอบที่สารยึดเกาะนี้จะผูกกับ (org.joget.apps.form.model.Element)

    • primaryKey - The primary key provided by the element to load data. (java.lang.String)

      Thai

      primaryKey - คีย์หลักที่ได้รับจากองค์ประกอบในการโหลดข้อมูล (java.lang.String)

    • formData - The data holder of the whole form. (org.joget.apps.form.model.FormData)

      Thai

      formData - ผู้ถือข้อมูลของทั้งฟอร์ม (org.joget.apps.form.model.FormData)

Expected Return Object::

Thai

วัตถุส่งคืนที่คาดหวัง:

    • An org.joget.apps.form.model.FormRowSet object which contains one org.joget.apps.form.model.FormRow object. 

      Thai

      วัตถุ

      An

      org.joget.apps.form.model.FormRowSet

      object which contains one

      ซึ่งมีหนึ่ง org.joget.apps.form.model.FormRow

      object. 

      หนึ่งวัตถุ

Samples:

Thai

ตัวอย่าง:

Load user data using jdbc.

Thai

โหลดข้อมูลผู้ใช้โดยใช้ jdbc

Code Block
languagejava
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

Thai

ใช้เป็น Binder ตัวเลือกแบบฟอร์ม

Injected Variables:

Thai

ตัวแปรที่ฉีด:

    • element - Element that this binder is tie to. (org.joget.apps.form.model.Element)
    • primaryKey - The primary key provided by the element to load data. (java.lang.String)
    • formData - The data holder of the whole form. (org.joget.apps.form.model.FormData)

Expected Return Object:

Thai

วัตถุส่งคืนที่คาดหวัง:

    • An org.joget.apps.form.model.FormRowSet object which contains one or more org.joget.apps.form.model.FormRow object. All FormRow objects are expected to have "value" and "label" property.

      Thai

      วัตถุ org.joget.apps.form.model.FormRowSet ซึ่งมีวัตถุ org.joget.apps.form.model.FormRow หนึ่งรายการขึ้นไป วัตถุ FormRow ทั้งหมดคาดว่าจะมีคุณสมบัติ "value" และ "label"

Samples:

Thai

ตัวอย่าง:

Load time zone as the select box options.

Thai

โหลดเขตเวลาเป็นตัวเลือกของกล่องที่เลือก

Code Block
languagejava
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 primaryKey, 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

Thai

ใช้เป็น Binder ตัวเลือก Ajax ของฟอร์ม

Injected Variables:

Thai

ตัวแปรที่ฉีด:

    • values - Dependency values of the controlling field

      .

      . (java.lang.String[])

      Thai

      ค่า - ค่าการพึ่งพาของเขตข้อมูลการควบคุม (java.lang.String [])

Expected Return Object:

Thai

วัตถุส่งคืนที่คาดหวัง:

    • An org.joget.apps.form.model.FormRowSet object which contains one or more org.joget.apps.form.model.FormRow object. All FormRow objects are expected to have "value" and "label" property.

      Thai

      วัตถุ org.joget.apps.form.model.FormRowSet ซึ่งมีวัตถุ org.joget.apps.form.model.FormRow หนึ่งรายการขึ้นไป วัตถุ FormRow ทั้งหมดคาดว่าจะมีคุณสมบัติ "value" และ "label"

Samples:

Thai

ตัวอย่าง:

Load user as options based on the group id passed by the controlling field.

Thai

โหลดผู้ใช้เป็นตัวเลือกตามรหัสกลุ่มที่ส่งผ่านโดยฟิลด์ควบคุม

Code Block
languagejava
import java.util.Collection;
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.directory.model.User;
import org.joget.directory.model.service.ExtDirectoryManager;

public FormRowSet load(String[] values) { 
    FormRowSet rows = new FormRowSet();
    
    ExtDirectoryManager directoryManager = (ExtDirectoryManager) AppUtil.getApplicationContext().getBean("directoryManager");
    
    //set groupId based on dependency value
    String groupId = null;
    if (values != null && values.length > 0) {
        groupId = values[0];
    }
    
    //Get users using directory manager
    Collection userList = directoryManager.getUsers(null, null, null, null, groupId, null, null, "firstName", false, null, null);
    for(Object u : userList){
        User user = (User) u;
        FormRow option = new FormRow();
        option.setProperty(FormUtil.PROPERTY_VALUE, user.getUsername());
        option.setProperty(FormUtil.PROPERTY_LABEL, user.getFirstName() + " " + user.getLastName());
        rows.add(option);
    }
    
    return rows;
}

//call load method with injected variable
return load(values);

Use as Form Store Binder

Thai

ใช้เป็นเครื่องผูกสำหรับเก็บฟอร์ม

Injected Variables:

Thai

ตัวแปรที่ฉีด:

    • element - Element that this binder is tie to. (org.joget.apps.form.model.Element)

      Thai

      องค์ประกอบ - องค์ประกอบที่สารยึดเกาะนี้จะผูกกับ (org.joget.apps.form.model.Element)

    • rows - Data to be store. Contains only one org.joget.apps.form.model.FormRow object

      .

      . (org.joget.apps.form.model.FormRowSet)

      Thai

      rows - ข้อมูลที่จะจัดเก็บ มีวัตถุ org.joget.apps.form.model.FormRow เดียวเท่านั้น (org.joget.apps.form.model.FormRowSet)

    • formData - The data holder of the whole form. (org.joget.apps.form.model.FormData)

      Thai

      formData - ผู้ถือข้อมูลของทั้งฟอร์ม (org.joget.apps.form.model.FormData)

Expected Return Object:

Thai

วัตถุส่งคืนที่คาดหวัง:

    • Same org.joget.apps.form.model.FormRowSet object which stored.

      Thai

      วัตถุ org.joget.apps.form.model.FormRowSet เดียวกันซึ่งเก็บไว้

Samples:

Thai

ตัวอย่าง:

Do some calculation before storing the data using Workflow Form Binder.

Thai

ทำการคำนวณก่อนที่จะเก็บข้อมูลโดยใช้ Binder ของฟอร์มงาน

Code Block
languagejava
import java.text.DecimalFormat;
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.model.FormStoreBinder;
import org.joget.plugin.base.PluginManager;
 
public FormRowSet store(Element element, FormRowSet rows, FormData formData) {
    
    //check the rows is not empty before store it
    if (rows != null && !rows.isEmpty()) {
        //Get the submitted data
        FormRow row = rows.get(0);
        
        String unitPrice = row.getProperty("unitPrice");
        String unit = row.getProperty("unit");
        
        double total = 0;
        try {
            double price = Double.parseDouble(unitPrice);
            int unit = Integer.parseInt(unit);
            
            total = price * unit;
        } catch (Exception e) {}
        
        //format the total to 2 decimal
        DecimalFormat df = new DecimalFormat("#.00");
        row.setProperty("total", df.format(total));
    
        //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;
}

//call store method with injected variable
return store(element, rows, formData);

Use as Form Validator

Thai

ใช้เป็นเครื่องมือตรวจสอบแบบฟอร์ม

Injected Variables:

Thai

ตัวแปรที่ฉีด:

    • element - Element that this validator is tie to. (org.joget.apps.form.model.Element)

      Thai

      องค์ประกอบ - องค์ประกอบที่ตัวตรวจสอบนี้จะผูกกับ (org.joget.apps.form.model.Element)

    • values - The submitted values of the element. (java.lang.String[])

      Thai

      ค่า - ค่าส่งขององค์ประกอบ (java.lang.String [])

    • formData - The data holder of the whole form. (org.joget.apps.form.model.FormData)

      Thai

      formData - ผู้ถือข้อมูลของทั้งฟอร์ม (org.joget.apps.form.model.FormData)

Expected Return Object:

Thai

วัตถุส่งคืนที่คาดหวัง:

    • A boolean value to indicate the validation pass or fail.

      Thai

      เปรียบเทียบค่าที่ส่งกับค่าของฟิลด์อื่น

Samples:

Thai

ตัวอย่าง:

Compare the submitted value with the value of another field.

Thai

เปรียบเทียบค่าที่ส่งกับค่าของฟิลด์อื่น

Code Block
languagejava
import java.util.Arrays;
import org.joget.apps.app.service.AppUtil;
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;

public boolean validate(Element element, FormData formData, String[] values) {
    boolean result = true;

    //get field 1 value from form data object
    String field1Id = "field1";
    Form form = FormUtil.findRootForm(element);
    Element field1 = FormUtil.findElement(field1Id, form, formData);

    if (field1 != null) {
        //get value of field 1
        String[] compareValues = FormUtil.getElementPropertyValues(field1, formData);

        //compare the value of field 2 and field 1 are equals
        if (!Arrays.equals(values, compareValues)) {
            String id = FormUtil.getElementParameterName(element);
            formData.addFormError(id, "Value not equal!!!!");
            result = false;
        } 
    } else {
        //ignore if the field 1 not exist
    }

    return result; 
}

//call validate method with injected variable
return validate(element, formData, values);

Use as Form Multi Row Load Binder

Thai

ใช้เป็น Load Binder สำหรับโหลดฟอร์มหลายแถว

Injected Variables:

Thai

ตัวแปรที่ฉีด:

    • element - Element that this binder is tie to. (org.joget.apps.form.model.Element)

      Thai

      องค์ประกอบ - องค์ประกอบที่สารยึดเกาะนี้จะผูกกับ (org.joget.apps.form.model.Element)

    • primaryKey - The primary key provided by the element to load data. (java.lang.String)

      Thai

      primaryKey - คีย์หลักที่ได้รับจากองค์ประกอบในการโหลดข้อมูล (java.lang.String)

    • formData - The data holder of the whole form. (org.joget.apps.form.model.FormData)

      Thai

      formData - ผู้ถือข้อมูลของทั้งฟอร์ม (org.joget.apps.form.model.FormData)

Expected Return Object:

Thai

วัตถุส่งคืนที่คาดหวัง:

    • An org.joget.apps.form.model.FormRowSet object which contains one or more org.joget.apps.form.model.FormRow object. 

      Thai

      วัตถุ org.joget.apps.form.model.FormRowSet ซึ่งมีวัตถุ org.joget.apps.form.model.FormRow หนึ่งรายการขึ้นไป

Samples:

Thai

ตัวอย่าง:

Load default grid data from another table if current record does not have any grid data.

Thai

โหลดข้อมูลกริดเริ่มต้นจากตารางอื่นหากบันทึกปัจจุบันไม่มีข้อมูลกริดใด ๆ

Code Block
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.plugin.base.PluginManager;
import org.joget.apps.form.model.FormLoadBinder;

public FormRowSet load(Element element, String primaryKey, FormData formData) {
    String defaultFormDefId = "default_grid_entry"; //change this to the form id used to store default grid data
    String formDefId = "grid_entry";  //change this to the form id used to store grid data
    String foreignKey = "fk"; //change this to the foreign key 
    
    FormRowSet f = new FormRowSet();
    f.setMultiRow(true);

    // Reuse Multi Row Binder to load data
    PluginManager pluginManager = (PluginManager) FormUtil.getApplicationContext().getBean("pluginManager");
    FormLoadBinder binder = (FormLoadBinder) pluginManager.getPlugin("org.joget.plugin.enterprise.MultirowFormBinder");
    
    //Load from the grid table
    binder.setProperty("formDefId", formDefId);
    binder.setProperty("foreignKey", foreignKey);
    f = binder.load(element, primaryKey, formData);
    
    //if no grid data is retrieved, get from default table
    if (f == null || f.isEmpty()) {
        binder.setProperty("formDefId", defaultFormDefId);
        
        //set the foreign key value to empty
        f = binder.load(element, "", formData);
    }
    return f;
}

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

Use as Form Multi Row Store Binder

Thai

ใช้เป็น Binder เก็บฟอร์มหลายแถว

Injected Variables:

Thai

ตัวแปรที่ฉีด:

    • element - Element that this binder is tie to. (org.joget.apps.form.model.Element)

      Thai

      องค์ประกอบ - องค์ประกอบที่สารยึดเกาะนี้จะผูกกับ (org.joget.apps.form.model.Element)

    • rows - Data to be store. Contains one or more org.joget.apps.form.model.FormRow object. (org.joget.apps.form.model.FormRowSet)

      Thai

      rows - ข้อมูลที่จะจัดเก็บ มีวัตถุ org.joget.apps.form.model.FormRow หนึ่งรายการขึ้นไป (org.joget.apps.form.model.FormRowSet)

    • formData - The data holder of the whole form. (org.joget.apps.form.model.FormData)

Expected Return Object:

    • Thai

      formData - ผู้ถือข้อมูลของทั้งฟอร์ม (org.joget.apps.form.model.FormData)

Expected Return Object:

Thai

วัตถุส่งคืนที่คาดหวัง:

    • A org.joget.apps.form.model.FormRowSet object.

      Thai

      วัตถุ

      A

      org.joget.apps.form.model.FormRowSet

      object.

Samples:

Thai

ตัวอย่าง:

Bulk create users based on the grid data.

Thai

กลุ่มผู้ใช้สร้างตามตารางข้อมูล

Code Block
import java.util.HashSet;
import java.util.Set;
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;
import org.joget.commons.util.StringUtil;
import org.joget.directory.model.Role;
import org.joget.directory.model.User;
import org.joget.directory.dao.RoleDao;
import org.joget.directory.dao.UserDao;
import org.joget.directory.model.service.DirectoryUtil;
import org.joget.directory.model.service.UserSecurity;
 
public FormRowSet store(Element element, FormRowSet rows, FormData formData) {
    try {
        UserSecurity us = DirectoryUtil.getUserSecurity();
        RoleDao roleDao = (RoleDao) AppUtil.getApplicationContext().getBean("roleDao");
        UserDao userDao = (UserDao) AppUtil.getApplicationContext().getBean("userDao");
        
        for (FormRow row : rows) {
            //Get the submitted data for each grid row
            String username = row.getProperty("username");
            String firstName = row.getProperty("firstName");
            String lastName = row.getProperty("lastName");
            String email = row.getProperty("email");
            String password = row.getProperty("password");
            
            User user = new User();
            user.setId(username);
            user.setUsername(username);
            user.setTimeZone("0");
            user.setActive(1);
            user.setFirstName(firstName);
            user.setLastName(lastName);
            user.setEmail(email);
            
            //Check if there is user security implementation, using it to encrypt password
            if (us != null) {
                user.setPassword(us.encryptPassword(username, password));
            } else {
                user.setPassword(StringUtil.md5Base16(password));
            }
            user.setConfirmPassword(password);
            
            //set user role
            Set roleSet = new HashSet();
            roleSet.add(roleDao.getRole("ROLE_USER"));
            user.setRoles(roleSet);
            
            userDao.addUser(user);
            if (us != null) {
                us.insertUserPostProcessing(user);
            }
        }
    } catch (Exception e) {
        LogUtil.error("Sample app - Bulk Create Users form", e, "Store user error!!");
    }
    return rows;
}
 
//call store method with injected variable
return store(element, rows, formData);

Use as Form Multi Row Validator

Thai

ใช้เป็นเครื่องมือตรวจสอบหลายแถวของฟอร์ม

Injected Variables:

Thai

ตัวแปรที่ฉีด:

    • element - Element that this validator is tie to. (org.joget.apps.form.model.Element)

      Thai

      องค์ประกอบ - องค์ประกอบที่ตัวตรวจสอบนี้จะผูกกับ (org.joget.apps.form.model.Element)

    • rows - Submitted data. Contains one or more org.joget.apps.form.model.FormRow object. (org.joget.apps.form.model.FormRowSet)

      Thai

      แถว - ข้อมูลที่ส่ง มีวัตถุ org.joget.apps.form.model.FormRow หนึ่งรายการขึ้นไป (org.joget.apps.form.model.FormRowSet)

    • formData - The data holder of the whole form. (org.joget.apps.form.model.FormData)

      Thai

      formData - ผู้ถือข้อมูลของทั้งฟอร์ม (org.joget.apps.form.model.FormData)

Expected Return Object:

Thai

วัตถุส่งคืนที่คาดหวัง:

    • A boolean value to indicate the validation pass or fail.

      Thai

      ค่าบูลีนเพื่อระบุว่าผ่านการตรวจสอบหรือล้มเหลว

Samples:

Thai

ตัวอย่าง:

Validate the sum of a column values are less than 1000..

Thai

ตรวจสอบผลรวมของค่าคอลัมน์น้อยกว่า 1,000

Code Block
import java.util.Arrays;
import org.joget.apps.app.service.AppUtil;
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.model.FormRow;
import org.joget.apps.form.model.FormRowSet;
import org.joget.apps.form.service.FormUtil;
 
public boolean validate(Element element, FormRowSet rows, FormData formData) {
    boolean result = true;
    if (rows != null && !rows.isEmpty()) {
        int total = 0;
        
        //Sum the values from column "amount"
        for (FormRow row : rows) {
            try {
                int amount = Integer.parseInt(row.getProperty("amount"));
                total += amount;
            } catch (Exception e) {}
        }
        
        //if amount larger than 1000
        if (total > 1000) {
            String id = FormUtil.getElementParameterName(element);
            formData.addFormError(id, "Total amount should not larger than 1000!!!!");
            result = false;
        }
    }
 
    return result;
}
 
//call validate method with injected variable
return validate(element, rows, formData);

Use as Form Permission

Thai

ใช้เป็นแบบฟอร์มอนุญาต

Injected Variables:

Thai

ตัวแปรที่ฉีด:

    • user - User object of current logged in user (org.joget.directory.model.User)

      Thai

      ผู้ใช้ - วัตถุผู้ใช้ของผู้ใช้ที่เข้าสู่ระบบในปัจจุบัน (org.joget.directory.model.User)

    • requestParams - Request parameters map of current HTTP Request (java.util.Map)

      Thai

      requestParams - แมปพารามิเตอร์คำขอของคำขอ HTTP ปัจจุบัน (java.util.Map)

Expected Return Object:

Thai

วัตถุส่งคืนที่คาดหวัง:

    • A boolean value to indicate the user is authorized.

      Thai

      ค่าบูลีนเพื่อระบุผู้ใช้ที่ได้รับอนุญาต

Samples:

Thai

ตัวอย่าง:

Check the current user's username are same with the form field "creator" value. The following sample using Form Hash Variable to retrieve form field value.

Thai

ตรวจสอบชื่อผู้ใช้ปัจจุบันของผู้ใช้ว่าเป็นค่าเดียวกันกับค่า "ผู้สร้าง" ในฟิลด์ ตัวอย่างต่อไปนี้ใช้ Form Hash Variable เพื่อดึงค่าฟิลด์ของแบบฟอร์ม

Code Block
import java.util.Map;
import org.joget.directory.model.User;

public boolean isAuthorized(User user, Map params) { 
    //using hash variable to get "creator" field value and escapes it with java syntax, then compare with current username
    return "#form.crm_account.creator?java#".equals(user.getUsername());
}

//call isAuthorized method with injected variable
return isAuthorized(user, requestParams);

Use as Form Post Submission Processing Tool

Thai

ใช้เป็นเครื่องมือประมวลผลการโพสต์การส่งแบบฟอร์ม

Injected Variables:

Thai

ตัวแปรที่ฉีด:

    • workflowAssignment - The workflow activity assignment object of the saving form. Null when the form is not an assignment form. (org.joget.workflow.model.WorkflowAssignment)

      Thai

      workflowAssignment - วัตถุการกำหนดกิจกรรมเวิร์กโฟลว์ของแบบฟอร์มการบันทึก เป็นโมฆะเมื่อแบบฟอร์มไม่ใช่แบบฟอร์มการมอบหมาย (org.joget.workflow.model.WorkflowAssignment)

    • pluginManager - Plugin Manager service bean for

      convenient usage.

      convenient usage. (org.joget.plugin.base.PluginManager)

      Thai

      pluginManager - ปลั๊กอินตัวจัดการปลั๊กอินสำหรับการใช้งานที่สะดวก (org.joget.plugin.base.PluginManager)

    • appDef - App definition of the process. (org.joget.apps.app.model.AppDefinition)

      Thai

      appDef - นิยามแอพของกระบวนการ (org.joget.apps.app.model.AppDefinition)

    • request - Http Request object of current Http Request. (javax.servlet.http.HttpServletRequest)

      Thai

      ขอ - วัตถุคำขอ HTTP การร้องขอ Http ปัจจุบัน (javax.servlet.http.HttpServletRequest)

Expected Return Object:

Thai

วัตถุส่งคืนที่คาดหวัง:

    • None

      Thai

      ไม่มี

Samples:

Thai

ตัวอย่าง:

Reuse Email tool to send separate email to each users. The following script is for a form not mapped to workflow assignment, therefore workflowAssignment is not available.

Thai

ใช้เครื่องมืออีเมลซ้ำเพื่อส่งอีเมลแยกต่างหากให้กับผู้ใช้แต่ละคน สคริปต์ต่อไปนี้ใช้สำหรับฟอร์มที่ไม่ได้แมปกับการกำหนดเวิร์กโฟลว์ดังนั้นจึงไม่สามารถใช้งานเวิร์กโฟลว์การลงชื่อเข้าใช้งานได้

Code Block
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.joget.apps.app.model.AppDefinition;
import org.joget.apps.app.service.AppPluginUtil;
import org.joget.apps.app.service.AppUtil;
import org.joget.plugin.base.ApplicationPlugin;
import org.joget.plugin.base.Plugin;
import org.joget.plugin.base.PluginManager;
import org.joget.plugin.property.model.PropertyEditable;
 
public Object execute(AppDefinition appDef, HttpServletRequest request) {
    String[] emails = new String[]{"test1@joget.org", "test2@joget.org"};
    
    //Reuse Email Tool to send separated email to a list of users;
    Plugin plugin = pluginManager.getPlugin("org.joget.apps.app.lib.EmailTool");
    
    //Get default properties (SMTP setting) for email tool
    Map propertiesMap = AppPluginUtil.getDefaultProperties(plugin, null, appDef, null);
    propertiesMap.put("pluginManager", pluginManager);
    propertiesMap.put("appDef", appDef);
    propertiesMap.put("request", request);
    
    ApplicationPlugin emailTool = (ApplicationPlugin) plugin;
    
    //send email
    for (String email : emails) {
        propertiesMap.put("toSpecific", email);
        propertiesMap.put("subject", "This is a test email for " + email);
        propertiesMap.put("message", "Email content for " + email);
        
        //set properties and execute the tool
        ((PropertyEditable) emailTool).setProperties(propertiesMap);
        emailTool.execute(propertiesMap);
    }
    
    return null;
}
 
//call execute method with injected variable
return execute(appDef, request);

Use as Datalist Action

Thai

ใช้เป็นดาต้าลิสต์แอคชั่น

Injected Variables:

Thai

ตัวแปรที่ฉีด:

    • httpRequest - HTTP Request object of current HTTP Request. (javax.servlet.http.HttpServletRequest)

      Thai

      httpRequest - วัตถุคำขอ HTTP ของคำขอ HTTP ปัจจุบัน (javax.servlet.http.HttpServletRequest)

    • datalist - Datalist object of the current datalist (org.joget.apps.datalist.model.Datalist)

      Thai

      datalist - วัตถุ datalist ของ datalist ปัจจุบัน (org.joget.apps.datalist.model.Datalist)

    • rowKeys - A String array of record ID(s) for datalist row(s) that is selected for this action

      Thai

      rowKeys - อาร์เรย์สตริงของ ID เรคคอร์ด (s) สำหรับแถวดาต้าลิสต์ที่เลือกสำหรับการกระทำนี้

Expected Return Object:

Thai

วัตถุส่งคืนที่คาดหวัง:

    • None

      Thai

      ไม่มี

Samples:

Thai

ตัวอย่าง:

Code Block
languagejava
for (String key : rowKeys) {
	System.out.println("Record key is " + key);
}

Use as Datalist Formatter

Thai

ใช้เป็นฟอร์แมตดาต้าลิสต์

Note:

Thai

บันทึก:

    • The entire script is defaulted to be applied to every row in the specified column, hence it is not needed to perform looping to manually apply formatting to every row.

      Thai

      สคริปต์ทั้งหมดจะมีค่าเริ่มต้นที่จะนำไปใช้กับทุกแถวในคอลัมน์ที่ระบุดังนั้นจึงไม่จำเป็นต้องทำการวนซ้ำเพื่อใช้การจัดรูปแบบกับทุกแถวด้วยตนเอง

    • It is also possible to use hash variable in the returned value.

      Thai

      นอกจากนี้ยังเป็นไปได้ที่จะใช้ตัวแปรแฮชในค่าที่ส่งคืน

Injected Variables:

Thai

ตัวแปรที่ฉีด:

    • datalist - Datalist object of the current datalist (org.joget.apps.datalist.model.Datalist)

      Thai

      datalist - วัตถุ datalist ของ datalist ปัจจุบัน (org.joget.apps.datalist.model.Datalist)

    • column - The current datalist column object (org.joget.apps.datalist.model.DataListColumn)

      Thai

      column - วัตถุคอลัมน์ datalist ปัจจุบัน (org.joget.apps.datalist.model.DataListColumn)

    • row - row Object of current record row in the datalist.
      To retrieve the property value from Object row, use this service method: DataListService.evaluateColumnValueFromRow(Object row, String

       propertyName

       propertyName)

      Thai

      row - row วัตถุของแถวเร็กคอร์ดปัจจุบันใน datalist
      ในการดึงค่าคุณสมบัติจากแถวออบเจกต์ให้ใช้วิธีการบริการนี้: DataListService.evaluateColumnValueFromRow (Object row, String propertyNameName)


    • value - value of the current row as String

      Thai

      value - ค่าของแถวปัจจุบันเป็น String

Expected Return Object:

Thai

วัตถุส่งคืนที่คาดหวัง:

    • A String value to replace the original row value

      Thai

      ค่าสตริงเพื่อแทนที่ค่าแถวเดิม

Samples:

Thai

ตัวอย่าง:

1) Example code using DataListService service method to evaluate column value "Name" from row object.

Thai

โค้ดตัวอย่างที่ใช้วิธีการบริการ DataListService เพื่อประเมินค่าคอลัมน์ "ชื่อ" จากวัตถุแถว

Code Block
languagejava
//import the necessary classes
import org.joget.apps.datalist.service.DataListService;
import org.joget.apps.app.service.AppUtil;
 
DataListService dataListService = (DataListService) AppUtil.getApplicationContext().getBean("dataListService");
  
//since this entire bean shell applies to every row, "row" is automatically iterated here.
//"name" is the column id
return dataListService.evaluateColumnValueFromRow(row, "name");

2) Format the row value by appending hash variable.

Thai

จัดรูปแบบค่าแถวโดยการผนวกตัวแปรแฮช

Code Block
languagejava
return value + " #currentUser.firstName#";

Use as Process Participant

Thai

ใช้เป็นผู้เข้าร่วมกระบวนการ

Injected Variables:

Thai

ตัวแปรที่ฉีด:

    • pluginManager - Plugin Manager service bean for convenient usage. (org.joget.plugin.base.PluginManager)

      Thai

      pluginManager - บีนปลั๊กอินตัวจัดการปลั๊กอินสำหรับการใช้งานที่สะดวก (org.joget.plugin.base.PluginManager)

    • workflowActivity - Workflow Activity that trying to retrieves assignee. (org.joget.workflow.model.WorkflowActivity)

      Thai

      workflowActivity - กิจกรรมเวิร์กโฟลว์ที่พยายามดึงผู้รับมอบหมาย (org.joget.workflow.model.WorkflowActivity)

Expected Return Object:

Thai

วัตถุส่งคืนที่คาดหวัง:

    • A java.util.Collection of username in java.lang.String to be assign to the Workflow Activity.

      Thai

      java.util.Collection ของชื่อผู้ใช้ใน java.lang.String เพื่อกำหนดให้กับกิจกรรมเวิร์กโฟลว์

Samples:

Thai

ตัวอย่าง:

        Randomly assign a user in a department to a workflow activity.

Thai

สุ่มผู้ใช้ในแผนกให้กับกิจกรรมเวิร์กโฟลว์

Code Block
languagejava
import java.util.ArrayList;
import java.util.Collection;
import org.joget.apps.app.service.AppUtil;
import org.joget.directory.model.User;
import org.joget.directory.model.service.ExtDirectoryManager;
import org.joget.workflow.model.WorkflowActivity;

public Collection getAssignees(WorkflowActivity activity) { 
    Collection assignees = new ArrayList();
    
    ExtDirectoryManager directoryManager = (ExtDirectoryManager) pluginManager.getBean("directoryManager");
    
    String deptId = "D-005";
    
    //Get total user in department
    Long total = directoryManager.getTotalUsers(null, null, deptId, null, null, null, null);
    
    //Get random number from 0 to the total number of users in department
    int random = (int) (Math.random() * total);
    
    //Get users using directory manager
    Collection userList = directoryManager.getUsers(null, null, deptId, null, null, null, null, "firstName", false, random, 1);
    for(Object u : userList){
        User user = (User) u;
        assignees.add(user.getUsername());
    }
    
    return assignees;
}

//call getAssignees method with injected variable
return getAssignees(workflowActivity);

Use as Process Tool 
Anchor
Use as Process Tool
Use as Process ToolTool

Thai

ใช้เป็นเครื่องมือประมวลผล

Injected Variables:

Thai

ตัวแปรที่ฉีด:

    • workflowAssignment - The workflow tool activity assignment object. (org.joget.workflow.model.WorkflowAssignment)

      Thai

      workflowAssignment - วัตถุเครื่องมือกำหนดกิจกรรมกิจกรรมเวิร์กโฟลว์ (org.joget.workflow.model.WorkflowAssignment)

    • pluginManager - Plugin Manager service bean for convenient usage. (org.joget.plugin.base.PluginManager)

      Thai

      pluginManager - บีนปลั๊กอินตัวจัดการปลั๊กอินสำหรับการใช้งานที่สะดวก (org.joget.plugin.base.PluginManager)

    • appDef - App definition of the process. (org.joget.apps.app.model.AppDefinition)

      Thai

      appDef - นิยามแอพของกระบวนการ (org.joget.apps.app.model.AppDefinition)

    • request - Http Request object of current HTTP Request. Not available if the tool is trigger by Deadline. (javax.servlet.http.HttpServletRequest)

      Thai

      คำขอ - วัตถุคำขอ HTTP ของคำขอ HTTP ปัจจุบัน ไม่สามารถใช้งานได้หากเครื่องมือนั้นเปิดใช้งานตามกำหนดเวลา (javax.servlet.http.HttpServletRequest)

Expected Return Object:

Thai

วัตถุส่งคืนที่คาดหวัง:

    • None

      Thai

      ไม่มี

Samples:

Thai

ตัวอย่าง:

Start a new process in the same app with current record id.

Thai

เริ่มกระบวนการใหม่ในแอพเดียวกันด้วยรหัสบันทึกปัจจุบัน

Code Block
languagejava
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.joget.apps.app.model.AppDefinition;
import org.joget.apps.app.service.AppService;
import org.joget.apps.app.service.AppUtil;
import org.joget.workflow.model.service.WorkflowManager;
import org.joget.workflow.model.WorkflowAssignment;
import org.joget.workflow.model.WorkflowProcess;
 
public Object execute(WorkflowAssignment assignment, AppDefinition appDef, HttpServletRequest request) {
    AppService appService = (AppService) AppUtil.getApplicationContext().getBean("appService");
    WorkflowManager workflowManager = (WorkflowManager) AppUtil.getApplicationContext().getBean("workflowManager");
    
    //get current record id
    String recordId = appService.getOriginProcessId(assignment.getProcessId());
    
    //get process
    WorkflowProcess process = appService.getWorkflowProcessForApp(appDef.getAppId(), appDef.getVersion().toString(), "process2");
    
    //start process
    workflowManager.processStart(process.getId(), null, null, null, recordId, false);
    
    return null;
}
 
//call execute method with injected variable
return execute(workflowAssignment, appDef, request);

Update Form Field Value

Thai

ปรับปรุงค่าของฟิลด์แบบฟอร์ม

Code Block
languagejava
import org.joget.apps.app.model.AppDefinition;
import org.joget.apps.app.service.AppService;
import org.joget.apps.app.service.AppUtil;
import org.joget.apps.form.model.FormRow;
import org.joget.apps.form.model.FormRowSet;
import org.joget.workflow.model.WorkflowAssignment;

String formDefId = "hr_expense_new";
String fieldId = "status";
String fieldValue = "Submitted";

//Get record Id from process
AppService appService = (AppService) AppUtil.getApplicationContext().getBean("appService");
String id = appService.getOriginProcessId(workflowAssignment.getProcessId());

//retrieve data
FormRow row = new FormRow();
FormRowSet rowSet = appService.loadFormData(appDef.getAppId(), appDef.getVersion().toString(), formDefId, id);
if (!rowSet.isEmpty()) {
    row = rowSet.get(0);
    //update field value
    row.setProperty(fieldId, fieldValue);
}

//Store the updated data
rowSet.set(0, row);
appService.storeFormData(appDef.getAppId(), appDef.getVersion().toString(), formDefId, rowSet, id);

Start a new process instance for each row of data loaded using foreign key and value

Thai

เริ่มต้นอินสแตนซ์กระบวนการใหม่สำหรับแต่ละแถวของข้อมูลที่โหลดโดยใช้ foreign key และ value

Code Block
languagejava
import org.joget.apps.form.model.FormRow;
import org.joget.apps.form.model.FormRowSet;
import org.joget.apps.app.service.AppUtil;
import org.joget.plugin.base.PluginManager;
import org.joget.apps.form.model.FormLoadBinder;
import org.joget.workflow.model.service.WorkflowManager;

    String formDefId = "ExpensesClaimEntry";  //change this to the form id used to load grid data
    String foreignKey = "claim"; //change this to the foreign key field id
      
    // Reuse Multi Row Binder to load data
    PluginManager pluginManager = (PluginManager) AppUtil.getApplicationContext().getBean("pluginManager");
    WorkflowManager workflowManager = (WorkflowManager) AppUtil.getApplicationContext().getBean("workflowManager");
    FormLoadBinder binder = (FormLoadBinder) pluginManager.getPlugin("org.joget.plugin.enterprise.MultirowFormBinder");
     
    //Load from the grid table
    binder.setProperty("formDefId", formDefId);
    binder.setProperty("foreignKey", foreignKey);
    
    FormRowSet rows;
    rows = binder.load(null, "#assignment.processId#", null);
    
	//loop through records returned
    for (FormRow row : rows) {
        try {
            Map variableMap = new HashMap();
            variableMap.put("status", row.getProperty("purpose"));
            
			//start a new process instance with data from each row
            //processStart(String processDefId, String processId, Map<String, String> variables, String startProcUsername, String parentProcessId, boolean startManually)
            workflowManager.processStart("expenseclaim:latest:process1", null, variableMap, null, row.getProperty("id"), false);
        } catch (Exception e) {}
    }

Use as Process Route DecisionRoute Decision

Thai

ใช้เป็นการตัดสินใจเส้นทางกระบวนการ

Note:

Thai

บันทึก:

    • By using a decision plugin, the specified route transition rules configured explicitly in the process design will be ignored.

      Thai

      โดยใช้ปลั๊กอินการตัดสินใจกฎการเปลี่ยนเส้นทางที่ระบุซึ่งกำหนดค่าไว้อย่างชัดเจนในการออกแบบกระบวนการจะถูกละเว้น

Injected Variables:

Thai

ตัวแปรที่ฉีด:

    • appDef - App definition of the process. (org.joget.apps.app.model.AppDefinition)

      Thai

      appDef - นิยามแอปของกระบวนการ (org.joget.apps.app.model.AppDefinition)

    • pluginManager - Plugin Manager service bean for convenient usage. (org.joget.plugin.base.PluginManager)

      Thai

      pluginManager - บีนปลั๊กอินตัวจัดการปลั๊กอินสำหรับการใช้งานที่สะดวก (org.joget.plugin.base.PluginManager)

    • processDefId - The process definition ID of the current process instance

      Thai

      processDefId - ID นิยามกระบวนการของอินสแตนซ์กระบวนการปัจจุบัน

    • processId - The process instance ID of the current process instance

      Thai

      processId - ID อินสแตนซ์กระบวนการของอินสแตนซ์กระบวนการปัจจุบัน

    • routeId - The ID of the route node currently configured on

      Thai

      routeId - ID ของโหนดเส้นทางที่กำหนดค่าไว้ในปัจจุบัน

Expected Return Object:

Thai

วัตถุส่งคืนที่คาดหวัง:

Samples:

Thai

ตัวอย่าง:

Decide to approve or reject a request based on the value of the workflow variable "status".

Thai

ตัดสินใจที่จะอนุมัติหรือปฏิเสธคำขอตามมูลค่าของตัวแปรสถานะ "เวิร์กโฟลว์"

Code Block
languagejava
import org.joget.workflow.model.DecisionResult;

//Transition Name to go to
String transitionApprove = "approve";
String transitionReject = "reject";

//Workflow variable ID to set
String workflowVariable = "status";

DecisionResult result = new DecisionResult();

/* Bean Shell Decision supports hash variable, and has access to the process instance context. */
//System.out.println("Value of \'status\' wf-var in this process instance is: " + "#variable.status#");

if ("yes".equalsIgnoreCase("#variable.status#")) {
    result.addTransition(transitionApprove);
    result.setVariable(workflowVariable, "ApprovedByBeanShellDecision");
} else {
    result.addTransition(transitionReject);
    result.setVariable(workflowVariable, "RejectedByBeanShellDecision");
}

/* For use cases requiring multiple node routing, do set boolean below as true. */
//result.setIsAndSplit(true);

return result;

Use as Userview Permission

Thai

ใช้เป็นสิทธิ์ของ Userview

Injected Variables:

Thai

ตัวแปรที่ฉีด:

    • user - User object of current logged in user (org.joget.directory.model.User)

      Thai

      ผู้ใช้ - วัตถุผู้ใช้ของผู้ใช้ที่เข้าสู่ระบบในปัจจุบัน (org.joget.directory.model.User)

    • requestParams - Request parameters map of current HTTP Request (java.util.Map)

      Thai

      requestParams - แมปพารามิเตอร์คำขอของคำขอ HTTP ปัจจุบัน (java.util.Map)

Expected Return Object:

Thai

วัตถุส่งคืนที่คาดหวัง:

    • A boolean value to indicate the user is authorized.

      Thai

      ค่าบูลีนเพื่อระบุผู้ใช้ที่ได้รับอนุญาต

Samples:

Thai

ตัวอย่าง:

Check the user is in a group and is an admin user.

Thai

ตรวจสอบผู้ใช้ที่อยู่ในกลุ่มและเป็นผู้ใช้ที่เป็นผู้ดูแลระบบ

Code Block
languagejava
import java.util.Collection;
import java.util.Map;
import org.joget.apps.app.service.AppUtil;
import org.joget.directory.model.Group;
import org.joget.directory.model.User;
import org.joget.directory.model.service.ExtDirectoryManager;
import org.joget.workflow.model.service.WorkflowUserManager;
import org.joget.workflow.util.WorkflowUtil;
public boolean isAuthorized(User user, Map params) { 
    //if no logged in user
    if (user == null) {
        return false;
    }
    //check current user is admin
    boolean isAdmin = WorkflowUtil.isCurrentUserInRole(WorkflowUserManager.ROLE_ADMIN);
    
    //check current user is in group "G-001"
    boolean inGroup = false;
    
    ExtDirectoryManager directoryManager = (ExtDirectoryManager) AppUtil.getApplicationContext().getBean("directoryManager");
    Collection groups = directoryManager.getGroupByUsername(user.getUsername());
    if (groups != null) {
        String groupId = "G-001";
        for (Group g : groups) {
            if (groupId.equals(g.getId())) {
                inGroup = true;
            }
        }
    }
    
    return isAdmin && inGroup;
}
  
//call isAuthorized method with injected variable
return isAuthorized(user, requestParams);


Best Practices

Thai

ปฏิบัติที่ดีที่สุด

1. Only import classes that are needed

Thai

นำเข้าคลาสที่จำเป็นเท่านั้น

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:

Thai

ห้ามใช้อักขระตัวแทนในคำสั่งการนำเข้า มันให้ประสิทธิภาพที่แย่มากในล่าม Bean Shell เพื่อค้นหาแพ็คเกจทั้งหมดและโหลดในหน่วยความจำ

ไม่ได้:

Code Block
languagejava
import java.util.*;

Do:

Thai

ทำ:

Code Block
languagejava
import java.util.Collection;

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

Thai

ไม่จำเป็นต้องพูดถึงประเภทขององค์ประกอบของคอลเลกชัน

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

Don't:

Thai

ล่าม Bean Shell ไม่สามารถจดจำไวยากรณ์ประเภทองค์ประกอบของคลาสคอลเลกชันเช่น Collection, Map, List, Set และอื่น ๆ

ไม่ได้:

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

Do:

Thai

ทำ:

Code Block
languagejava
Map map = new HashMap();

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

Thai

ย่อหน้าสคริปต์ของคุณอย่างดีและเป็นไปตามจาวาโค้ด

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.change quickly.

Thai

มันจะทำให้ชีวิตของคุณและผู้อื่นง่ายขึ้นในการบำรุงรักษาและตรวจสอบสคริปต์เมื่อใดก็ตามที่จำเป็นเนื่องจาก Joget Workflow มอบความยืดหยุ่นในการปรับเปลี่ยนการเปลี่ยนแปลงอย่างรวดเร็ว

4. Write your script in functions

Thai

เขียนสคริปต์ของคุณในฟังก์ชั่น

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.

Thai

หากสคริปต์ของคุณค่อนข้างยาวและบางส่วนของสคริปต์นั้นสามารถใช้ซ้ำได้โปรดใช้ประโยชน์จากฟังก์ชั่นในการเขียนสคริปต์ของคุณ มันให้การอ่านและประสิทธิภาพที่ดีขึ้น

5. Remember to write some comments 

Thai

อย่าลืมเขียนความคิดเห็น

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

Thai

มันจะช่วยให้คุณและคนอื่น ๆ เข้าใจว่าอะไรคือจุดประสงค์ของสคริปต์อย่างรวดเร็ว

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

Thai

ตรวจจับข้อยกเว้นและให้ข้อความที่มีความหมายในบันทึก

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

Thai

หากคุณใช้ Bean Shell Scripting จำนวนมากในแอปข้อความบันทึกที่มีความหมายสามารถช่วยคุณค้นหาปัญหาได้อย่างรวดเร็ว

ไม่ได้:

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

Do:

Thai

ทำ:

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

7. Consider to make it as a plugin instead of using Bean Shell

Thai

พิจารณาให้เป็นปลั๊กอินแทนการใช้ Bean Shell

If your script need to reuse for multiple times in an app or can be used by others app in future development, please consider to make your Bean Shell script as a plugin instead of copy and paste it multiple times across your app. Bean Shell script is harder to maintain in this case. Imagine that you want to make a change, you will have to update all the places that are using the same script as well. Beside that, a normal plugin implementation will have better performance than a script running by a Bean Shell interpreter.

Thai

หากสคริปต์ของคุณจำเป็นต้องนำมาใช้ซ้ำหลายครั้งในแอปหรือสามารถใช้โดยแอพอื่น ๆ ในการพัฒนาในอนาคตโปรดพิจารณาสร้างสคริปต์ Bean Shell ของคุณเป็น pluginแทนการคัดลอกและวางหลายครั้งในแอปของคุณ สคริปต์ Bean Shell นั้นยากที่จะรักษาไว้ในกรณีนี้ ลองจินตนาการว่าคุณต้องการเปลี่ยนแปลงคุณจะต้องอัปเดตสถานที่ทั้งหมดที่ใช้สคริปต์เดียวกันด้วย นอกจากนั้นการใช้งานปลั๊กอินปกติจะมีประสิทธิภาพที่ดีกว่าสคริปต์ที่ทำงานโดยล่าม Bean Shell

8. Reuse existing plugins in your code to make it cleaner and easy to maintain

Thai

นำปลั๊กอินที่มีอยู่กลับมาใช้ใหม่ในรหัสของคุณเพื่อให้ทำความสะอาดและบำรุงรักษาง่าย

If partial of your script can be done by existing plugins in Joget Workflow, you can reuse the plugin in stead of writting it again. 

To reuse a plugin, you can retrieve the plugin using PluginManager, then set it properties and execute it.

Example: 

Thai

หากบางส่วนของสคริปต์ของคุณสามารถทำได้โดยปลั๊กอินที่มีอยู่ใน Joget Workflow คุณสามารถนำปลั๊กอินนั้นกลับมาใช้ใหม่ได้อีกครั้ง

ในการใช้ปลั๊กอินอีกครั้งคุณสามารถเรียกคืนปลั๊กอินโดยใช้ PluginManager จากนั้นตั้งค่าคุณสมบัติและเรียกใช้งานปลั๊กอิน

ตัวอย่าง:

  1. Reuse Multi Row Binder plugin to load data
  2. Reuse Email Tool to send email

9. How to use 3rd party libraries in Bean Shell

Thai

วิธีใช้ไลบรารีบุคคลที่สามใน Bean Shell

In your webserver, you can add the jar file to your WEB-INF/lib folder and restart the server. Then, you should be able to "import" and use them in your bean shell java code.

Thai

ในเว็บเซิร์ฟเวอร์ของคุณคุณสามารถเพิ่มไฟล์ jar ลงในโฟลเดอร์ WEB-INF / lib ของคุณแล้วรีสตาร์ทเซิร์ฟเวอร์ จากนั้นคุณควรจะสามารถ "นำเข้า" และใช้พวกเขาในรหัสของคุณ

More samples

Thai

ตัวอย่างเพิ่มเติม

Children Display

...