Add javascript in the onclick of a custom button

Hi,

I work with the 7.6 pro version of Sugar and I have added a custom button in the module Quotes that follows the old standard, the 6.7 version. 

I have added the button with this code in the detailviewdefs.php:

'buttons' => 
array (
  0 => 'EDIT',
  1 => 'SHARE',
  2 => 'DUPLICATE',
  3 => 'DELETE',
  4 =>
  array (
    'customCode' => '<form action="index.php" method="POST" name="Quote2Opp" id="form">

            <input id="apriConfiguratoreOfferta" title="apriConfiguratoreOfferta" class="btn" type="button" name="apriConfiguratoreOfferta" value="Apri Configuratore"
            onclick="
            var URL=document.URL;
            var idOfferta = URL.match(\'record=(.*)&\');
            alert(idOfferta[1]);
            sessionStorage.selectedQuoteId=idOfferta[1];
            var arg = \'/Dashboards\';
            var a=app.api.buildURL(arg);
            alert(a);
            function fun(){alert(\'Function call\')};
            fun();
            var homeURL = \'http://localhost/sugarcrm/#Home/3f7529b1-d575-6981-6097-574becea9c9e\';
            var win = window.open(homeURL, \'_self\');

            ">

in the onclick event of the button I want to do some stuff and one of these stuff is the use of a function but, when I declare it, Sugar shows me a 500 error in the browser and it doesn't load the Quotes page.

My problem is that I need to use the function declaration for the callback of an ajax request but in this manner I can't do this.

In the posted code, the error is shown when I insert the 'function fun()' declaration.

Can you help me? 

Thanks

Stefano

Parents Reply Children
  • Hi Stefano Mapelli

    In your custom/modules/<module-name>/metadata/detailviewdefs.php and add this code:

    'templateMeta' => 
      array (
         'javascript' => '
           <script src="./include/javascript/jquery.min.js" type="text/javascript"></script>
           <script src="./custom/include/javascript/test.js" type="text/javascript"></script>
         ',
         'form' =>
          array (
            'buttons' =>
              array (
                0 => 'EDIT',
                1 => 'SHARE',
                2 => 'DUPLICATE',
                3 => 'DELETE',
                4 =>
                  array(
                   'customCode1' => '<input id="BillToValidateButton" title="Bill To Button" class="button" type="button" name="BillToButton" value="Bill To Validate" onclick="myButtonFunt();">',
                  ),
              ),
          ),
      ),


    Now in your custom/include/javascript/test.js file write all your script code to do so:

    $(document).ready(function(){ 
      function myButtonFunt(){
        console.log("Hi Friends am Triggered..");
      }
    });

    Once check with this approach and let me know if you found any issues.
    Hope this Helps

    Best Regards

    S Ramana Raju