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
  • What happen if you remove function invocation, and have only function declaration or if you remove both lines?

    If you are getting error 500, that mean that you have apache server error, which is probably related to url that is not formatted correctly.

    Maybe you can try to use Immediately invoked function expression(IIFE). Have no idea if that would help.

    However, generally you shouldn't have so many lines in onclick html property, so I would rather create new button type and attach event listeners for onclick event, so it would allow you to add more custom code and it would be easier to maintain.

Reply
  • What happen if you remove function invocation, and have only function declaration or if you remove both lines?

    If you are getting error 500, that mean that you have apache server error, which is probably related to url that is not formatted correctly.

    Maybe you can try to use Immediately invoked function expression(IIFE). Have no idea if that would help.

    However, generally you shouldn't have so many lines in onclick html property, so I would rather create new button type and attach event listeners for onclick event, so it would allow you to add more custom code and it would be easier to maintain.

Children