Create Dynamic Field

I have tried this and the same results. again i have run a rebuild and repair then tested this when it did not work I rebooted the server and again this did not change anything.

Be careful because I have edited my post. Now my code is slightly different.

These are the two edits I did soon after posting the code I adapted to your install:

  1. I changed:
      if(xx_extra_field.value == 0) {

to:

      if(xx_extra_field.value == 'No') {
  1. I changed:
      if(xx_extra_field.value == 1) {

to:

      if(xx_extra_field.value == 'Yes') {

You don’t need to run Quick Repair and Rebuild. It should work immediately. Maybe you should just clear the browser cache (Ctrl+F5).

If it doesn’t work then:

  1. Check the browser console when loading the page and when changing the dropdown value.

  2. Check php logs

Report any anomalies.

Last thing:
I need to see your code because there must be something that you are not doing right in some way. In my test system it works perfectly.
Please make sure that the file is in the correct position and that it has correct permissions.
As an extra test you can remove all the php code and just place the javascript like this (without even php open/close tags):

<script type="text/javascript">
  window.onload=function(){
    function toggle_extra_field() {
      if(xx_extra_field.value == 'No') {
        document.querySelector('div[data-label="LBL_DEFECT_NUMBER"]').style.display='none';
        document.querySelector('div[field="defect_number_c"]').style.display='none';
      }
      if(xx_extra_field.value == 'Yes') {
        document.querySelector('div[data-label="LBL_DEFECT_NUMBER"]').style.display='';
        document.querySelector('div[field="defect_number_c"]').style.display='';
        var xx_extra_field_sub = document.getElementById('defect_number_c');
        xx_extra_field_sub.value = '';
      }
    }
    var xx_extra_field = document.getElementById('defect_number_required_c');
    toggle_extra_field();
    xx_extra_field.addEventListener('change', toggle_extra_field, false);
  }
</script>

So it turns out there was a view.edit.php file in the file location you specified I have tried to modify the file by putting the below code below the function display(){ line however when i run a rebuild and repair and I try to create a new invoice I am unable to access the create invoices area of my crm. Please see below for my php code.

<?php require_once('include/MVC/View/views/view.edit.php'); class AOS_InvoicesViewEdit extends ViewEdit{ function AOS_InvoicesViewEdit(){ parent::ViewEdit(); } function display(){ public function display(){ parent::display(); $javascript = <<<'EOT' EOT; echo $javascript; exit; } echo ''; parent::display(); } } ?>

Your code has some php bugs that should crash the execution.

Try this instead:

<?php
require_once('include/MVC/View/views/view.edit.php');

class AOS_InvoicesViewEdit extends ViewEdit{
  function AOS_InvoicesViewEdit(){
    parent::ViewEdit();
  }

	public function display(){
    parent::display();
    $javascript = <<<'EOT'
<script type="text/javascript">
  window.onload=function(){
    function toggle_extra_field() {
      if(xx_extra_field.value == No) {
        document.querySelector('div[data-label="LBL_DEFECT_NUMBER"]').style.display='none';
        document.querySelector('div[field="defect_number_c"]').style.display='none';
      }
      if(xx_extra_field.value == Yes) {
        document.querySelector('div[data-label="LBL_DEFECT_NUMBER"]').style.display='';
        document.querySelector('div[field="defect_number_c"]').style.display='';
        var xx_extra_field_sub = document.getElementById('defect_number_c');
        xx_extra_field_sub.value = '';
      }
    }
    var xx_extra_field = document.getElementById('defect_number_required_c');
    toggle_extra_field();
    xx_extra_field.addEventListener('change', toggle_extra_field, false);
  }
</script>
EOT;
    echo $javascript;
    echo '<script type="text/javascript" src="custom/include/javascript/showhide.js"></script>';
  }
}
?>

If this doesn’t work it may be for the reason that @pgr explained (the class extension has already been defined.
In such case try this:

<?php
require_once('include/MVC/View/views/view.edit.php');

class My_Custom_AOS_InvoicesViewEdit extends AOS_InvoicesViewEdit {
  function AOS_InvoicesViewEdit(){
    parent::ViewEdit();
  }

	public function display(){
    parent::display();
    $javascript = <<<'EOT'
<script type="text/javascript">
  window.onload=function(){
    function toggle_extra_field() {
      if(xx_extra_field.value == No) {
        document.querySelector('div[data-label="LBL_DEFECT_NUMBER"]').style.display='none';
        document.querySelector('div[field="defect_number_c"]').style.display='none';
      }
      if(xx_extra_field.value == Yes) {
        document.querySelector('div[data-label="LBL_DEFECT_NUMBER"]').style.display='';
        document.querySelector('div[field="defect_number_c"]').style.display='';
        var xx_extra_field_sub = document.getElementById('defect_number_c');
        xx_extra_field_sub.value = '';
      }
    }
    var xx_extra_field = document.getElementById('defect_number_required_c');
    toggle_extra_field();
    xx_extra_field.addEventListener('change', toggle_extra_field, false);
  }
</script>
EOT;
    echo $javascript;
    echo '<script type="text/javascript" src="custom/include/javascript/showhide.js"></script>';
  }
}
?>

I tried the first bit of code provided and this allows me to create a invoice after the code has been amended however the dynamic field is not working I then tried the seconds bit of code and I am unable to create a new invoice using this 2nd option.
At present I am using the first option of code you have provided however I have noticed an error in the php logs
“[04-Dec-2018 12:36:37 UTC] PHP Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; AOS_InvoicesViewEdit has a deprecated constructor in C:\inetpub\wwwroot\CRM\custom\modules\AOS_Invoices\views\view.edit.php on line 4”

try this:
(however this new modification is correcting some of the code you found. not mine)

<?php
require_once('include/MVC/View/views/view.edit.php');

class AOS_InvoicesViewEdit extends ViewEdit{
  function __construct(){
    parent::ViewEdit();
  }

	public function display(){
    parent::display();
    $javascript = <<<'EOT'
<script type="text/javascript">
  window.onload=function(){
    function toggle_extra_field() {
      if(xx_extra_field.value == No) {
        document.querySelector('div[data-label="LBL_DEFECT_NUMBER"]').style.display='none';
        document.querySelector('div[field="defect_number_c"]').style.display='none';
      }
      if(xx_extra_field.value == Yes) {
        document.querySelector('div[data-label="LBL_DEFECT_NUMBER"]').style.display='';
        document.querySelector('div[field="defect_number_c"]').style.display='';
        var xx_extra_field_sub = document.getElementById('defect_number_c');
        xx_extra_field_sub.value = '';
      }
    }
    var xx_extra_field = document.getElementById('defect_number_required_c');
    toggle_extra_field();
    xx_extra_field.addEventListener('change', toggle_extra_field, false);
  }
</script>
EOT;
    echo $javascript;
    echo '<script type="text/javascript" src="custom/include/javascript/showhide.js"></script>';
  }
}
?>

The error has now gone from the logs however the dynamic field is still not working.

To attach a screen shot you have to click “around” (4/5 mm above the top right corner of the button, slightly to the right: a tooltip should appear, then click) the submit button so that a pop up appears to let you select a file, It is a little tricky!

Evidently there is something that you are not doing right and I am missing it! So I need to see some screenshots.

Hi

See attached the PHP code although you have already seen it I thought it would be beneficial to attach it.Along with the dropdown field and text field details. If you require anything else let me know. Again thank you for all your help on this.

Change the folloing two lines:

      if(xx_extra_field.value == No) {

to:

      if(xx_extra_field.value == 'No') {

(put the single quotes around No)

and

      if(xx_extra_field.value == Yes) {

to:

      if(xx_extra_field.value == 'Yes') {

(put the single quotes around Yes)

PS: Something strange must have happened because in my working example the single quotes are there!
This typo should have been reproted in the browser Console though.

1 Like

This is now working! Thank you very much for all your help.

:wink:

I have a follow up question. Would it be possible for the defect number text field if a certain customer is selected instead of a yes/no dropdown? If so how would I go about this.

Thanks.

Everything is possible!

But I need more precise information!

Additionnally I need to explain something on the code I sent you:
Currently when “No” is selected in the dropdown the defect:number is hidden and its value is deleted.

If you don’t want to delete the value, you have to delete the following two lines:

        var xx_extra_field_sub = document.getElementById('defect_number_c');
        xx_extra_field_sub.value = '';

Hi

Thank you for letting me know about the code however if I can get the defect number text field to appear for specific customers I will probably go down that road.

I have created 2 test customers “Test Customer 1” & “Test Customer 2” I would like the defect number text field to be always hidden until either one of the test customers are selected under the Customers field. If possible I would also like the defect number text field to be mandatory when it pops up.

The field name for the customers field I have is “aos_invoices_accounts_1_name” & the field name for the defect number text field is “defect_number_c”

Is there anything else that I have missed that you require?

Thanks.

Where are you storing the list of customers that will trigger the field?

The code itself is simple.

Solution 1:
You may want to create a target list where you select the customers that you want to trigger the event and base the code on this selection.

Solution 2:
Alternatively you could create a custom module where you select the customers and then base the field on customers that have been selected. The code shouldn’t be so complicated.

Solution 3:
A third and simpler way could be that you manually enter the name of the customers in config_override.php.

Among the potential issues that I see: what if someone edits the name of the customer but the list has not been updated?

I am now working on the last (easiest) solution to show it working (without the compulsory part at the moment. We’ll see that later).

Hi

I have stored the customers under what I believe used to be called accounts. I will wait for the 4th solution and probably go for the 4th and easiest solution to get this up and running.

Here is a working solution:

  1. Edit config_override.php
    Add the following line at the end of the file (before the php closing tag (’?>’) if present:
$sugar_config['dflh_triggers'] = array();
$sugar_config['dflh_triggers'][] = 'Test Customer 1';
$sugar_config['dflh_triggers'][] = 'Test Customer 2';

Please note that, for each account that should trigger the field to be shown, there should be a line:

$sugar_config['dflh_triggers'][] = 'name of account exactly as saved in CRM';
  1. Change the previous working code of view.edit.php to:
<?php
class AOS_InvoicesViewEdit extends ViewEdit
{
  function __construct(){
    parent::ViewEdit();
  }
     public function display(){
        parent::display();
?>

<script type="text/javascript">
  var xx_triggers = <?php echo json_encode($GLOBALS['sugar_config']['dflh_triggers']); ?>;
</script>

<?php
          $javascript = <<<'EOT'
<script type="text/javascript">
  window.onload=function(){
function dflh_isInArray(value, array) {
  return array.indexOf(value) > -1;
}
    function toggle_extra_field(passedValue) {
      alert(' value is: ' + passedValue);
      if(dflh_isInArray(passedValue, xx_triggers) == true) {
        document.querySelector('div[data-label="LBL_DEFECT_NUMBER"]').style.display='';
        document.querySelector('div[field="defect_number_c"]').style.display='';
        document.getElementById('dflh_extra_field_c').value = passedValue;
      }
      if(dflh_isInArray(passedValue, xx_triggers) == false) {
        document.querySelector('div[data-label="LBL_DEFECT_NUMBER"]').style.display='none';
        document.querySelector('div[field="defect_number_c"]').style.display='none';
      }
    }
var xx_extra_field = document.getElementById('billing_account');
var lastxx_extra_fieldValue = xx_extra_field.value;
setInterval(function() {
    var newValue = xx_extra_field.value;
    if (lastxx_extra_fieldValue != newValue) {
        lastxx_extra_fieldValue = newValue;
        toggle_extra_field(newValue);
    }
}, 50); // 20 times/second
    toggle_extra_field(lastxx_extra_fieldValue);
  }
</script>
EOT;
      echo $javascript;
      exit;
     }
}
?>

Please test and let me know if it works. (I have edited it directly in the forum form so I may have introduced a typo).

Caveats:
With this solution you have to update manually the list of accounts that trigger the appearance of the field.

In order to have this updated dynamically we need to implement one of the other solutions (just some extra php code)

1 Like

Thank you for this. I have tested this however it did not work when I tried to create a new invoice I got the error below. Once I clicked OK i was able to continue to create a new invoice however the defect text field did not appear when either test customer was selected.