SugarCRM Adding Custom Button For Printing Labels Using Javascript

Do you like this?

Summary:
This article shows you how to add a custom button for printing address labels. You will be given steps to add the button in the DetailView of the Accounts module. The same method can be used for any other modules and can also be used for the EditView.


Content:

The following screenshot shows you the end result:
SugarCRM Adding Custom Button Using Javascript 1

This is how the address label will be printed using a browser:
SugarCRM Adding Custom Button Using Javascript 2

Now let's start implementing this feature. Open the file <sugar_crm_installation>\custom\modules\Accounts\metadata\detailviewdefs.php and add the following code.

	array(
                'customCode' => '        
    <input type="button" value="Print Label" onclick="printLabel(\'\
	  <b><font size=20px> \
      <font size=40px>{$fields.name.value}</font><br />\
      {$fields.shipping_address_street.value}<br />\
      {$fields.shipping_address_city.value}<br />\
      {$fields.shipping_address_state.value},\
      {$fields.shipping_address_postalcode.value}<br />\
      {$fields.shipping_address_country.value}<br />\
      PHONE: {$fields.phone_office.value}<br />\
	  </font></b>\
    \')">'
        ), 
SugarCRM Adding Custom Button Using Javascript 3

In the same file, add this extra code for the includes entry:
array('file'=>'custom/modules/Accounts/metadata/printlabel.js'),
SugarCRM Adding Custom Button Using Javascript 4

As you can see, we have to create this file custom/modules/Accounts/metadata/printlabel.js.
function printLabel(content) {
  labelPopup=window.open('','Print Address','width=500,height=300');
  labelPopup.document.writeln(content);
  labelPopup.document.close();
}
SugarCRM Adding Custom Button Using Javascript 5

Other examples on adding custom actions:

 
comments powered by Disqus