SugarCRM: Set relationship dynamically

$ModuleObj    	= new {MODULE_NAME}();
$table_name     = 'table_c';
$relate_values  = array(
    'fieldname1' => $bean->id
);
$check_duplicates   = true;
$do_update          = true;
$data_values        = array(
    'fieldname1' => $bean->id,
    'fieldname2' => $parent_id
);
$ModuleObj->set_relationship($table_name, $relate_values, $check_duplicates, $do_update, $data_values);

SugarCRM: Set relationship dynamically

7 thoughts on “SugarCRM: Set relationship dynamically

  1. Vince says:

    Concerning this code it may function improperly if the user triggering this code does not have full access to the $meetings_id. You should add this line before the retrieve :

    $meeting = new Meeting();

    $meeting_user->disable_row_level_security = true;

    $meeting->retrieve($meetings_id);
    $meeting->parent_type = ‘Accounts’;
    $meeting->parent_id = $accounts_id;
    $meeting->save();

    PS : Concerning this example it is probably useless but as the usage of the sugar’s method is good here, it might help if someone gets here and wants to copy / paste the code

    Like

  2. item says:

    Thanks phpbugs.

    i understand for meetings, a flex relate field..maybe a sample with Accounts and Contacts?
    i do not understand what i must fill in variable in :

    $ModuleObj->set_relationship($table_name, $relate_values, $check_duplicates, $do_update, $data_values);

    Regards

    Like

    1. The relatonship with the Accounts and Contacts module can be dynamicallly set using this method. Please see the code below:

      $accounts    	= new Accounts();
      $table_name     = 'accounts_contacts';
      $relate_values  = array(
          'contact_id' => $bean->id
      );
      $check_duplicates   = true;
      $do_update          = true;
      $data_values        = array(
          'contact_id' => $bean->id,
          'account_id' => $parent_id
      );
      $accounts->set_relationship($table_name, $relate_values, $check_duplicates, $do_update, $data_values);
      

      Like

  3. item says:

    Hi,
    thanks for all tips about SugarCrm. Really nice.

    Can i ask something about set a relationship ?
    can you give a sample about account and meetings with set a relationship code ?

    Regards

    Like

    1. The relationship between Accounts and Meetings module are quite different from other relationships. Here the parent_type method is used.


      $meeting = new Meeting();
      $meeting->retrieve($meetings_id);
      $meeting->parent_type = 'Accounts';
      $meeting->parent_id = $accounts_id;
      $meeting->save();

      Please check this code to attain the relationship.

      Like

Leave a comment