SugarCRM 7 - Adding a custom column to a list

Posted on Thu 22 May 2014 in SugarCRM

This is the SugarCRM 7 equivalent to this post here, because Sugar has dropped the process record logic hook, we need to come up with a new solution.

Here's how you can add a new column to a list view, I have to admit this is one of the really powerful additions to SugarCRM 7, no more crazy config files or hacks to try cheat your data in. By leveraging javascript events with a decent restful API you can come up with some really nice solutions, with lots of hooks to do custom things. Once you see the difference in implementation you'll see why I'm so pleased.

So anyway, back to the customisations.

1. Add the placeholder column

You need to do this by adding a new column into your custom modules list.php file, which is generally located at custom/modules/YOUR_CUSTOM_MODULE/clients/base/views/list/list.php, add your new column in under $viewdefs[$modulename]['base']['view']['list']['panels'][0]['fields'] and add another array element in like:

array (
                'name' => 'custom_fieldname',
                'label' => 'LBL_CUSTOM_RECORD',
                'enabled' => true,
                'width' => '10%',
                'default' => true,
              ),

2. Add the JS action

This should go into custom/modules/YOUR_CUSTOM_MODULE/clients/base/views/recordlist/recordlist.js

({

    extendsFrom: 'RecordlistView',
    bindDataChange: function() {
        this.before('render', function() {
            return this.beforeRenderCallback()
        }, this);
        this.on('render', function() {
            this.renderCallback();
            if (this.previewVisible) {
                this.decorateRow(this.previewModel);
            }
        }, this);
        this._super("bindDataChange");
    },
    /**
     * Callback for before on('render') event
     */
    beforeRenderCallback: function() {
        this.collection.each(function(model) {
            app.api.call('GET', app.api.buildURL('YOUR_CUSTOM_MODULE/YOUR_CUSTOM_FUNCTION/'+model.id), null, {
                success: _.bind(function(data) {
                    model.set({
                        your_custom_field : data
                    });
                }, this)
            });
        }, this);
    },
})

3. Add the API end point

The documentation here is really straightforward but as an example for completeness this should return a json response with the data needed.

This code would go into custom/modules/YOUR_CUSTOM_MODULE/clients/base/api/YOUR_CUSTOM_FUNCTION.php

<?php

if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
require_once('custom/modules/YOUR_CUSTOM_MODULE/Custom_Bean.php');

class YOUR_CUSTOM_FUNCTION extends SugarApi
{
    public function registerApiRest()
    {
        return array(
            //GET
            'MyGetEndpoint' => array(
                //request type
                'reqType' => 'GET',

                //endpoint path
                'path' => array('YOUR_CUSTOM_MODULE', 'YOUR_CUSTOM_FUNCTION', '?'),

                //endpoint variables
                'pathVars' => array('', '', 'data'),

                //method to call
                'method' => 'MyGetMethod',

                //short help string to be displayed in the help documentation
                'shortHelp' => 'Some help text',

                //long help to be displayed in the help documentation
                //'longHelp' => '',
            ),
        );
    }

    /**
     * Method to be used for my MyEndpoint/GetExample endpoint
     */
    public function MyGetMethod($api, $args)
    {
        $id = $args;

        $custom_bean = new Custom_Bean();
        $custom_bean->retrieve($id);

        return $custom_bean->getCustomColumnData();
    }

}

4. Add to column to the dictionary

This code goes in here: custom/Extension/modules/YOUR_CUSTOM_MODULE/Ext/Vardefs/sugarfield_custom_fieldname.php

<?php
$dictionary['YOUR_CUSTOM_MODULE']['fields']['custom_fieldname']['labelValue']='Custom Row ';
$dictionary['YOUR_CUSTOM_MODULE']['fields']['custom_fieldname']['full_text_search']=array (
  'boost' => '0',
  'enabled' => false,
);
$dictionary['YOUR_CUSTOM_MODULE']['fields']['custom_fieldname']['enforced']='';
$dictionary['YOUR_CUSTOM_MODULE']['fields']['custom_fieldname']['dependency']='';
 ?>

On quick repair/rebuild this will get merged into custom/modules/YOUR_CUSTOM_MODULES/Ext/Vardefs/vardefs.ext.php

5. No need to even guess, do a quick repair/rebuild.