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

Parents
  • 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

Reply
  • 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

Children
  • 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 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