IMPORTANT NOTE: The following information is only valid for SugarCRM 6.5 and older. This information does not apply to newer versions of the product.  

When I started digging into customizing SugarCRM I often found it difficult to follow along with suggested solutions.  I mainly searched the SugarCRM forums for help on specific questions.  Most of the time I could find sample code but not the information on how to use it.  In this blog post I'll show how to create a very basic onload and onchange event for a module.  The specific example I will work towards is implementing a Subtype dropdown based on the Type dropdown in the Cases module.

The below post assumes you have a basic knowledge of SugarCRM, the studio, upgrade safe customizations and the custom directory. If you have any questions feel free to ask in the comments section.

The first step is to have an action fire when the edit view of the module is loaded (onload event) and when the Type dropdown is changed (onchange event). In my example I've already created the Subtype dropdown field and added it to the Cases module:

SugarCRM Type and Subtype

Because I configured the Cases Edit View Layout in the Studio, the custom file I need to edit now exists at:
..custommodulesCasesmetadataeditviewdefs.php

Note that this file may not look very clean if you open it in a general text editor such as Notepad. You can use the free source code editor and Notepad replacement Notepad++ for a better experience, http://notepad-plus-plus.org/.

For the onload event I need to add to the 'templateMeta' array in editviewdefs.php:
'javascript' => '<script type="text/javascript"> onload=alert("OnLoad"); </script>',

I also need to add the onchange event to my type dropdown. To do this I add the 'displayParams' property to the Type field:

  1. 4 =&gt;
  2. 0 =&gt;
  3. 'name' =&gt; 'type',
  4. 'label' =&gt; 'LBL_TYPE',
  5. 'displayParams' =&gt;
  6. 'javascript' =&gt; 'onchange=alert("OnChange");',
  7. ),
  8. ),
  9. ),

In this case, 4 is the row number and 0 is the only field on that row. Depending on your layout and the name of your field this may be different.

Making these changes to the editviewdefs.php file should cause a popup window to show when you edit a Case and when you change the Case's type. In my next post, I will look at having the events call a custom JavaScript function.

Posted in:

Looking for SugarCRM help?

We do training, customization, integration, and much more. Contact us today.