Adding custom JavaScript to a view in SugarCRM

If you need to add custom JavaScript to a view in SugarCRM the first thing you need to do is create your script file and place it in the custom folder of the module you want to add the client side functionality to.

Example:

custom/modules/YOURMODULE/customscript.js

Then add your script file to includes part of the viewdefs file. In this example I’m adding it to the edit view of the Opportunities module.

custom/modules/Opportunities/metadata/editviewdefs.php

/Add javascript to editviewdefs
$viewdefs ['Opportunities'] = 
array (
  'EditView' => 
  array (
    'templateMeta' => 
    array (
      'maxColumns' => '2',
      'widths' => 
      array (
        0 => 
        array (
          'label' => '10',
          'field' => '30',
        ),
        1 => 
        array (
          'label' => '10',
          'field' => '30',
        ),
      ),
      'includes' => 
      array (
        0 => 
        array (
          'file' => 'custom/modules/Opportunities/lost_opportunity.js', //call for the js file
        ),
      ), 


An alternative method of adding the JavaScript would be to create a new view for the particular module and then echo out the link to the script file or you could even echo out the JavaScript code directly in the view.

In the example below I create a new edit view in the calls module and then add the JavaScript. I dont want to alter anything so I just call the parent display method and add the script afterwards.

custom/modules/Calls/views/view.edit.php


class CallsViewEdit extends ViewEdit
{

     public function display(){

        parent::display();
        
        echo '<script type="text/javascript">
             var current_status = document.getElementById("status");
                if(current_status.value == "Held"){
                    //do something
                }
             </script>';

     }

}
Andy
About

Software Developer from Falkirk, Central Scotland.

Posted in SugarCRM development development hints and tips
One comment on “Adding custom JavaScript to a view in SugarCRM
  1. hafez says:

    Hi Thank you for the post, but can we add java script to sub-panel in related module, Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>