You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Context

This is the form to capture the grid data.

On submission of this form, we would like to start a process instance for each of the rows from this grid.

We would like to start a process instance of the process definition "travel_approver_process".

We will need to pick up the grid data by matching the current batch record ID in gda_batch and list down records from gda_travel.

Sample SQL
SELECT * FROM app_fd_gda_travel WHERE c_batch = '#form.gda_batch.id#'

Solution

In the batch form itself, we can make use of "Post Form Submission Processing" to capture event of "Data Creation" or "Both data creation and update", whichever suits your own use case, to execute bean shell Java code to achieve this.

aa

import org.joget.workflow.model.service.WorkflowManager;
import org.joget.apps.app.service.AppUtil;
import org.joget.apps.app.service.AppService;
import org.joget.workflow.model.WorkflowAssignment;
import org.joget.workflow.util.WorkflowUtil;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.joget.workflow.model.WorkflowProcess;
import org.joget.workflow.model.WorkflowProcessResult;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.joget.commons.util.LogUtil;

//define process to start
String processDefKey = "travel_approver_process";

//utility bean
WorkflowManager workflowManager = (WorkflowManager) AppUtil.getApplicationContext().getBean("workflowManager");
AppService appService = (AppService) AppUtil.getApplicationContext().getBean("appService");
 
//get processDefId
WorkflowProcess processDef = appService.getWorkflowProcessForApp(appDef.getId(), appDef.getVersion().toString(), processDefKey);
String processDefId = processDef.getId();

//get foreign key
String batchId = "#form.gda_batch.id#";

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 * FROM app_fd_gda_travel WHERE c_batch = ?");
        stmt.setObject(1, batchId);
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            LogUtil.info(appDef.toString(), "Processing Batch " + batchId + " - Record: " + rs.getObject("id"));
		    Map variables = new HashMap();
		    //variables.put("batch", batchId);
		    WorkflowProcessResult result = workflowManager.processStart(processDefId, null, variables, "admin", rs.getObject("id"), false);
            LogUtil.info(appDef.toString(), "Processing Batch " + batchId + " - Record: " + rs.getObject("id") + " - Status: " + result.getProcess().getInstanceId());
        }
    }
} catch(Exception e) {
    LogUtil.error(appDef.toString(), e, "Error in creating approval process for batch " + batchId);
} finally {
    //always close the connection after used
    try {
        if(con != null) {
            con.close();
        }
    } catch(SQLException e) {/* ignored */}
}


aa


  • No labels