Importing data

Lesson 9 out of 29

A prevalent task during the initial phases of utilizing Bitrix24 is importing an already established customer database.

Importing data

4 min

Basic data import

Let's navigate to "Import and Export Data" and choose the "Import Customers" template.

We have already made an example for this tutorial, which you can download and upload to the server, similarly to what we did before.

You will observe that a preconfigured webhook for incoming data is available for us, which we will use to insert contacts.

Let us examine the code example.

It employs a method to add a contact and passes the first name, last name, email, and phone number as parameters.

Let us test the example by accessing index.php via the browser. The example runs successfully, and we can now view the newly added contact in the CRM.

In practicality, importing operations involve transferring multiple customers, not just one.

Let us write the code to import data from a CSV file format, which can be exported from various sources, including databases or Excel tables. The file used in this lesson can be retrieved from the lesson materials along with an example.

Let us examine the CSV file, which has a straightforward structure—each data line comprises of five fields separated by semicolons, with the first line denoting the field names.

Let us first test the example by opening csv_import.php in the browser. The example is successful, and we can view the debugging information, including the data array from our CSV file and the array with the REST API query results. In case of any issues, we can analyze and fix the problem causes by examining the error messages in this array.

Importing huge data

The implementation has been successful, and it is probable that in most cases, you have sufficient knowledge to import data into Bitrix24. Nevertheless, you may encounter difficulties when the volume of data to be imported is substantial. In such scenarios, a simple loop may trigger the limit on the number of REST hits per unit of time. You can read more about this limit in the supplementary materials for the lesson.

We will demonstrate a special method for executing REST requests in batches, which can assist us.

Let us open the batch_import file. The code begins by retrieving the data from the CSV file, which now contains approximately 300 records.

We still use a loop to traverse the array with the data to be imported, but instead of executing each individual crm.contact.add request, we form an array for batch execution.

This array has a unique structure where we specify the REST method to be executed, the parameters, and parameter values in each element.

The objective is to form another batch of 50 queries and execute that batch of queries by calling the special batch method, as we traverse the array.

Batch - a way to execute up to 50 requests per hit to Bitrix24

Why 50? This is the current limitation of the batch method in the Bitrix24 REST API.

If we have 2000 clients in a file, instead of executing 2000 separate requests of the crm.contact.add method, we will execute only 40 batch requests, resulting in substantial optimization.

Let us test the finished example by accessing batch_import.php via the browser. The example runs successfully, and upon visiting Bitrix24, we will see the imported customers.

Resources

Lesson materials: