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

sugarcrmdevelopers —  April 12, 2013

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 '
                          function custom_validation() {
                                // Your code here
                          }
                      ';
        }
}

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 = elems.length; i < maxl; i++)
{
      // Check if the value of the current element is Save
      if (elems[i].value == "Save")
      {
          // Override the onclick event with our custom function
	  elems[i].setAttribute("onclick", onclickFunc);
      }
}

4. Run Quick Repair and Rebuild

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