After about three hours of working through the code and using a lot of DEBUG messaging, I figured out the problem - and it's something that should probably be looked at by Sugar team. Looking through the 4.5.0f patch, it doesn't appear to be fixed yet. Basically, custom fields saved via the "DynamicFields" module - not by the "SugarBean". If you look at the "save()" method of DynamicFields, it doesn't take into account how to handle fields if they are set to a "zero length" value. That is, when you save a form, it appears that all fields get assigned, even if they are blank. The "SugarBean" takes care of weeding out (setting to NULL) any values that were not filled in on the form. However, the "DynamicFields" module doesn't do this. SO, all of your custom fields get set as blank - which isn't a huge deal for string fields in the DB (although NULL would be better) - but it's a HUGE deal for dates. A blank insert ('') isn't permissible. That's why these fields were failing. This must be some new behavior in 4.5, because we never had this problem in 4.2 and earlier.
To fix this, I simply took the logic in the save() method of SugarBean and applied it to the save() method of the DynamicFields module. Staring at line 209 (in Sugar 4.5.0c) of "DynamicFields" - here is the edit:
PHP Code:
if(isset($this->bean->$name)){
//Added Ryan Booz
if(strlen($this->bean->$name) <= 0) {
if(!$isUpdate && isset($field['default']) && (strlen($field['default']) > 0)) {
$this->bean->$name = $field['default'];
}
else {
$this->bean->$name = null;
}
}
if(is_null($this->bean->$name)) {
$value = "null";
}
else {
$value = "'".PearDatabase::quote(from_html($this->bean->$name))."'";
}
if($isUpdate){
if($first){
$query .= " $name=$value";
}else{
$query .= " ,$name=$value";
}
}
$first = false;
$queryInsert .= " ,$name";
$values .= " ,$value";
}
This follows the logic in Sugarbean that if the value is empty it gets set to NULL or the default Sugar vaule in the UPDATE or INSERT statement. After these edits, my problem is fixed.
Bookmarks