Live update list view in Vtiger CRM

Live update list view in Vtiger CRM

Recently one of my client asked me to implement interesting functionality in vtiger crm. Most of the employees used the Contacts module. And in the list view constantly added more and more new customers. The client wanted to see how new contacts appear in the table without having to reload the page.

For example, as soon as a user added a new contact, an entry immediately appeared for all other employees.

What is most interesting, it is implemented quite simply. It is enough to add two files.

First file: modules/Contacts/actions/LiveUpdateAjax.php

With following content:

<?php

 

class Contacts_LiveUpdateAjax_Action extends Vtiger_Action_Controller

{

    public function checkPermission(Vtiger_Request $request)

    {

    }

 

    public function __construct()

    {

        $this->exposeMethod("getContacts");

        $this->exposeMethod("getLastId");

    }

 

    public function process(Vtiger_Request $request)

    {

        $mode = $request->get("mode");

        if (!empty($mode)) {

            $this->invokeExposedMethod($mode, $request);

        }

    }

 

    public function getContacts(Vtiger_Request $request)

    {

        $response = new Vtiger_Response();

        global $adb;

        $result = array();

        $res = $adb->pquery('SELECT contactid FROM vtiger_contactdetails INNER JOIN vtiger_crmentity ON vtiger_contactdetails.contactid = vtiger_crmentity.crmid WHERE contactid > ? AND vtiger_crmentity.deleted = 0 ORDER BY contactid DESC', array($request->get('record')));

        if (0 < $adb->num_rows($res)) {

            while ($row = $adb->fetchByAssoc($res)) {

                $result[] = array('id' => $row['contactid']);

            }

        }

        $response->setResult($result);

        $response->emit();

    }

 

    public function getLastId(Vtiger_Request $request)

    {

        global $adb;

        $res = $adb->pquery('SELECT contactid FROM vtiger_contactdetails ORDER BY contactid DESC LIMIT 1');

        $contactid=$adb->query_result($res,0,'contactid');

        $response = new Vtiger_Response();

        $response->setResult(array('id' => $contactid));

        $response->emit();

    }

}

 

Second file: layouts/v7/modules/Contacts/resources/List.js

With content:

Vtiger_List_Js("Contacts_List_Js", {

    lastId: false,

}, {

    registerUpdateContactsEvent: function () {

        var self = this;

        var params = {

            module: 'Contacts',

            action: 'LiveUpdateAjax',

            mode: 'getLastId'

        }

        app.request.post({data: params}).then(

            function(err,data) {

                if (err === null) {

                    Contacts_List_Js.lastId = data.id;

                    setInterval(self.runUpdate, 10000);

                }

            }

        )

    },

    runUpdate: function() {

        var params = {

            module: 'Contacts',

            action: 'LiveUpdateAjax',

            mode: 'getContacts',

            record: Contacts_List_Js.lastId

        }

        app.request.post({data: params}).then(

            function(err,data) {

                data.forEach(function(entity) {

                    Contacts_List_Js.lastId = entity.id;

                });

                if(data.length > 0) {

                    var listInstance = new Vtiger_List_Js();                    

                    listInstance.loadListViewRecords();

                }

            }

        );

    },

    registerEvents : function() {

        this._super();

        this.registerUpdateContactsEvent();

    }

});

How it all works:

  1. Javascript when loading the page receives the last contact id, which is currently in the database. Those it puts a bookmark.
  2. After setting the bookmark, we setting a timeout in 10 seconds for running the function.
  3. Every 10 seconds, a function is launched that looks to see if new contacts have appeared after the bookmark has been set. And if they do, then the table is updated by ajax (the page does not reload at the same time).

In such a simple way, you can achieve the rapid appearance of new records in the table when they appear in the database.

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