Font Size:
Ask Joget AI

Bulk Create Form Records with Attachments and Start Process Using BeanShell

Introduction

This article demonstrates a server-side approach to bulk create form records with file attachments and automatically start a workflow for each record. This is useful for migration or automation scenarios where files already exist on the Joget server, and Excel/CSV import cannot be used because file upload fields are involved.

How does it work?

This BeanShell logic can be executed in various Joget components, such as:

The script should be adapted to match your environment by updating configuration values such as baseFolder, formDefId, fileFieldId, and processDefId.

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.joget.apps.app.service.AppUtil;
import org.joget.apps.app.service.AppService;
import org.joget.apps.app.model.AppDefinition;
import org.joget.apps.form.model.FormRow;
import org.joget.apps.form.model.FormRowSet;
import org.joget.apps.form.service.FileUtil;
import org.joget.workflow.model.service.WorkflowManager;
import org.joget.commons.util.LogUtil;

// ================= CONFIG =================
String baseFolder = "/documents"; 
String formDefId = "formA";       
String fileFieldId = "fileUploadField";    
String processDefId = "testApp#4#processTest";

// ================= SERVICES =================
AppDefinition appDef = AppUtil.getCurrentAppDefinition();
AppService appService = (AppService) AppUtil.getApplicationContext().getBean("appService");
WorkflowManager workflowManager = (WorkflowManager) AppUtil.getApplicationContext().getBean("workflowManager");

String tableName = appService.getFormTableName(appDef, formDefId);

// ================= SCRIPT =================
File root = new File(baseFolder);
File[] folders = root.listFiles();

for (File folder : folders) {
    try {
        if (!folder.isDirectory()) continue;

        String recordId = folder.getName();

        File[] files = folder.listFiles();
        if (files == null || files.length == 0) continue;

        File attachmentFile = files[0];

        LogUtil.info("BulkImport", "Processing record: " + recordId);

        // ===== STORE FORM DATA =====
        FormRow row = new FormRow();
        row.setId(recordId);
        row.setProperty(fileFieldId, attachmentFile.getName());

        FormRowSet rowSet = new FormRowSet();
        rowSet.add(row);

        appService.storeFormData(
            appDef.getAppId(),
            appDef.getVersion().toString(),
            formDefId,
            rowSet,
            null
        );

        // ===== STORE FILE INTO JOGET STORAGE =====
        FileUtil.storeFile(
            attachmentFile,
            tableName,
            recordId
        );

        LogUtil.info("BulkImport", "Form and attachment stored for: " + recordId);

        // ===== START WORKFLOW =====
        workflowManager.processStart(
            processDefId,
            null,
            null,
            null,
            recordId,
            false
        );

        LogUtil.info("BulkImport", "Workflow started for: " + recordId);

    } catch (Exception ex) {
        LogUtil.error("BulkImport", ex, "Error processing folder");
    }
}

The BeanShell script reads folders from a specified server directory, where each folder represents a single record and the folder name is used as the record ID. The script retrieves the first file found in each folder and treats it as the attachment for that record. Assuming that the path structure is as follows:

documents/
  123/
    file.pdf
  124/
    file.pdf

For each folder, the script first creates and stores the form record, setting the file upload field with the attachment filename. It then copies the file into Joget’s internal upload storage, ensuring the attachment is accessible through the form. After the record and attachment are successfully stored, the script starts the corresponding workflow process.

Expected Outcome

For each folder in the source directory, a form record is created with its attachment properly stored in Joget, and a workflow instance is started for that record. This enables automated bulk migration or processing of records with attachments while maintaining proper linkage between form data, uploaded files, and workflow processes.

Download sample app

Download the demo app for Bulk Create Form Records with Attachments and Start Process Using BeanShell:
Created by Nik Nufayl Daniel Md Nezam Last modified by Debanraj Ravindran on Apr 24, 2026