Custom Javascript Function on DateTime Field

Hello Folks,

Its nice to be here. Thanks to Sugar for being so closed to their Community.

I am stuck in middle of one functionality requiring javascript onchange function on datetime field.

It involves some calculation and autofilling other fields based on this. I have found some old threads 

regarding this challenge but could not get much out of it.

Hey Sugar Heroes, please help me out on this. Any help would be appreciated.

Many Thanks.

Hats.
  • You can do this by editing custom/modules/<YOURMODULE>/metadata/editviewdefs.php

    I would create a custom javascript file and include it

    'useTabs' => false,
                            'includes' =>
                            array (
                                    0 =>
                                    array (
                                            'file' => 'custom/include/universal.js',
                                    ),
                                 ),

    Then you can add the onchange to your field

    array (
                                                    'name' => 'contract_start',
                                                    'label' => 'LBL_CONTRACT_START',
                                                    'tabindex' => '105',
                                                    'displayParams' =>
                                                    array (
                                                            'field' =>
                                                            array (
                                                                    'onChange' => 'GetTerm()',
                                                            ),
                                                    ),
                                            ),



    I hope that helps
  • Hi Mike,

    Thanks for your reply.

    I tried your solution but it doesn't work out.

    Do we need to change or modify datetime field file under /include/SugarFields ??

    Awaiting your helpful suggestion.

    Regards.

    Hats
  • Sorry I should have said you need to do a repair after editing the file

    Don't  edit  /include/SugarFields if you need to change thes files you can create a new one in custom/include/Sugarfields but it shouldn't be necessary

    If after doing a repair and it still doesn't workt hen test it by putting something like alert(0) in your onchange to see if is being called

    What version of Sugar are you using this technique certainly works in 6.5

    Can you post the code from your editviewdefs file so I can have a look at whats wrong
  • Hi Mike,

    Thanks for this.

    I am using SugarPro 6.5.14.

    Here is how my code looks like -> 

    name' => 'vacation_start',
    'label' => 'LBL_VACATION_START',
    'displayParams' =>
    array (
    'field' =>
    array (
     'onChange' => 'calculate();',
     ),
    ),

    I found it calls onchange js function which overrides the user defined onchange function.

    Regards.

    Hats
  • Did you try changing calculate() to alert(0) just to check if it is being called?
  • Hi Mike,

    I went through your solution and it worked perfectly for Date field.

    Thanks for that.

    But I need a js function to be called on date time field and I am not able to find a way

    out.

    Can you please help me out on this ??


    Regards.

    Hats
  • Datetime is a bit trickier
    Your best bet would be to copy include/SugarFields/Datetimecombo/EditView.tpl & Datetimecombo.js to custom/include/SugarFields/Datetimecombo

    Then you can amend the onchange to call your function

  • Hi to all .. 
    I have don this part .. I have made the changes  and now I have onchange Datepicer  working..

    I have a problem to call php function from the javascript file....  
    I need to do , when I change the date , it need to make update in currency table..:)
    Pls. help some one..
    ---------------------------------------------------------------------------------------------------
    getCurrency.js  :

    function getDateCurrency(){
      
       var the_string =document.getElementById('invoice_date').value;

        jQuery.ajax({
            type: "POST",        
            url: 'http://localhost/testDima/modules/AOS_Invoices/Dima1.php',
            //dataType: 'json',
            //data: {functionname: 'getCurr', arguments: [the_string]}, 
            data: { arguments: the_string},
             success:function(data) {
             alert(data); 
             }
        });
    }

    ---------------------------------------------------------------------------------------------------

    Dima1.php

    <?php

    if (isset($_POST)) { 

        $aResult = array();
        
        if( !isset($_POST['functionname']) ) { $aResult['error'] = 'No function name!'; } 

        if( !isset($_POST['arguments']) ) { $aResult['error'] = 'No function arguments!'; }

        if( !isset($aResult['error']) ) {

            switch($_POST['functionname']) {
          
                case 'getCurr': 

                $aResult['result'] = GetCurrencyRate($_POST['arguments']);
               // $aResult['result'] = GetCurrencyRate();

                echo ($aResult);
                   break;

                default:
                   $aResult['error'] = 'Not found function '.$_POST['functionname'].'!';
                   break;
            }

        }

       echo json_encode($aResult);

    } else {

        echo ("Nothing send");
    }

       
    function GetCurrencyRate($datum){
          
         $db = DBManagerFactory::getInstance();
         $query = "SELECT iso4217,re_rate FROM currency_rates WHERE date_entered= CURDATE()";
            $result = $db->query($query); 
        while ($row = $db->fetchByAssoc($result))
                {
                  $db = DBManagerFactory::getInstance();             
                  $query = "Update currencies set conversion_rate='".$row['re_rate']."' where iso4217= '" . $row['iso4217'] . "'";
                  $db->query($query);
                }
    }

    ?>
    --------------------------------------------------------------------------------------------------------------------
    Pls. help me whit this one..  
    Thank you  all..





  • same problem for me
    when change the date through date picker onChange not works 
    as my editviewdefs code is 
    array ( 'field' =>
    array (
    'onChange' => 'changedate();',

    ),
    ),
  • Is this still working in sugar 7.6? Can't even get a simple basic alert to go off on onchange,onblur etc. on any fields.