Hi,
I am trying to update a field once a save has been completed, but before save I also call a function to save the email and name of a contact and return with an id (to/from a different database)
Here is my hook:
$hook_array = Array();
$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(1, 'custom', 'custom/include/ContactBeforeSave.php','customfield', 'customfield');
$hook_array['after_save'] = Array();
$hook_array['after_save'][] = Array(1, 'custom2', 'custom/include/ContactBeforeSave.php','customfield', 'customfield');
and here is the class:
require_once('data/SugarBean.php');
require_once('modules/Contacts/Contact.php');
require_once('include/utils.php');
require_once('custom/include/client.php');
class customfield{
function customfield(&$bean, $event, $arguments){
global $sugar_config;
global $subscriberid;
global $field1;
global $field2;
global $field3;
global $field4;
$field1 = $bean->fetched_row['newsletter_type_c'];
$field2 = $bean->fetched_row['customfield_id_c'];
$field3 = $bean->retrieve($bean->newsletter_type_c) ;
$field4 = $bean->retrieve($bean->customfield_id_c) ;
if ($event == 'before_save'){
if($field1 != NULL && $field1 != '-blank-'){
$subscriberid = createSubscriber($bean);
}
elseif(!empty($field2) && $field1 == '-blank-'){
deleteSubscriber($bean);
}
}
if ($event == 'after_save' && empty($field4) || $field4 == '0'){
$query = "UPDATE contacts_cstm SET customfield_id_c = '$subscriberid' WHERE id_c = '".$bean->fetched_row['id']."'";
$bean->db->query($query, true, "Error adding custom lead info: ");
}
elseif ($event == 'after_save' && !empty($field4) || $field4 != '0'){
$query = "UPDATE contacts_cstm SET customfield_id_c = NULL WHERE id_c='".$bean->fetched_row['id']."'";
$bean->db->query($query, true, "Error removing custom lead info: ");
}
}
}
So this basically works except for the fact that I have to edit and save twice before it either adds an id or removes the id (both from the sugar database and the other database)
Im guessing its because its not picking up the new field input value... so it saves it first with the new value (and none of the function run) and upon saving it the second time it gets the correct info and runs the functions...
Any ideas?
I also might be doing this in a complicated way, but I couldnt find anything that would do what I need to do... so if anyone has a better solution im all ears.![]()


LinkBack URL
About LinkBacks




Reply With Quote
Bookmarks