The SugarCRM Relate Field can be a little tricky if you don't understand everything that Sugar is doing under the hood to pull this off. When you create a Relate Field, it actually adds a second field that is hidden (you never get to see it on any of the Sugar Views) that holds the ID of the related field.  

So, to change the value of the related field programmatically, you need to address that 'hidden field' not the Relate Field that you created.  For example, if I am building two Relate Fields in a custom module (named 'exampleMod') -- the first will relate to Meetings and the second will use Meetings to relate to Accounts. This, of course, assumes that we already have a relationship between exampleMod and Meetings. In my logic hook [/custom/modules/exampleMod/logichooks.php] I would add the following code:

//As always ensure that the file can only be accessed through SugarCRM
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
$hook_version = 1;
$hook_array = Array(); #Create an array
/*
* This will trigger our process before we display the object.
*/
$hook_array['after_retrieve'] = Array(); 
$hook_array['after_retrieve'] = array( 
Array(1                                    // Hook order 
, 'updateStore'                        // Hoook name 
, 'custom/include/updateStoreForm.php' // File name 
, 'updateStore'                        // Class name 
, 'updateStore'                        // Function name 
),
);
/*
* This will trigger our process before we save the object.
*/
$hook_array['before_save'] = Array();
$hook_array['before_save'] = array(
Array(1                                      // Hook order 
, 'updateStore'                        // Hoook name
, 'custom/include/updateStoreForm.php' // File name
, 'updateStore'                        // Class name
, 'updateStore'                        // Function name
),
); 

Note: we trigger the same function twice -- once before we display it in a form and again when we save the object. This will ensure that we both display and save the object properly.

Then my include file [/custom/include/UpdateMeetingStore.php] I would contain the following code.

// Define the entry point
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
// Load any required files
require_once( 'data/SugarBean.php' );
require_once( 'modules/Meetings/Meeting.php' );
// Define the class
class updateStore {
function updateStore( &$bean, $event, $arguments) {
// First, check to see if there is already a value for this item           
$bean->schedule_c = $bean->meetings_b2ec6eetings_ida;
if( $bean->meeting_id_c == '' )  {
// If not, we will create a new Meeting object to get the Account ID
$meeting_id =$bean->meetings_b2ec6eetings_ida;
//This last value is the name of the field for the join
$meeting = new Meeting(  );
$meeting->retrieve($meeting_id);
// Fill the new Meeting Object with data from the DB based on ID
$bean->meeting_id_c = $meeting_id;
$bean->account_id_c = $meeting->parent_id;
// This uses the Meeting Object to get the ID of the Account
}
}
}
Tags: SugarCRM