Skip to main content

Use the Before Row Hook

The PHP code in the Before Row scripting area is executed before each row of the import file is evaluated field by field. You typically use this area to load existing records so that the import updates them rather than creating duplicates.

The $model Variable

The $model reserved variable is used to populate data of the entity that will be imported. It is a PHP Object of the same type as the Model field specified in the Import. For example, if the Import is related to a DeepCmdb -- Ci Model, then $model will contain a Deep_Cmdb_Model_Ci Object. It represents a record of the CMDB CI table in the database.

The $data Variable

The $data reserved variable exposes data from the current Excel file row. It is a PHP Array containing the values of every column of the current row.

For example, given the sample Excel file in Create an Import, the variable $data will contain the following values for the first row:

$data[0] = "106545"
$data[1] = "Apple Iphone XI"
$data[2] = "Mobiles"
$data[3] = "Smartphones"
$data[4] = "Active"
$data[5] = "15/03/2019"
$data[6] = "LFC6370742"
$data[7] = "Scratches on the screen"
$data[8] = "INTODEEP srl"
$data[9] = "4\euler"

Starting from these variables, the Before Row area is used to retrieve a model from the database starting from an alphanumeric value.

For example, if a record must be loaded by the CODE column in the Excel and not by ID, you can load the record with that code in the Before Row, starting from the Excel data and the Deepser database.

note

In this case, the original DB record, once loaded into the $model variable, can be updated with the values in the current Excel file row instead of creating a new record for every row.

Before Row Tutorial

This tutorial demonstrates how to use the Before Row scripting area to update existing records rather than creating duplicates. The code checks the CODE column and loads a matching device from the database when one already exists.

In the following example, the column "CODE" is checked. If it is not empty and another device with the same code already exists, the device will be updated with data in the current row of the import file. Without this check, a new device would be created.


/* the Excel file column with index 0 (column A) containing the CODE field is evaluated.
the $data array represents the current line of the import file and it has all
its values accessible via the Excel column index. The index starts from 0. */
$deviceCode = trim((string)$data[0]);
/* if the column containing the attribute 'CODE' is not empty */
if($deviceCode){
/* if a device with same CODE already exists
the $deviceArray array was created with key = CODE and value = instance of the device
in 'Before Run' area */
if(array_key_exists($deviceCode,$deviceArray)){
/* the instace of the device already present, with the same code, is assigned to
the $model object
$model object represents the current instance of the
Deep_Cmdb CI model (specified in the Model field of the import configuration)*/
$model = $deviceArray[$deviceCode];
}
/* the class_id attribute is set by default to 2 (Device) */
$model->setClassId(2);
}
else {
/* if 'CODE' is empty, the current model is set to null to avoid saving
any empty or incomplete records */
$model = null;
}


warning

If the CODE field is empty, the $model is set to null to prevent saving incomplete records. Make sure your import file does not contain rows with missing key fields unless you intend to skip them.