How can i call a method (From sugar server) on the  click of custom added button?

Hi Team,

I have added a custom button on custom entity next to SAVE and CANCEL.

On the click of custom button i want to pick some value from the FORM and want to send it to one of the custom exposed method. this call should go through the sugar server so that we do not face CORS issue.

Similar thing I have done on the click of SAVE button and our business logic is inside  logic hooks.

Now i want to call the same custom method from the custom button how can i achieve it.

What is right way to achieve it.

Thanks in advance.

Regards,

Deepak

  • Hi Parag Mittal

    Check this post for creating custom button 

    Now in your custom/include/javascript/test.js file write all your script code to do so:
    $(document).ready(function(){
       function myButtonFunt(){           

             var formvalue = $("#field-name-id").val();

             console.log("Hi Friends am Triggered.. ", formvalue);

             $.ajax({
                type: 'POST',
                url: "custom/modules/<module-name>/saveChanges.php",
                data: {formdata: formvalue},            
                success:function(fdata){
                      console.log(" Success on it.. ");                 
             },
             error: function(fdata) {                  
                   console.log(" With Error in it.. ");
          }
       }); // End of ajax

       } // End of myButtonFunt
    });

    -- Under saveChange.php write your logic:
    <?php
    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    require_once('include/entryPoint.php');

    // here write all your logic which you want

    echo $_REQUEST['formdata'];

    Hope this Helps..!!

    Best Regards

    S Ramana Raju

  • Thanks Ramana for your Reply.

    I have attached the screen shot for better understanding.

    In the above image you can see i have created a custom entity.

    On this entity i want to add Test Button next to Cancel.

    I have made  following changes (changes are in bold letters) in the file editviewdefs.php

    under the path custom/modules/CustomEntity/metadata/

    <?php
    $module_name = 'PB_PitneyBowesEntity';
    $viewdefs [$module_name] =
    array (
    'EditView' =>
    array (
    'templateMeta' =>
    array (
    'maxColumns' => '2',
    'widths' =>
    array (
    0 =>
    array (
    'label' => '10',
    'field' => '30',
    ),
    1 =>
    array (
    'label' => '10',
    'field' => '30',
    ),
    // Add Custom Code
    2 =>
    array (
    'buttons' =>
    array (
    'customCode' => '<input id="TestButton" title="Test Button" class="button" type="button" name="TestButton" value="Test Button" onclick="myButtonFunt();">',
    ),
    ),

    But it is not visible in Custom module.

    Please suggest What i have missed.

    What am i missing in following code share by you?

    templateMeta' => 
        // Add this code
        array (        
           'javascript' => '       
                <script src="./include/javascript/jquery.min.js" type="text/javascript"></script> 
                <script src="./custom/include/javascript/test.js" type="text/javascript"></script> 
           ', 
        ),        
        array (
           'buttons' =>
            array (
              0 => 'SAVE',
              1 => 'CANCEL',
              // Add This code
              2 =>
              array (
                'customCode' => '<input id="MyButton" title="My Button" class="button" type="button" name="MyButton" value="My Button" onclick="myButtonFunt();">',
            ),
        ),

    Regards,

    Deepak

  • Hi Ramana,

    later i have added following code (In bold letters) in editviewdefs.php and able to see the test Connection button next to SAVE and CANCEL.

    <?php
    $module_name = 'PB_PitneyBowesEntity';
    $viewdefs [$module_name] =
    array (
    'EditView' =>
    array (
    'templateMeta' =>
    array (
    // START  Custom Code
    'form' =>
    array (
    'buttons' =>
    array (
    0 => 'SAVE',
    1 => 'CANCEL',
    2 =>
    array (
    'customCode' => '<input id="TestButton" title="Test Button" class="button" type="button" name="TestButton" value="Test Connection" onclick="testConnection();">',
    ),
    ),
    ),

    // END custom Code
    'maxColumns' => '2',
    'widths' =>

  • Hi Parag Mittal

    I will write it for you the correct code.

    Can you give your editviewdefs.php full code.

    Best Regards

    S Ramana Raju

  • Hi Parag Mittal

    Sorry for wrong code there is slight syntax error in that code.

    Following is the correct 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 => 'SAVE',
              1 => 'CANCEL',
              // Add This code
              2 =>
      array(
                   'customCode1' => '<input id="BillToValidateButton" title="Bill To Button" class="button" type="button" name="BillToButton" value="Bill To Validate" onclick="myButtonFunt();">',
                  ),
              ),
          ),
      ),

    Once check this and let me know.

    Best Regards

    S Ramana Raju

  • Hi Ramana,

    I am able to see the test connection button next to SAVE and CANCEL.

    In my case following is path of js files.

    <script src="./include/javascript/jquery.js" type="text/javascript"></script>

    <script src="./include/javascript/jquery/jquery.min.js" type="text/javascript"></script>

    <script src="./custom/include/javascript/test.js" type="text/javascript"></script>

    Following us the test.js code:

    $(document).ready(function(){
    function testConnection(){
    //console.log("Hi Friends am Triggered Bill To..");
    alert('Hi Friends am Triggered Bill To');
    }
    });

    Following is the code of editviewdefs.php:

    <?php
    $module_name = 'PB_PitneyBowesEntity';
    $viewdefs [$module_name] =
    array (
    'EditView' =>
    array (
    'templateMeta' =>
    array (
    // START Custom Code
    array (
    'javascript' => '
    <script src="./include/javascript/jquery.js" type="text/javascript"></script>
    <script src="./custom/include/javascript/test.js" type="text/javascript"></script>
    '),
    'form' =>
    array (
    'buttons' =>
    array (
    0 => 'SAVE',
    1 => 'CANCEL',
    2 =>
    array (
    'customCode' => '<input id="TestButton" title="Test Button" class="button" type="button" name="TestButton" value="Test Connection" onclick="testConnection();">',
    ),
    ),
    ),
    // END custom Code
    'maxColumns' => '2',
    'widths' =>
    array (
    0 =>
    array (
    'label' => '10',
    'field' => '30',
    ),
    1 =>
    array (
    'label' => '10',
    'field' => '30',
    ),
    ),
    'useTabs' => false,
    'tabDefs' =>
    array (
    'DEFAULT' =>
    array (
    'newTab' => false,
    'panelDefault' => 'expanded',
    ),
    'LBL_EDITVIEW_PANEL1' =>
    array (
    'newTab' => false,
    'panelDefault' => 'expanded',
    ),
    ),
    ),
    'panels' =>
    array (
    'default' =>
    array (
    0 =>
    array (
    0 => 'name',
    1 => 'assigned_user_name',
    ),
    1 =>
    array (
    0 => 'description',
    ),
    ),
    'lbl_editview_panel1' =>
    array (
    0 =>
    array (
    0 =>
    array (
    'name' => 'servername_c',
    'label' => 'LBL_SERVERNAME',
    ),
    1 => '',
    ),
    1 =>
    array (
    0 =>
    array (
    'name' => 'port_c',
    'label' => 'LBL_PORT',
    ),
    1 => '',
    ),
    2 =>
    array (
    0 =>
    array (
    'name' => 'accountid_c',
    'label' => 'LBL_ACCOUNTID',
    ),
    1 => '',
    ),
    3 =>
    array (
    0 =>
    array (
    'name' => 'password_c',
    'label' => 'LBL_PASSWORD',
    'type' => 'Password',
    ),
    1 => '',
    ),
    ),
    ),
    ),
    );
    ?>

    please help me why it is not getting trigger.

    Regards,

    Deepak

  • Hi Deepak

    When you click on Test Connection button are you getting any error.

    Can you check it in your console and let me know if there are any errors.

    Best Regards

    S Ramana Raju

  • Following is the error:

    index.php?module=PB_PitneyBowesEntity&action=EditView&return_module=PB_PitneyBowesEntity&return_act…:479 Uncaught ReferenceError: testConnection is not defined

  • Hi Deepak

    I tried in my Account module with below code and is working for me.
    Can you try in this way and let me know for further help.
    In custom/modules/Accounts/metadata/editviewdefs.php i have added following code:

    'templateMeta' => 
        array (
          'form' =>
          array (
            'buttons' =>
            array (
              0 => 'SAVE',
              1 => 'CANCEL',
              2 =>
                   array (
                   'customCode' => '<input id="TestButton" title="Test Button" class="button" type="button" name="TestButton" value="Test Connection" onclick="testConnection();">',
                   ),
            ),
          ),
          'includes' =>
          array (
            0 =>
            array (
              'file' => 'custom/modules/Accounts/test.js',
            ),
          ),
        ),

    Here is my test.js code ( custom/modules/Accounts/test.js):

    function testConnection()
    {
    alert("Hi Friends am triggered..");
    }

    Also dont forget to change module name according to your usage and let me know for further help.
    Hope this Helps..!!

    Best Regards
    S Ramana Raju