SugarCRM: Set a custom javascript function upon clicking the Save button in Quick Create View

This example will use the Cases module as the one that will use the quick create.

1. Go to custom/modules/Cases/views/view.classic.php. If the file doesnt exist then create it and input the codes below:

<?php

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

class CustomCasesViewClassic extends ViewClassic
{
	public function preDisplay()
	{
		parent::preDisplay();
                echo '<script type="text/javascript"> 
                          function custom_validation() {
                                // Your code here
                          }
                      </script>';
        }
}

2. Input your function inside the preDisplay after parent::preDisplay.

3. You have to change the onclick attribute of the Save button to perform the custom javascript function first before calling the default save function. Look at the codes below:

// Get the form name of the subpanel
var form = document.getElementById('form_SubpanelQuickCreate_Cases');
// Get the elements under the form
var formElements = form.getElementsByTagName("*");
// Set the custom onclick event that we will assign the save button
var onclickFunc = "var _form = document.getElementById(\'form_SubpanelQuickCreate_Cases\'); disableOnUnloadEditView(); _form.action.value=\'Save\';if(custom_validation(\'form_SubpanelQuickCreate_Cases\'))return SUGAR.subpanelUtils.inlineSave(_form.id, \'Cases_subpanel_save_button\');return false;";

// Loop through the form elements
for(var i=0, maxl = formElements.length; i < maxl; i++)
{
      // Check if the value of the current element is Save
      if (formElements[i].value == "Save")
      {
          // Override the onclick event with our custom function
	  formElements[i].setAttribute("onclick", onclickFunc);
      }
}

* Take note that you need to change the form id and some of the codes for the onclick event

4. Run Quick Repair and Rebuild

5. Check if the onclick event of the Save button is changed.

About these ads

One thought on “SugarCRM: Set a custom javascript function upon clicking the Save button in Quick Create View

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s