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

  • 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

  • Thanks for your reply!

    All issue resolved now i am getting following response from ajax call:

    Not A Valid Entry Point.

     I have debuged the code and found that request is correct and reaching to the server side PHP mentioned by us.

    But  it is throwing die part of the file.

    I am analyzing it,  i will appreciate if you can help me to understand Why it is happening ? so that i can proceed further.

    Regards,

    Deepak

  • Hi Deepak

    Can you past code of your ajax call.

    So that i can help you further.

    Best Regards

    S Ramana Raju


  • function testPBConnection(){
    var formvalue1 = $('#accountid_c').val();
    var formvalue2 = $('#password_c').val();
    var formvalue3 = $('#port_c').val();
    var formvalue4 = $('#servername_c').val();
    var formvalue5 = $('#name').val();
    //console.log("Hi Friends am Triggered.. ", formvalue1,formvalue2,formvalue3,formvalue4);
    //alert('Hi Friends am Triggered Bill To');
    alert("Hi Friends am Triggered.. " + "Account ID : " + formvalue1 +
    "Password : " + formvalue2 + "Port : " + formvalue3 + "Server Name : " + formvalue4
    + "Config Name : " + formvalue5 );

    $.ajax({
    type: 'POST',
    url: "custom/modules/PB_PitneyBowesEntity/test_connection.php",
    data: {formdata: formvalue1 },
    success:function(fdata){
    console.log(" Success on it.. ");
    },
    error: function(fdata) {
    console.log(" With Error in it.. ");
    }
    }); // End of ajax

    }// End test Connection

  • Hi Deepak

    This Creating_Custom_Entry_Points Article may help you in fixing the issue.

    Let me know for further Help.

    Best Regards

    S Ramana Raju

  • Hi Ramana,

    Thanks for your reply!

    I am analyzing the root cause. will update you. soon. i am able to call php file.

     I saw the console in mozilla and the console is getting printed with "console.log(" Success on it.. ");" and in call back function value is "Not A Valid Entry Point".

    so it  is clear that some thing is wrong in php file will analyze and update you.

    Thanks for your support and guidance.

Reply Children
No Data