Calling Javascript function from a relate field in Editview

Hello developers!

I need to call a javascript function when a record is chosen in a relate field (in the editview).

i know how to do that with standard fields like dropdowns and others but the javascript function is not called on relate fields.

1. i added the include in custom/modules/aa_Subscriptions/metadata/editviewdefs.php

'includes' =>
   array (
      0 =>
      array (
         'file' => 'custom/modules/aa_Subscriptions/Subscription.js',
      ),
),

2. added the "onchange" event in the field:

0 => 

array (

   'name' => 'aos_products_aa_subscriptions_2_name',

   'displayParams' => 

      array (

         'field' => 

            array (

               'onChange' => 'doSomthing(this.value);',

            ),

      ),

),

3. added a javascript file  custom/modules/aa_Subscriptions/Subscription.js

function doSomthing(param)
{
   alert('write your code here');

}

any way for that to work?

i really really need to find a solution for this. any help will do...

  • Hi Asaf Army

    Try this and let me know for further help:

    1. Add the javascript in custom/modules/aa_Subscriptions/metadata/editviewdefs.php
    'templateMeta' =>
    // Add this code
    array (
    'javascript' => '
    <script src="./include/javascript/jquery.min.js" type="text/javascript"></script>
    <script src="./custom/modules/aa_Subscriptions/Subscription.js" type="text/javascript"></script>
    ',
    ),
    2. added the "onchange" event:
    0 =>
    array (
    'name' => 'aos_products_aa_subscriptions_2_name',
    'displayParams' =>
    array (
    'javascript' => 'onchange=doSomthing(this.value);',
    ),
    ),

    3. added a javascript file custom/modules/aa_Subscriptions/Subscription.js
    function doSomthing(param)
    {
    alert('write your code here');

    }

    Hope this Helps..!!

    Best Regards

    S Ramana Raju

  • thank you for the quick response.

    I'm sorry but i don't have the file include/javascript/jquery.min.js in my sugarcrm project, where can i get it?

    (i use CE version 6.5.13)

    i tried referencing file /include/javascript/jquery/jquery-min.js but it doesn't work...

  • HI Asaf Army

    Okay..try including jquery.js file and let me know it worked or not

    include/javascript/jquery.js

    Also change this:

    3. added a javascript file  custom/modules/aa_Subscriptions/Subscription.js

     $(document).ready(function(){

    function doSomthing(param)
    {
       alert('write your code here');

    }

    });

    Hope this Helps..!!

    Best Regards

    S Ramana Raju

  • Thank you, but it still doesn't call the javascript function (also made rebuild and cleaned the browser cache):

    <?php
    $module_name = 'aa_Subscriptions';
    $viewdefs [$module_name] =
    array (
    'EditView' =>
    array (
    'templateMeta' =>
    array (
    0 =>
    array (
    'javascript' => '
    <script src="./include/javascript/jquery.js" type="text/javascript"></script>
    <script src="./custom/modules/aa_Subscriptions/Subscriptions.js" type="text/javascript"></script>
    ',
    ),

    in the javascript file:

    $(document).ready(function(){
    function doSomthing(param)
    {
    alert('write your code here');
    }
    });

  • Hi Asaf Army

    Your code syntax is wrong Asaf.

    Correct it as:

    $module_name = 'aa_Subscriptions';
    $viewdefs [$module_name] =
    array (
      'EditView' =>
      array (
        'templateMeta' =>
         array (
                   'javascript' => '
                        <script src="./include/javascript/jquery.min.js" type="text/javascript"></script>
                        <script src="./custom/modules/aa_Subscriptions/Subscription.js" type="text/javascript"></script>
                   ',
         ),
         'panels' =>
        array (
          'lbl_account_information' =>
          array (
                   0 =>
                   array (
                     0 =>
                     array (
                        'name' => 'aos_products_aa_subscriptions_2_name',
                        'displayParams' =>
                        array (
                          'javascript' => 'onchange=doSomthing(this.value);',
                        ),
                     ),
                   ),
              ),
         ),
         ),
         );

    Save & Do R&R.

    Let me know for further help.

    Best Regards

    S Ramana Raju

  • I'm sorry, i tried it again and again.

    as i said in the previous post. i use 6.5.13 CE version so i don't have the file /include/javascript/jquery.min.js

    i do have the file /include/javascript/jquery/jquery-min.js but it doesn't work with that file too.

    can you please post the file as ZIP file maybe....

  • Hi Asaf Army

    I tried in my custom module for field with dropdown as data type and below code is working for me.
    Can you try in this way and let me know for further help.
    In custom/modules/aa_Subscriptions/metadata/editviewdefs.php added following code:

    'templateMeta' => 
        array (
          'form' =>
          array (
            'buttons' =>
            array (
              0 => 'SAVE',
              1 => 'CANCEL',         
            ),
          ),
          'includes' =>
          array (
            0 =>
            array (
              'file' => 'custom/modules/aa_Subscriptions/Subscription.js',
            ),
          ),
        ),
    'panels' =>
        array (
          'lbl_account_information' =>
          array (
                   0 =>
                   array (
                     0 =>
                     array (
                        'name' => 'aos_products_aa_subscriptions_2_name',
                        'displayParams' =>
                        array (
                          'javascript' => 'onchange=doSomthing(this.value);',
                        ),
                     ),
                   ),
              ),
         ),

    Here is Subscription.js code (custom/modules/aa_Subscriptions/Subscription.js):

    function doSomthing(cvalue)
    {    
         alert(cvalue," Hi Friends am triggered..");                                  
    }

    Its working for me.
    Let me know for further help.
    Hope this Helps..!!

    Best Regards
    S Ramana Raju

  • No. Still doesn't show the javascript alert when i choose a product in aos_products_aa_subscriptions_2_name field.

    Did all that you wrote.

    I really appreciate your effort. thank you very much!

  • Hi Asaf

    For me both code which i suggested is working.

    Can you share your code and what data type is your field ( i.e aos_products_aa_subscriptions_2_name ).

    If it is dropdown then when you change option of your dropdown do we getting an error or what.

    Can you check and let me know. So that i can help you further.

    Best Regards

    S Ramana Raju

  • Thank you Ramana,

    just read my post , the first one:

    Hello developers!

     

    I need to call a javascript function when a record is chosen in a relate field (in the editview).

    i know how to do that with standard fields like dropdowns and others but the javascript function is not called on relate fields.

    .....

    ....

    ...

    This is code:

    <?php
    $module_name = 'aa_Subscriptions';
    $viewdefs [$module_name] =
    array (
    'EditView' =>
    array (
    'templateMeta' =>
    array (
    0 =>
    array (
    'javascript' => '
    <script src="./include/javascript/jquery.js" type="text/javascript"></script>
    <script src="./custom/modules/aa_Subscriptions/Subscriptions.js" type="text/javascript"></script>
    ',
    ),
    'maxColumns' => '2',
    'widths' =>
    array (
    0 =>
    array (
    'label' => '10',
    'field' => '30',
    ),
    1 =>
    array (
    'label' => '10',
    'field' => '30',
    ),
    ),
    'includes' =>
    array (
    0 =>
    array (
    'file' => 'custom/modules/aa_Subscriptions/Subscriptions.js',
    ),
    ),
    'useTabs' => false,
    'tabDefs' =>
    array (
    'DEFAULT' =>
    array (
    'newTab' => false,
    'panelDefault' => 'expanded',
    ),
    'LBL_EDITVIEW_PANEL1' =>
    array (
    'newTab' => false,
    'panelDefault' => 'expanded',
    ),
    'LBL_EDITVIEW_PANEL2' =>
    array (
    'newTab' => false,
    'panelDefault' => 'expanded',
    ),
    'LBL_EDITVIEW_PANEL3' =>
    array (
    'newTab' => false,
    'panelDefault' => 'expanded',
    ),
    'LBL_EDITVIEW_PANEL4' =>
    array (
    'newTab' => false,
    'panelDefault' => 'expanded',
    ),
    ),
    'syncDetailEditViews' => false,
    ),
    'panels' =>
    array (
    'default' =>
    array (
    0 =>
    array (
    0 => 'name',
    1 =>
    array (
    'name' => 'contacts_aa_subscriptions_1_name',
    ),
    ),
    1 =>
    array (
    0 =>
    array (
    'name' => 'register_status_c',
    'studio' => 'visible',
    'label' => 'LBL_REGISTER_STATUS',
    ),
    1 =>
    array (
    'name' => 'accounts_aa_subscriptions_1_name',
    ),
    ),
    2 =>
    array (
    0 =>
    array (
    'name' => 'register_date_c',
    'label' => 'LBL_REGISTER_DATE',
    ),
    1 =>
    array (
    'name' => 'leads_aa_subscriptions_1_name',
    ),
    ),
    3 =>
    array (
    0 =>
    array (
    'name' => 'details_confirmed_c',
    'studio' => 'visible',
    'label' => 'LBL_DETAILS_CONFIRMED',
    ),
    1 =>
    array (
    'name' => 'aa_classes_aa_subscriptions_1_name',
    ),
    ),
    4 =>
    array (
    0 =>
    array (
    'name' => 'trace_18_c',
    'studio' => 'visible',
    'label' => 'LBL_TRACE_18',
    ),
    1 => '',
    ),
    5 =>
    array (
    0 =>
    array (
    'name' => 'commnets_c',
    'studio' => 'visible',
    'label' => 'LBL_COMMNETS',
    ),
    1 => 'description',
    ),
    ),
    'lbl_editview_panel1' =>
    array (
    0 =>
    array (
    0 =>
    array (
    'name' => 'aos_products_aa_subscriptions_1_name',
    'displayParams' =>
    array (
    'field_to_name_array' =>
    array (
    'id' => 'aos_products_aa_subscriptions_1aos_products_ida',
    'name' => 'aos_products_aa_subscriptions_1_name',
    'product_price_c' => 'class_curse_price_c',
    ),
    'additionalFields' =>
    array (
    'product_price_c' => 'class_curse_price_c',
    ),
    ),
    ),
    1 =>
    array (
    'name' => 'class_curse_price_c',
    'label' => 'LBL_CLASS_CURSE_PRICE',
    ),
    ),
    1 =>
    array (
    0 =>
    array (
    'name' => 'subs_cost_add_c',
    'studio' => 'visible',
    'label' => 'LBL_SUBS_COST_ADD',
    ),
    1 =>
    array (
    'name' => 'subs_add_cost_c',
    'label' => 'LBL_SUBS_ADD_COST',
    ),
    ),
    2 =>
    array (
    0 =>
    array (
    'name' => 'aos_products_aa_subscriptions_2_name',

    ),
    1 =>
    array (
    'name' => 'discount_sum_c',
    'label' => 'LBL_DISCOUNT_SUM',
    ),
    ),
    3 =>
    array (
    0 =>
    array (
    'name' => 'sale_code_c',
    'label' => 'LBL_SALE_CODE',
    ),
    1 =>
    array (
    'name' => 'sale_code_sum_c',
    'label' => 'LBL_SALE_CODE_SUM',
    ),
    ),
    4 =>
    array (
    0 => '',
    1 =>
    array (
    'name' => 'manager_discount_c',
    'label' => 'LBL_MANAGER_DISCOUNT',
    ),
    ),
    5 =>
    array (
    0 => '',
    1 =>
    array (
    'name' => 'total_before_interest_c',
    'label' => 'LBL_TOTAL_BEFORE_INTEREST',
    ),
    ),
    6 =>
    array (
    0 => '',
    1 =>
    array (
    'name' => 'payment_interest_sum_c',
    'label' => 'LBL_PAYMENT_INTEREST_SUM',
    ),
    ),
    7 =>
    array (
    0 => '',
    1 =>
    array (
    'name' => 'credit_sum_c',
    'label' => 'LBL_CREDIT_SUM',
    ),
    ),
    8 =>
    array (
    0 => '',
    1 =>
    array (
    'name' => 'final_sum_c',
    'label' => 'LBL_FINAL_SUM',
    ),
    ),
    9 =>
    array (
    0 => '',
    1 =>
    array (
    'name' => 'paid_sum_c',
    'label' => 'LBL_PAID_SUM',
    ),
    ),
    10 =>
    array (
    0 => '',
    1 =>
    array (
    'name' => 'not_paid_c',
    'label' => 'LBL_NOT_PAID',
    ),
    ),
    ),
    'lbl_editview_panel2' =>
    array (
    0 =>
    array (
    0 =>
    array (
    'name' => 'name_mafne_c',
    'label' => 'LBL_NAME_MAFNE',
    ),
    1 =>
    array (
    'name' => 'cigarette_type_c',
    'studio' => 'visible',
    'label' => 'LBL_CIGARETTE_TYPE',
    ),
    ),
    1 =>
    array (
    0 =>
    array (
    'name' => 'num_mafne_c',
    'label' => 'LBL_NUM_MAFNE',
    ),
    1 =>
    array (
    'name' => 'credit_pass_to_c',
    'studio' => 'visible',
    'label' => 'LBL_CREDIT_PASS_TO',
    ),
    ),
    ),
    'lbl_editview_panel3' =>
    array (
    0 =>
    array (
    0 =>
    array (
    'name' => 'cred_request_date_c',
    'label' => 'LBL_CRED_REQUEST_DATE',
    ),
    1 =>
    array (
    'name' => 'cred_isok_c',
    'label' => 'LBL_CRED_ISOK',
    ),
    ),
    1 =>
    array (
    0 =>
    array (
    'name' => 'users_aa_subscriptions_1_name',
    'label' => 'LBL_USERS_AA_SUBSCRIPTIONS_1_FROM_USERS_TITLE',
    ),
    1 =>
    array (
    'name' => 'users_aa_subscriptions_2_name',
    'label' => 'LBL_USERS_AA_SUBSCRIPTIONS_2_FROM_USERS_TITLE',
    ),
    ),
    2 =>
    array (
    0 =>
    array (
    'name' => 'cred_start_handle_date_c',
    'label' => 'LBL_CRED_START_HANDLE_DATE',
    ),
    1 =>
    array (
    'name' => 'credit_return_c',
    'studio' => 'visible',
    'label' => 'LBL_CREDIT_RETURN',
    ),
    ),
    3 =>
    array (
    0 =>
    array (
    'name' => 'cred_result_c',
    'studio' => 'visible',
    'label' => 'LBL_CRED_RESULT',
    ),
    1 =>
    array (
    'name' => 'cred_receive_form_date_c',
    'label' => 'LBL_CRED_RECEIVE_FORM_DATE',
    ),
    ),
    4 =>
    array (
    0 =>
    array (
    'name' => 'manager_confirm_c',
    'studio' => 'visible',
    'label' => 'LBL_MANAGER_CONFIRM',
    ),
    1 =>
    array (
    'name' => 'cred_receive_credit_date_c',
    'label' => 'LBL_CRED_RECEIVE_CREDIT_DATE',
    ),
    ),
    5 =>
    array (
    0 =>
    array (
    'name' => 'demand_manager_approve_c',
    'label' => 'LBL_DEMAND_MANAGER_APPROVE',
    ),
    1 =>
    array (
    'name' => 'approved_by_manager_c',
    'label' => 'LBL_APPROVED_BY_MANAGER',
    ),
    ),
    6 =>
    array (
    0 =>
    array (
    'name' => 'cred_invoice_date_c',
    'label' => 'LBL_CRED_INVOICE_DATE',
    ),
    1 =>
    array (
    'name' => 'cred_cancel_reason_c',
    'studio' => 'visible',
    'label' => 'LBL_CRED_CANCEL_REASON',
    ),
    ),
    7 =>
    array (
    0 =>
    array (
    'name' => 'cred_damage_date_c',
    'label' => 'LBL_CRED_DAMAGE_DATE',
    ),
    1 =>
    array (
    'name' => 'general_payment_status_c',
    'studio' => 'visible',
    'label' => 'LBL_GENERAL_PAYMENT_STATUS',
    ),
    ),
    ),
    'lbl_editview_panel4' =>
    array (
    0 =>
    array (
    0 => 'assigned_user_name',
    ),
    ),
    ),
    ),
    );
    ?>