+91 9717203377

Home » SugarCRM » Add javascript in sugarcrm view

Add javascript in sugarcrm view

Many time developers need to add custom Javascript to a SugarCRM View. The important thing should keep in mind is that code would be upgrade safe so that any future enhancement in core files should not affect your modification.
Here we are going to implement custom javascript in view for all edition of sugarcrm and it is upgrade safe
This requirement is necessary for developers when creating new functionality inside sugarCRM which need some javascript code

Overriding the view class of module can be a good way to fulfil our requirement So first create a view file in custom if it is not available file can be view.detail.php, view.edit.php or view.list.php
then override the class View and then adding javascript code snippet before the display() method of the particular view class.
We can include javascript files by this method, and can also be used to output javascript code directly to the view.

Here are steps
1. Create view file if not present custom/modules/<modulename>/views/view.<viewname>.php. In my case I am using Reports module and detail view, So my path will be custom/modules/reports/views/view.detail.php
2. Following Code is for adding javascript.

require_once('include/MVC/View/views/view.detail.php');

 class ReportsViewDetail extends ViewDetail {
 function ReportsViewDetail() {
 parent::ViewDetail();
 }

 function display(){
 echo '<script type="text/javascript" src="custom/js/logics.js"></script>'; //js file
 echo '<script type="text/javascript">alert('Hi')</script>';
 parent::display();
 }

 }

If you are not able to see modification on browser Of course Repair and rebuild from admin. Go to Admin -> Repair -> Quick Repair

Happy Coding..