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

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

  • Hi Ramana,

    I have moved to 7.7 version and want to do the same thing.

    I have done changes in record.php file and able to see test button on my custom module .

    but it is not coming while click on create record there i only see CANCEL and SAVE i want to add this custom button NEXT to SAVE as we done in 6.5.

    but if i create one record and edit that record then i see it next to edit button.

    Can you help me to resole it.

  • Hi Deepak

    In sugarcrm 7 we need to do it in different way.

    In custom/modules/module_name/clients/base/views/record/record.php

    array(
                'type' => 'button',
                'name' => 'your_button',
                'label' => 'LBL_YOUR_BUTTON_LABEL',
                'css_class' => 'btnYBClick',

    Now in custom/modules/module_name/clients/base/views/record/record.js file add this code:

    In initialize function add:

    this.model.on('click .btnYBClick', this.buttonCalled, this);

    After add above line, outside of initialize function we need to define our function as:

    buttonCalled:function(){
    // Write your code here
    alert("Friend am triggered.. ");
    },

    Hope this Helps..!!

    Best Regards

    S Ramana Raju

  • Hi Ramana,

    Thanks for your reply!

    Can we do all this things in Extension framework for safe customization.

    I have read following link is it the correct way?

    https://developer.sugarcrm.com/2015/12/14/leveraging-backbone-events-in-sugar/ 

  • One more Link which i have got.

    http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.5/UI_Model/Views/Examples/Adding_Buttons_to_the_Record_View/

    I have read that all customization should be the part of extension frame work how can i do all this thing in extension framework.

    Please suggest the correct approach.

    Regards,

    Deepak

  • Hi Deepak

    Above mention two articles by you are of two different approach but the result will be same and upgrade safe.

    We can write in both way.

    Regards

    Sam Roy

  • Hi Ramana,

    I am doing following changes please check n let me knoow if i am doing any thing wrong. after these changes i repair and rebuild and not able to open any module it shows loading all the time.

    custom/modules/module_name/clients/base/views/record/record.php

    please see changes in bold.

    <?php
    $module_name = 'PB_PBServiceConfiguration';
    $viewdefs[$module_name] =
    array (
    'base' =>
    array (
    'view' =>
    array (
    'record' =>
    array (
    'buttons' =>
    array (
    0 =>
    array (
    'type' => 'button',
    'name' => 'cancel_button',
    'label' => 'LBL_CANCEL_BUTTON_LABEL',
    'css_class' => 'btn-invisible btn-link',
    'showOn' => 'edit',
    'events' =>
    array (
    'click' => 'button:cancel_button:click',
    ),
    ),
    1 =>
    array (
    'type' => 'rowaction',
    'event' => 'button:save_button:click',
    'name' => 'save_button',
    'label' => 'LBL_SAVE_BUTTON_LABEL',
    'css_class' => 'btn btn-primary',
    'showOn' => 'edit',
    'acl_action' => 'edit',
    ),
    2 =>
    array (
    'type' => 'actiondropdown',
    'name' => 'main_dropdown',
    'primary' => true,
    'showOn' => 'view',
    'buttons' =>
    array (
    0 =>
    array (
    'type' => 'rowaction',
    'event' => 'button:edit_button:click',
    'name' => 'edit_button',
    'label' => 'LBL_EDIT_BUTTON_LABEL',
    'acl_action' => 'edit',
    ),
    1 =>
    array (
    'type' => 'shareaction',
    'name' => 'share',
    'label' => 'LBL_RECORD_SHARE_BUTTON',
    'acl_action' => 'view',
    ),
    2 =>
    array (
    'type' => 'pdfaction',
    'name' => 'download-pdf',
    'label' => 'LBL_PDF_VIEW',
    'action' => 'download',
    'acl_action' => 'view',
    ),
    3 =>
    array (
    'type' => 'pdfaction',
    'name' => 'email-pdf',
    'label' => 'LBL_PDF_EMAIL',
    'action' => 'email',
    'acl_action' => 'view',
    ),
    4 =>
    array (
    'type' => 'divider',
    ),
    5 =>
    array (
    'type' => 'rowaction',
    'event' => 'button:find_duplicates_button:click',
    'name' => 'find_duplicates_button',
    'label' => 'LBL_DUP_MERGE',
    'acl_action' => 'edit',
    ),
    6 =>
    array (
    'type' => 'rowaction',
    'event' => 'button:duplicate_button:click',
    'name' => 'duplicate_button',
    'label' => 'LBL_DUPLICATE_BUTTON_LABEL',
    'acl_module' => 'PB_PBServiceConfiguration',
    'acl_action' => 'create',
    ),
    7 =>
    array (
    'type' => 'rowaction',
    'event' => 'button:audit_button:click',
    'name' => 'audit_button',
    'label' => 'LNK_VIEW_CHANGE_LOG',
    'acl_action' => 'view',
    ),
    8 =>
    array (
    'type' => 'divider',
    ),
    9 =>
    array (
    'type' => 'rowaction',
    'event' => 'button:delete_button:click',
    'name' => 'delete_button',
    'label' => 'LBL_DELETE_BUTTON_LABEL',
    'acl_action' => 'delete',
    ),
    ),
    ),
    3 =>
    array (
    'customCode' => '<input id="TestButton" title="Test Button" class="button" type="button" name="TestButton" value="Test Connection">',
    ),
    4 =>
    array (
    'name' => 'sidebar_toggle',
    'type' => 'sidebartoggle',
    ),
    ),
    'panels' =>
    array (
    0 =>
    array (
    'name' => 'panel_header',
    'label' => 'LBL_RECORD_HEADER',
    'header' => true,
    'fields' =>
    array (
    0 =>
    array (
    'name' => 'picture',
    'type' => 'avatar',
    'width' => 42,
    'height' => 42,
    'dismiss_label' => true,
    'readonly' => true,
    ),
    1 => 'name',
    2 =>
    array (
    'name' => 'favorite',
    'label' => 'LBL_FAVORITE',
    'type' => 'favorite',
    'readonly' => true,
    'dismiss_label' => true,
    ),
    3 =>
    array (
    'name' => 'follow',
    'label' => 'LBL_FOLLOW',
    'type' => 'follow',
    'readonly' => true,
    'dismiss_label' => true,
    ),
    ),
    ),
    1 =>
    array (
    'newTab' => true,
    'panelDefault' => 'expanded',
    'name' => 'LBL_RECORDVIEW_PANEL1',
    'label' => 'LBL_RECORDVIEW_PANEL1',
    'columns' => 2,
    'labelsOnTop' => 1,
    'placeholders' => 1,
    'fields' =>
    array (
    0 =>
    array (
    'name' => 'servername_c',
    'label' => 'LBL_SERVERNAME',
    ),
    1 =>
    array (
    ),
    2 =>
    array (
    'name' => 'port_c',
    'label' => 'LBL_PORT',
    ),
    3 =>
    array (
    ),
    4 =>
    array (
    'name' => 'accountid_c',
    'label' => 'LBL_ACCOUNTID',
    ),
    5 =>
    array (
    ),
    6 =>
    array (
    'name' => 'password_c',
    'label' => 'LBL_PASSWORD',
    ),
    7 =>
    array (
    ),
    ),
    ),
    2 =>
    array (
    'name' => 'panel_body',
    'label' => 'LBL_RECORD_BODY',
    'columns' => 2,
    'labelsOnTop' => true,
    'placeholders' => true,
    'newTab' => true,
    'panelDefault' => 'expanded',
    'fields' =>
    array (
    0 => 'assigned_user_name',
    1 => 'team_name',
    2 =>
    array (
    'name' => 'tag',
    'span' => 12,
    ),
    ),
    ),
    3 =>
    array (
    'name' => 'panel_hidden',
    'label' => 'LBL_SHOW_MORE',
    'hide' => true,
    'columns' => 2,
    'labelsOnTop' => true,
    'placeholders' => true,
    'newTab' => false,
    'panelDefault' => 'expanded',
    'fields' =>
    array (
    0 =>
    array (
    'name' => 'description',
    'span' => 12,
    ),
    1 =>
    array (
    'name' => 'date_modified_by',
    'readonly' => true,
    'inline' => true,
    'type' => 'fieldset',
    'label' => 'LBL_DATE_MODIFIED',
    'fields' =>
    array (
    0 =>
    array (
    'name' => 'date_modified',
    ),
    1 =>
    array (
    'type' => 'label',
    'default_value' => 'LBL_BY',
    ),
    2 =>
    array (
    'name' => 'modified_by_name',
    ),
    ),
    ),
    2 =>
    array (
    'name' => 'date_entered_by',
    'readonly' => true,
    'inline' => true,
    'type' => 'fieldset',
    'label' => 'LBL_DATE_ENTERED',
    'fields' =>
    array (
    0 =>
    array (
    'name' => 'date_entered',
    ),
    1 =>
    array (
    'type' => 'label',
    'default_value' => 'LBL_BY',
    ),
    2 =>
    array (
    'name' => 'created_by_name',
    ),
    ),
    ),
    ),
    ),
    ),
    'templateMeta' =>
    array (
    'useTabs' => true,
    ),
    ),
    ),
    ),
    );

      Changes done in custom/modules/module_name/clients/base/views/record/record.js

    ({

    extendsFrom: 'RecordView',

    initialize: function (options) {
    app.view.invokeParent(this, {type: 'view', name: 'record', method: 'initialize', args:[options]});

    //add listener for custom button
    this.model.on('click #TestButton', this.buttonCalled, this);
    },

    buttonCalled:function(){
    // Write your code here
    alert("Friend am triggered.. ");
    },


    })
          

    After repiare and rebuild i am not able to load any module.

    Regards,

    Deepak