Hi Nihanth,
I recommend implementing a separate CSV template validation step before executing the Data Importer activity.
The important point is that row-level validation and file-structure validation should be handled separately.
Maintain the expected column names in a configuration entity or a predefined template.
For example:
EmployeeId,EmployeeName,Email,DepartmentDefine which columns are mandatory and whether additional columns are allowed.
Create a microflow named ACT_ValidateCSVStructure.
Before calling the Data Importer activity:
Read the uploaded CSV file.
Extract the first CSV record (header row).
Compare the actual headers against the expected template.
Check for missing required columns.
Check for duplicate column names.
Check for unexpected columns if your template requires an exact match.
Validate the expected column count.
For reliable CSV parsing, use a Java Action with a CSV parser such as Apache Commons CSV. Avoid using simple StringSplit because CSV values can contain quoted delimiters.
Also handle whitespace and UTF-8 BOM characters when comparing header names.
The validation step should return a clear result containing:
Validation status
Missing columns
Unexpected columns
Duplicate columns
Invalid header information
For example, if the uploaded file contains:
EmployeeId,EmployeeName,EmailExpected template:
EmployeeId,EmployeeName,Email,DepartmentReturn:
Validation Status: Failed
Missing Column: DepartmentConfigure your main microflow as follows:
Upload CSV
|
v
Validate CSV Structure
|
v
Validation Successful?
|
+---- No ----> Display Validation Errors
| Stop Processing
|
+---- Yes ---> Import with Mapping
|
v
Row-Level Validation
|
v
Process / Commit DataImportant: Do not execute Import with Mapping or start processing records before the structure validation succeeds. This ensures that an invalid template is rejected before any business data is processed.
Once the header validation passes, continue using your existing Data Importer and field-level validation logic for individual rows.
This gives you two validation levels:
File-level validation: Checks CSV format, headers, required columns, duplicates, and template compatibility.
Row-level validation: Checks individual field values and business rules.
Official Mendix documentation:
Hope this helps! If you find this answer helpful, please mark it as the accepted answer.