Autopopulating field based on relate

I have a custom module with two relationships setup. These means i have two relate fields in my custom module. I would like to auto populate the second relate field with the same value as the first relate field.

currently i am trying the following code:

<?php
// Do not store anything in this file that is not part of the array or the hook version. This file will
// be automatically rebuilt in the future.
$hook_version = 1;
$hook_array = Array();
// position, file, function
$hook_array['before_save'] = Array(); 
$hook_array['before_save'][] = Array(1, 'Value from one field to another', 'custom/modules/conn_connection/TextCopy.php', 'textcopy','copy');

?>
class textcopy{
    function copy($bean, $event, $arguments)
    {
    $bean->rece_receiver_conn_connection_1_name  = $bean->orga_organisation_conn_connection_1_name;
    }
}  

This returns a webpage which states the following:

class textcopy{ function copy($bean, $event, $arguments) { $bean->rece_receiever_conn_connection_1_name = $bean->orga_organisation_conn_connection_1_name; } }

Despite these issues i am unsure this will be the solution i would like to use. I would like this field to autopopulate as soon as the first relate field has been selected.

Hi,
I assume you created the file in the location: ‘custom/modules/conn_connection/TextCopy.php’
If not, then you must do this and enter in your code there.

Does your TextCopy.php file have the php tags at the start/end of the file? i.e: <?php[/i][/b] and [b][i] ?>

Note sure how i missed that the php tags.

I am still receieving a page that states

rece_receiver = $bean->orga_organisation; } } ?>

I’m unsure if i am using the correct names. Would i get the name of these relate field from the vardef file:

$dictionary["conn_connection"]["fields"]["rece_receiver_conn_connection_1_name"] = array (
  'name' => 'rece_receiver_conn_connection_1_name',
  'type' => 'relate',
  'source' => 'non-db',
  'vname' => 'LBL_RECE_RECEIVER_CONN_CONNECTION_1_FROM_RECE_RECEIVER_TITLE',
  'save' => true,
  'id_name' => 'rece_receiver_conn_connection_1rece_receiver_ida',
  'link' => 'rece_receiver_conn_connection_1',
  'table' => 'rece_receiver',
  'module' => 'rece_receiver',
  'rname' => 'name',
);

Also is there a way to get the field to update as soon as the relate field has been selected?

Could you post a screenshot of your TextCopy.php file? There must be some issue with it that causes some text to show instead of the correct webpage.

Also, In regards to populating fields when a relate field is selected; this guide may help: https://johndopenotes.wordpress.com/2013/03/18/sugarcrm-populating-fields-using-a-relate-field/

Here is a screenshot of my code.

Any Suggestions would be great.

Hi,
Check your PHP/Apache error Logs, are there any errors in there?
If not, turn on error reporting and see if anything shows on the screen that displays “rece_receiver = $bean->orga_organisation; } } ?>”

What are you using to edit the PHP files? From the screenshot I assume it is Notepad++

Hello John,

I eventually solved the issue but thought i would post to help others in future.

This is caused by an encoding error. I was using UCS-2 LB which must have been causing this. After changing the encoding this worked fine.