0

I'm trying to detect when the date has changed inside a regular "date" field

Have seen a few forum posts about how this can be done with text fields and dropdown, but it doesn't work for date fields.

Also tried:

'displayParams' => array ( 'javascript' => 'onchange="checkStatusOption(this)"', ),

In the editviewdefs.php, but it didn't work (works for text fields though)

The closest I've got it actually just tracking every click on on the screen and then checking the before and after state of the date field, but it's obviously not a very elegant solution

Here's the code from the extended editview

    function display() {

        parent::display();

        $js = <<<EOT
<script type="text/javascript" language="JavaScript">

        // Capture initial state
        calendar_before = document.getElementById("contract_date_c").value;

       // Wait for any click to take place anywhere on the screen
        $(document).click(function() {

            // Capture state after we clicked somewhere
            calendar_after = document.getElementById("contract_date_c").value;

            // Compare the before and after
            if(calendar_before != calendar_after) {

                // Change detected
                alert("Something's changed eh?" + calendar_before +" "+ calendar_after);
            }

            // Set the new state of the before_calendar
            calendar_before = document.getElementById("contract_date_c").value;


        });

    }

</script>
EOT;

// now I output the javascript
        echo $js;

    }

UPDATE:

I also tried the suggested solution

1) Created a file custom/modules/un_inventory/contract_date_c_change.js and put the following inside:

function yourCustomFunction(formElement){
    console.log(formElement);
}

2) Included a reference to that file in the metadata file (made sure it loads it):

array (
    'file' => 'custom/modules/un_inventory/contract_date_c_change.js',
),

3) Attached the updateCallback to the field:

    array (
        'name' => 'contract_date_c',
        'label' => 'LBL_CONTRACT_DATE',
        // Checks if this field got filled up and shows hidden form field
        'displayParams' =>
            array (
                'updateCallback' => 'yourCustomFunction();',
            ),
    ),

But nothing happens when I change that date field

Robert Sinclair
  • 4,550
  • 2
  • 44
  • 46

2 Answers2

1

Check this out (just tested in SuiteCRM 7.11) for a datetime field, for other fields take a look at this answer to another SO question

First, include your custom JS in the editviewdefs.php (example for Accounts module)

'includes' => 
      array (
        0 => 
        array (
          'file' => 'modules/Accounts/Account.js',
          'file' => 'custom/modules/Accounts/myCustomFile.js',
        ),
      ),

Create the custom JS file custom/modules/Accounts/myCustomFile.js .

function yourCustomFunction(formElement){
  console.log(formElement); 
}

Then update the field you want to monitor for changes (contractsigned_c in the example) using the following code in the editviewdefs.php:

array (
            'name' => 'contractsigned_c',
            'label' => 'LBL_CONTRACTSIGNED',
            'displayParams' =>
             array (
              'updateCallback' => 'yourCustomFunction(this)',
          ),
          ),

Now do a Repair and Rebuild inside the Admin/Repair section and voilà it should work :)

You can add the JS function on the function display() if you want, its the samething, the function will be called right after native combo update. It will look like this combo_contractsigned_c.update(); yourCustomFunction(this)

mrbarletta
  • 902
  • 11
  • 17
  • Thank you for your time and effort! I'm testing this now. Will keep you posted – Robert Sinclair Apr 01 '19 at 15:26
  • Hello again, just updated the answer with comment and recorded a quick video with a demo for you https://youtu.be/1rQ5je19gjo , for some reason it still doesn't work. So strange – Robert Sinclair Apr 01 '19 at 21:28
  • Have you done a repair and rebuild after the change? looks like the updatecallback parameter is not being included in the field as per your video – mrbarletta Apr 02 '19 at 21:35
  • Hi, yep, did it a few times and tested from incognito mode just in case to avoid caching problems. Just to confirm, the code you sent is exactly as it is in your files? I also tried attaching the updateCallback on a regular field and it also didn't react to the change for some reason. – Robert Sinclair Apr 03 '19 at 04:32
  • would you please try in a date_time field ? just to make sure its not an issue with the date type. – mrbarletta Apr 03 '19 at 05:22
  • yep you're right, it works for datetime field, but if the field is just "date" it will not work – Robert Sinclair Apr 04 '19 at 03:31
  • actually ran a few more tests and looks like updateCallback literally works only on the datetime field :) – Robert Sinclair Apr 04 '19 at 04:09
  • sorry about that, just added a link to another answer I wrote regarding validation using callback, check it out to see if it works – mrbarletta Apr 04 '19 at 05:36
  • hey, no worries, but I kinda gave up on it for now, but will try to test in the future. it looks interesting – Robert Sinclair Apr 04 '19 at 23:29
  • I 've been doing suitecrm for large implementations for a lonnnng time, we settled by using style.js and adding our own observers to monitor for ajax page, validations, everything. – mrbarletta Apr 05 '19 at 02:41
  • thank you for the tip, i'm actually also leaning more towards custom JS, something about these built-in listeners and validators by sugar is often pretty flaky – Robert Sinclair Apr 05 '19 at 03:13
1

use this . it is working

YAHOO.util.Event.addListener('your_field_name', 'change', function(){
// your code here
});
Navinkumar
  • 11
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 07 '22 at 16:55