Import files into custom module

Hi,
I have a custom module where I have enabled data entry via csv import.
Now i need to run checks on the records i import, how do i get the errors that are possibly generated returned?
i found the file

modules/import/CsvAutoDetect.php

where there are the imported rows, these rows i would like to compare with the DB content, but i don’t know how to return the error. Any ideas? Thanks

@fab
You can write functions in the custom module object to validate data before importing. These functions are called automatically.

public function beforeImportSave() {
...
}
public function afterImportSave() {
...
}

Where should they be inserted? In logic hook?

@fab

No. They should be in object file, for examples:

  • modules/Accounts/Account.php
  • modules/<custom_module_name>/<custom_object_name>.php

how the <custom_object_name> file is called and the method

public function beforeImportSave() {
...
}

?

When you start an import for any module, the function is called automatically.
You can look at the saveImportBean function in the modules/Import/Importer.php file.

sorry p.konetskiy, I am trying to follow your instructions, but it is not working, surely I am missing something.
I created the Import.php file inside custom/modules/<custom_module>/:

class Import{
public function beforeImportSave() {
	$GLOBALS['log']->fatal('***** IMPORT ***** ');
}
}

but is not executed

Okay, I edited the modules/<custom_module>/<custom_module>.php

by adding

public function beforeImportSave() {
	$GLOBALS['log']->fatal('***** IMPORT ***** ');
}

and now it works, but what I don’t understand is how I report an error when a line does not meet my conditions.

I see two variants:

  1. Modify the importRow function in the
    modules/Import/Importer.php file. But it does not support upgrade.
  2. Create a special filed for custom module and add the filed to the indexes array and set a special function to custom field in indexes array. The function should call the writeError function from object ImportDataSource of the modules/Import/sources/ImportDataSource.php file. Example for for vardefs.php of custom module:
$dictionary['<object_name>'] = array(
...
    'fields'=>array (
...
        'number' => array (
            'required' => true,
            'name' => 'number',
            'vname' => 'LBL_NUMBER',
            'type' => 'int',
            'readonly' => true,
            'len' => 11,
            'auto_increment' => false,
            'massupdate' => 0,
            'no_default' => false,
            'comments' => '',
            'help' => '',
            'importable' => true,
            'duplicate_merge' => 'disabled',
            'duplicate_merge_dom_value' => '0',
            'audited' => false,
            'reportable' => true,
            'unified_search' => false,
            'merge_filter' => 'disabled',
        ),
...
    ),
    'indices' => array(
        'number' => array(
            'name' => 'idx_number_collection',
            'type' => 'index',
            'fields' => array('number'),
            'dupeCheckFunction'=>'dupeChecknumberImportCollection'
        )
    ),
...

!!! Warning !!!
The realy name of special function for custom module should be numberImportCollection not dupeChecknumberImportCollection
This is a function format.

public function numberImportCollection($index) {
... 
    if(<your_custom_condition>)
    {
        return true;
    }
    else 
    {
        return false;
    }
}

The custom function controls duplicate records. I think you can call the writeError function without me.
The variant will safe to upgrade.

Update:

After a lot of work on it, I decided to use the first method you suggested (changing the importRow of Importer, also because I didn’t quite understand the other method). It was just what I was looking for, here I have the records in the .csv file that I import and I can do the checks I want within the DB, issuing custom error messages.
Thank you very much!

PS just one thing, when I import a file I have to set the date format each time (by default it’s like 12/23/2020), changing it to 23/12/2020, is there any way to default to the values I want?