1
0
-1

Hi,

I tried to use data populating Bean shell script given as sample in my application.

1 - Then I created this form

2 - I copied and pasted the bellow script in Strore binder option of the form.

-------------------------------------------------------------------------------------

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.model.AppDefinition;

import org.joget.apps.app.service.AppService;

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.apps.form.service.FormUtil;

import org.joget.plugin.base.PluginManager;

import org.joget.commons.util.LogUtil;

  

public FormRowSet storeData(Element element, FormRowSet rows, FormData formData) {

    //check for empty data

    if (rows == null || rows.isEmpty()) {

        return rows;

    }

    normalStoring(element, rows, formData);

   //store only needed field by create new Form Row Set

    FormRow originalRow = rows.get(0);

    FormRowSet newRows = new FormRowSet();

    FormRow newRow = new FormRow();

  

    newRow.put("firstName", originalRow.getProperty("firstName"));

    newRow.put("lastName", originalRow.getProperty("lastName"));

    newRow.put("email", originalRow.getProperty("email"));

    newRows.add(newRow);

      String id = "#currentUser.username#";

    //store

    storeToOtherFormDataTable(element, newRows, formData, id);

    storeUsingJDBC(element, newRows, formData, id);

  

    return rows;

}

  //this function will reuse workflow form binder to store data

public void normalStoring(Element element, FormRowSet rows, FormData formData) {

    PluginManager pluginManager = (PluginManager) AppUtil.getApplicationContext().getBean("pluginManager");

    FormStoreBinder binder = (FormStoreBinder) pluginManager.getPlugin("org.joget.apps.form.lib.WorkflowFormBinder");

    binder.store(element, rows, formData);

}

  

//this function will store rows data to a form's data table

public void storeToOtherFormDataTable(Element element, FormRowSet rows, FormData formData, String id) {

    AppService appService = (AppService) AppUtil.getApplicationContext().getBean("appService");

  

    String formId = "user"; // the table of database is configured in the form with id "user"

    AppDefinition appDef = AppUtil.getCurrentAppDefinition();

  

    appService.storeFormData(appDef.getId(), appDef.getVersion().toString(), formId, rows, id);

}

  

//this function will store rows data to external source using JDBC

public void storeUsingJDBC(Element element, FormRowSet rows, FormData formData, String id) {

    Connection con = null;

    try {

        // retrieve connection from the default datasource

        DataSource ds = (DataSource)AppUtil.getApplicationContext().getBean("setupDataSource");

        con = ds.getConnection();

  

        if (!con.isClosed()) {

            //manually handle insert and update by checking the data is exist or not

            String selectQuery = "SELECT username FROM dir_user WHERE username=?";

            PreparedStatement stmt = con.prepareStatement(selectQuery);

            stmt.setString(1, id);

            ResultSet rs = stmt.executeQuery();

  

            Boolean isExist = false;

            if (rs.next()) {

                isExist = true;

            }

-------------------------------------------------------------------------------------

3 - When I test  form submitting, this is what I get:

 

Any HELP?

THANKS very Much

  1. Walter

    It says there's error in executing script. Have you checked the server log?

CommentAdd your comment...

3 answers

  1.  
    1
    0
    -1

    HI Matthew,

    I tested the example app and it works fine. It will be usefull for me for another case.

    In fact, my purpose is to insert the current form data fisrt in the table related to it,  and in another table of the database. Even in 02 other tables simultaniously if possible after current form submitting.

    That's why I tried the script above.

    So how can I modify the script to reach my purpose.

     

    THANKS again.

      CommentAdd your comment...
    1.  
      1
      0
      -1

      HI Matthew,

      Thanks in advance for your reply.

      I make a feedback after testing.

      Best Regards

        CommentAdd your comment...
      1.  
        1
        0
        -1

        Hi Aimé WAHI

        I attached below a working example app using BeanShell Load & BeanShell Save Binder in a form. You can add records, edit records and delete records in datalist ( using JDBC Datalist Action ).

        All BeanShell codes were copied and customized from Joget Bean Shell Programming Guide in Joget Knowledge Base. 

        APP_load_store_beanshell_form_example.jwa

          CommentAdd your comment...