Skip to content
Light
English

2. Creating a view to map customers (PlugSales)

PlugSales customers need to have the following information:

  • ID
    • Numeric field of type inteiro. This field will be returned in the request.
  • REASON
    • texto field with the customer’s company name.
  • FANTASY
    • texto field with the customer’s business name.
  • CNPJ
    • Field texto with the customer’s CNPJ/CPF.
  • ADDRESS
    • texto field with the customer’s address.
  • PHONE
    • Field texto with the customer’s phone number.
  • EMAIL
    • texto field with the customer’s email.
  • CITY
    • Field texto with the customer’s city.
  • UF
    • texto field with the client’s status.
  • SITUATION
    • texto field with the client’s situation.
  • USER
    • Numeric field of type inteiro with the user code linked to the customer

To adapt to the PlugSales format, you do not need to create a new customer table with these columns and start registering from scratch.

Just create a view in your database, which will return records with the columns needed by PlugSales.

In practice, a view is a virtual table in which its records are the results of a SELECT defined at the time of its creation.

For example, to create a view that simulates a table that displays only VIP customers:

CREATE VIEW ClientesVIP AS SELECT FirstName1, LastName1 FROM Customers WHERE VIP = ‘S’;

And the query from VIP customers about this view would simply be:

SELECT * FROMClientsVIP;

To create the customer view that PlugSales needs, imagine that you have the following table structure in your database:

Captura da tela 1

PlugSales needs information that is, in this scenario, in different tables.

To solve this and give PlugSales the information it needs about customers, you can write a SQL query with several inner joins, which will be executed every time PlugBot needs to read information about your customers.

Or you can create an view that will bring the data in the correct format all at once to PlugSales.

To create view that serves PlugSales in this table structure, the SQL would be as follows:

CREATE OR REPLACE VIEW VwClientesPlugSales AS SELECT c.id, c.razao, c.fantasia, c.cnpj, CONCAT(e.logradouro, ’, N. ’, e.numero) address, co.telefone, co.email, e.cidade, e.uf, c.situacao FROM TbClientes c INNER JOIN TbAddresses e ON c.id = e.client_id INNER JOIN TbContacts co ON c.id = client_co.id;

Once the VwClientesPlugSales view is created, you can select that view in the dashboard and PlugBot can simply perform a simple SELECT * FROM VwClientesPlugSales to obtain the necessary data.

Captura da tela 2

And when loading the data from this view, whether directly from the table, or using an SQL query, the data will already be formatted correctly in the columns that PlugSales expects!

GIF demonstrativo