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.

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.