0

I am trying to add custom jQuery script in either header or footer of Accounts edit view, searched everywhere in Google but can't find an hook that prints scripts in header or footer.

My goal is to add custom validation for my custom fields, but as soon as I add jQuery code the page gets halted.

I am inserting following code in custom/modules/Accounts/metadata/editviewdefs.php

$viewdefs ['Accounts'] = array(
    'EditView' => array(
        'templateMeta' => array(
            'javascript' => '<script type="text/javascript">
            $(document).ready(function(){
                alert("This is my custom javascript code");
            });
            </script>',

Above code produces below screen.

Screen after inserting jQuery

I remove the jQuery part like this

$viewdefs ['Accounts'] = array(
    'EditView' => array(
        'templateMeta' => array(
            'javascript' => '<script type="text/javascript">

                alert("This is my custom javascript code");

            </script>',

I see following alert Simple javascript works fine

I see that the simple JavaScript code is working fine but problem occurs only when I add jQuery code, I tried adding jQuery library before above code but that did not solve the problem.

Can someone tell what I am missing here?

Tahir Yasin
  • 10,631
  • 5
  • 38
  • 58

1 Answers1

4

You also need to wrap the js with {literal} tags:

$viewdefs ['Accounts'] = array(
'EditView' => array(
    'templateMeta' => array(
        'javascript' => '<script type="text/javascript">
        {literal}
        $(document).ready(function(){
            alert("This is my custom javascript code");
        });
        {/literal}
        </script>',
egg
  • 1,706
  • 10
  • 11
  • Works like a charm! Now I know one should never neglect Smarty syntax while working in SugarCRM. :) – Tahir Yasin Sep 09 '13 at 04:58
  • Although after adding `literal` tags page is not breaking, but the jQuery binding is not working for form submit event as I want to do some validation before submitting form. Please check this code, what I am doing wrong there? http://pastebin.com/wxbbvUPx – Tahir Yasin Sep 09 '13 at 12:02
  • SugarCRM does some custom js upon button click that bypasses the form submit. You'll need to hijack those buttons instead of the form submit. – egg Sep 09 '13 at 15:11
  • 1
    You can override form submit with this method : http://developers.sugarcrm.com/wordpress/2013/08/19/sugarcrm-custom-form-validation-with-underscorejs/ – Cédric Mourizard Sep 10 '13 at 11:19