Versions Compared

Key

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

...

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 usernameprimaryKey, 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);

...

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);

...

Expected Return Object:

    • None

Samples:

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

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);

Use as Userview Permission

Injected Variables:

...

Update Form Field Value

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

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 Userview Permission

Injected Variables:

    • user - User object of current logged in user (org.joget.directory.model.User)
    • requestParams - Request parameters map of current HTTP Request (java.util.Map)

Expected Return Object:

    • A boolean value to indicate the user is authorized.

Samples:

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

Expected Return Object:

    • A boolean value to indicate the user is authorized.

Samples:

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

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);

...

  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

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. 

More samples

Children Display