Sync fields between modules Accounts and Contacts


I had recently set up a logic_hook in module Accounts in order to auto-sync the accounts fields industry and account type (in our case custom fields “branche_c” and “firmentyp_c”) to the related Contacts, when one of those fields is changed in module Accounts.

The following code worked a while back, but has stopped working and I can’t figure out why.
I debugged every line before $contact->save(); and they all seems to work fine, but the save doesn’t work.

<?php if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); class ContactsFieldsSync { function contactsFieldsSync($bean, $event, $arguments) { // check if any changes were made if (($bean->branche_c != $bean->fetched_row["branche_c"]) || ($bean->firmentyp_c != $bean->fetched_row["firmentyp_c"]) ) { $focus = new Account(); $focus->retrieve($bean->id); $focus->load_relationship('contacts'); $contacts_arr = Array(); $contacts_arr = $focus->contacts->getBeans(); foreach ($contacts_arr as $key => $value) { $contact = new Contact(); // initiate Contact object $contact->retrieve($key); // retrieve related account field data $contact->branche_c = $bean->branche_c; $contact->firmentyp_c = $bean->firmentyp_c; $contact->save(); // save not working!! } // end foreach } // end if } // end function contactFieldsSync } // end class ContactFieldsSync ?>

You have to put your code inside the forum’s code tags, otherwise anything inside square brackets will disappear.

You mentioned you debugged the code; why don’t you just keep debugging, into the Save function (SuiteCRM code)? Even though you probably won’t understand everything, you might get a clue of where it seems to take a wrong turn…