Functions for creating one to many relationships in Vtiger CRM

Functions for creating one to many relationships in Vtiger CRM

Programmers and developers are wondering: "How to connect modules in Vtiger CRM among themselves?". At the same time, the most common connection between modules is one to many.

This connection means that in the card of the parent (main) module there is a field in which you can select the desired record of the subsidiary module. And in the card of the subsidiary module there is a tab, when you click on it you can see all the records associated with this card.

Thus, in order to properly organize a one-to-many relationship in Vtiger CRM, we need to do the following:

  • in the first module, we create a field that connects us to the record of the child module;
  • in the second module, we create a linked list that will allow us to view the records that were selected through this field.

In other words, we need two functions. They can be used in work, for example, when you write custom functions or scripts to create a module. Their use will allow you to quickly organize a one-to-many relationship between two modules.

public function createRelationship($module1, $module2, $fieldLabel)

    {

     include_once "vtlib/Vtiger/Module.php";

        $block = 'LBL_CUSTOM_INFORMATION';

        $module = Vtiger_Module::getInstance($module1);

        $module1Class = Vtiger_Module::getClassInstance($module1);

        $block1 = Vtiger_Block::getInstance($block, $module);

        $columnName = "";

        if ($actionRelatedList == true) {

            $columnName = "cf_nrl_" . strtolower($module2) . rand(1, 1000) . "_id";

        } else {

            $columnName = "cf_" . strtolower($module2) . "_id";

        }

        $fieldName = $columnName;

        $field1 = new Vtiger_Field();

        $field1->label = $fieldLabel;

        $field1->name = $fieldName;

        $field1->table = $module1Class->table_name;

        $field1->column = $fieldName;

        $field1->generatedtype = 2;

        $field1->columntype = "INT(10)";

        $field1->uitype = 10;

        $field1->typeofdata = "I~O";

        $field1->quickcreate = 2;

        $field1->sequence = 7;

        $block1->addField($field1);

        $field1->setRelatedModules(array($module2));

        $block1->save($module);

        return $field1->id;

    }

The createRelationship function allows us to create in the first module (module1) a field associated with the second module (module2), which will allow us to select the necessary records from the second module and associate them with the first. The function will create a field in the format cf_potentials_id, where potentials is the name of the associated module. The field will be created in the LBL_CUSTOM_INFORMATION block.

The function accepts the following parameters:

  • module1 - the main module in which the field with the associated module will be added,
  • module2 - the module with which we plan to communicate through this field,
  • fieldLabel - as we plan to name our new field. It is advisable to name the fields in English and then translate it through translation files.

After we made communication with the second module in our main module, now in the second module we should see all the records that are currently attached to it. To do this, we need to display the linked lists tab, when clicked, the related records we need are displayed. You can do this through another function:

 

    /**

     * @param $module1

     * @param $module2

     * @param $relListLabel

     */

    public function addRelatedList($module1, $module2, $relListLabel, $actions)

    {

        include_once "vtlib/Vtiger/Module.php";

        $module = Vtiger_Module::getInstance($module2);

        $module->setRelatedList(Vtiger_Module::getInstance($module1), $relListLabel, $actions, "get_dependents_list");

    }

 

The function accepts the following parameters:

  • module1 - the module in which we plan to display a linked list,
  • module2 - the module in which we previously created the field and which will display related records,
  • relListLabel - the heading of the list, usually it matches module2,
  • actions - accepts an array of actions that can be done in the list. For example: array ('add', 'select') - this array will allow you to add new and select existing entries in the linked list.

Attention! Use these functions at your own risk. And do not forget to backup data before using it. The changes are irreversible.

If you still have questions, I will be happy to answer them through the feedback form.

Popular Posts

My most popular posts

Maximum productivity on remote job
Business

Maximum productivity on remote job

I started my own business and intentionally did my best to work from anywhere in the world. Sometimes I sit with my office with a large 27-inch monitor in my apartment in Cheboksary. Sometimes I’m in the office or in some cafe in another city.

Hello! I am Sergey Emelyanov and I am hardworker
Business PHP

Hello! I am Sergey Emelyanov and I am hardworker

I am a programmer. I am an entrepreneur in my heart. I started making money from the age of 11, in the harsh 90s, handing over glassware to a local store and exchanging it for sweets. I earned so much that was enough for various snacks.

Hire Professional CRM developer for $25 per hour

I will make time for your project. Knowledge of Vtiger CRM, SuiteCRM, Laravel, and Vue.js. I offer cooperation options that will help you take advantage of external experience, optimize costs and reduce risks. Full transparency of all stages of work and accounting for time costs. Pay only development working hours after accepting the task. Accept PayPal and Payoneer payment systems. How to hire professional developer? Just fill in the form

Telegram
@sergeyem
Telephone
+4915211100235