Logic Hook Field-Change Detection


Aug 25, 2017    Janaki Mahapatra, Sugar

SugarCRM Logic Hooks are a powerful way of interacting with data or other systems on specific system actions, usually retrieving or saving a record. A common customization challenge we face is saving a record and performing an action if a particular field has changed.
if($bean->$key_field != $bean->fetched_row[$key_field]{
   // where $key_field = the field you are looking for
}

class DataSync
{
	/*
	 * The method that does the checking for the specific fields. This is
	 * the method that we'll want to call in the logic_hooks.php file.
	 */
	function CheckUpdatedFields($bean, $event, $arguments){
		// if there are more than one field you're interested in, use an array
		$key_fields = array('phone_home','phone_other','phone_work');
		// and cycle through them
		foreach($key_fields as $key_field){
			// do a change detection leveraging the fetched_row value
			if($bean->$key_field != $bean->fetched_row[$key_field]{
				// and if a change is detected, queue up the Sync method
				$this->SyncToSystem($bean,$key_field);
			}
		}
	}

	/*
	 * A Generic Placeholder function, to be called when a change is detected
	 */
	function SyncToSystem($bean,$field){
		// placeholder
		return true;
	}
}