Use the Before Run Hook
The Before Run scripting area lets you place custom PHP code that executes before the import procedure starts processing any rows. You typically use it to pre-load data from the database into arrays, which avoids running repeated queries during the import.
When to Use Before Run
Use the Before Run area when you need to:
- Retrieve existing records to check for duplicates during import.
- Pre-load lookup tables (types, subtypes, statuses, companies) so you can resolve human-readable names to database IDs without per-row queries.
- Store any data in variables that will be available in all other scripting areas (Before Row, After Row, and per-field Code).
If you use the same PHP variable name in multiple scripting areas, the value will be overwritten.
Before Run Tutorial
This tutorial shows a practical Before Run script that pre-loads existing data from the Deepser database before the import begins. By building arrays of devices, companies, types, subtypes, and statuses, you can efficiently check for duplicates and resolve human-readable names to their corresponding IDs.
In the following example, you are retrieving devices already inside the Deepser database to avoid duplicates when importing new records from an Excel file. You are also retrieving Companies, Types, Subtypes, and Status List Values to convert their names to corresponding IDs.
/* through the static method in Deep class, a collection of CI is retrived. The collection is filtered by class_id = 2 that represents the 'Device' class*/
$deviceCollection = Deep::getResourceModel('deep_cmdb/ci_collection')->addFieldToFilter('class_id',['eq' => 2]);
/* declaring array containing device instances */
$deviceArray = [];
/* each element of the collection is saved in an associative array, having as key the CODE field (first column of the import file) and as value the instance of the Device. The instance is a PHP object with all the data inside the Database for the record */
foreach($deviceCollection as $device){
$deviceArray[$device->getCustCode()] = $device;
}
/* declaring an array containing type-subtype tree */
$subtypesTree = [];
/* declaring array used for id->name type traslation */
$typeIdentity = [];
/* retrieving CMDB Type collection through static method getResourceModel in Deep class */
$typeCollection = Deep::getResourceModel('deep_cmdb/type_collection');
/* retrieving CMDB Subtype collection through static method getResourceModel in Deep class */
$subtypeCollection = Deep::getResourceModel('deep_cmdb/subtype_collection');
foreach ($typeCollection as $type){
/* for each type, the array tree is populated assigning the name of the type converted into uppercase
and a child array as a value, whose key 'id' corresponds to the type id
es: arrayTree [typeName] ['id'] = typeId */
$subtypesTree [strtoupper($type->getName())] = ['id' => $type->getId()];
/* for each type, the identity array is configured by assigning the id as key and name
converted uppercase as value the name attribute transformed into uppercase */
$typeIdentity[$type->getId()] = strtoupper($type->getName());
}
/* for each subtype, the types in the array tree are updated by updating child array, adding the element
that represents a subtype having as key the name of the subtype transformed into uppercase, and as
value the id of the subtype. eg: arrayTree [typeName] [subtypeName] = subtypeId */
foreach ($subtypeCollection as $subtype){
$subtypesTree[$typeIdentity[$subtype->getTypeId()]] = array_merge($subtypesTree[$typeIdentity[$subtype->getTypeId()]], [strtoupper($subtype->getName()) => $subtype->getId()]);
}
/* retrieving Company collection through static method getResourceModel in Deep class */
$companyCollection = Deep::getResourceModel('deep_company/company_collection');
$companyArray = [];
foreach($companyCollection as $company){
/* for each company, the array is configured by assigning as key the company name
transformed into uppercase and company id as value */
$companyArray [strtoupper($company->getName())] = $company->getId();
}
/* Retrieving values and keys of cmdb_ci_status list
Deep::helper('deep_list') retieving list helper which is an utility class
->loadListValues('cmdb_ci_status') retieving list elements
->toOptionHash() the return collection is converted in a key = label array
array_flip wrapping keys with vakues
array_change_key_case keys case converted to uppercase */
$statusArray = array_change_key_case(array_flip(Deep::helper('deep_list')
->loadListValues('cmdb_ci_status')->toOptionHash()),CASE_UPPER);
By pre-loading all collections in Before Run, you avoid executing a database query for every row of the import file. This significantly improves performance on large imports.