Results 1 to 5 of 5

Thread: after_save hook db update

  1. #1
    rusdvl is offline Senior Member
    Join Date
    Feb 2009
    Posts
    48

    Default after_save hook db update

    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.

  2. #2
    jjwdesign's Avatar
    jjwdesign is offline Sugar Community Member
    Join Date
    Dec 2006
    Location
    Orlando, FL
    Posts
    503

    Default Re: after_save hook db update

    I'm not sure if I have the best advice, but here goes. Most of what I've been able to learn about the $bean and the logic hooks, came from displaying the $bean (testing instance). Just var_dump the $bean to see the values at the place where you are doing the SQL or modifications.

    PHP Code:
    var_dump($bean);
    exit; 
    This should give you an idea of the values before_save. I've usually used an after_save logic hook and then re-saved the bean once the modifications were done. I've posted several logic hooks in the past on this forum. Just search for my posts if interested.

    Concerning your task. Isn't there already ID's in the leads table which identifiy the related contact/account IDs?


    Hope this helps some.
    SugarForge Projects:
    JJWDesign Google Maps
    JJWDesign Tools and Reports

    Follow my blog postings at JJW Design.

  3. #3
    rusdvl is offline Senior Member
    Join Date
    Feb 2009
    Posts
    48

    Default Re: after_save hook db update

    Hey,

    Thanks for the advice... I'll try that and I'll dig around in your posts.

    What I am trying to do is... I've got an email marketing client and I am trying to integrate it with sugar. So when a user creates a new lead or contact and selects that that lead or contact wants to receive a newsletter, upon saving I need to contact that email client and add the user to their database to a specific list. Then I get an xml reply from that client with the ID that they provide for that user (from their database) and I need to grab that ID via xml and save it into the sugar database under the custom field in contact or lead.

    So all the interaction between sugar and the email people happens its just i need to double save each time for sugar to send the details (run the functions)

    Hope that explains it....

  4. #4
    jjwdesign's Avatar
    jjwdesign is offline Sugar Community Member
    Join Date
    Dec 2006
    Location
    Orlando, FL
    Posts
    503

    Default Re: after_save hook db update

    We use a different email marketing program as well. Here's what were doing to help with the communication between the two systems. I've setup the Windows ODBC driver for MySQL on the email/spam computer to allow us to connect to the SugarCRM database. From there we can SELECT the tragets/leads or whatever and UPDATE the data when needed. Of course that requires some understanding of the SQL tables. The custom tables are also a bit of a pain to deal with if you have to, but at least it allows you to update the data thru MySQL.

    There's also a SOAP API for SugarCRM, but the program we're dealing with doesn't have support for making the SOAP calls.
    SugarForge Projects:
    JJWDesign Google Maps
    JJWDesign Tools and Reports

    Follow my blog postings at JJW Design.

  5. #5
    rusdvl is offline Senior Member
    Join Date
    Feb 2009
    Posts
    48

    Default Re: after_save hook db update

    Thanks but we figured out the problem....
    The problem was that after_save, the bean discards all the new field info. So we needed to use $bean->retrieve($bean->id); to get it back. Then, it didn't retrieve the email addresses properly, so we needed to pull the email address out before we ran the retrieve.

    Here is the code now....
    The hook:
    $hook_array = Array();
    $hook_array['after_save'] = Array();
    $hook_array['after_save'][] = Array(1, 'mobilize2', 'custom/include/ContactBeforeSave.php','ContactBeforeSave', 'EmailClient');

    The class: (its named before save but its actually running after save... a bit confusing i know but you can name it whatever you want)
    class ContactBeforeSave {
    function EmailClient(&$bean, $event, $arguments){
    $email_address = $bean->emailAddress->addresses[0]['email_address'];
    $bean->retrieve($bean->id);
    global $sugar_config;
    // if hook_logic event is after_save, then do the following
    if ($event == 'after_save'){
    $newsletter_type = $bean->fetched_row['newsletter_type_c'] != null ? $bean->fetched_row['newsletter_type_c'] : $bean->newsletter_type_c;
    $email_signup = $bean->fetched_row['email_newsletter_signup_c'] != null ? $bean->fetched_row['email_newsletter_signup_c'] : $bean->email_newsletter_signup_c;

    /* Adding the subscriber to email client*/
    // if the field newsletter_type_c is not set to -blank-
    if($bean->fetched_row['newsletter_type_c'] != NULL && $bean->fetched_row['newsletter_type_c'] != '-blank-'
    && $bean->fetched_row['email_newsletter_signup_c'] == 'Yes') {
    $subscriberid = $this->createSubscriber($bean, $email_address);
    $bean->email_client_id_c = $subscriberid;
    $query = "UPDATE contacts_cstm SET email_client_id_c = '".$bean->email_client_id_c."' WHERE id_c = '".$bean->id."'";
    $bean->db->query($query, true);
    }


    /* Deleting the subscriber from email client */
    if(!empty($bean->fetched_row['mobilizemail_id_c']) && $bean->fetched_row['newsletter_type_c'] == '-blank-'){
    $stuff = $this->deleteSubscriber($bean);
    $query = "UPDATE contacts_cstm SET email_client_id_c = '' WHERE id_c = '".$bean->id."'";
    $bean->db->query($query, true);
    }
    }
    }
    /*
    OTHER FUNCTION THAT FOLLOW
    deleteSubscriber($bean) and createSubscriber($bean, $email_address)
    */

    }


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. after_save hook showing old values
    By jjshoe in forum Developer Help
    Replies: 7
    Last Post: 2010-11-17, 06:32 PM
  2. Logic hook for update Account after update Contact
    By sugaradmin in forum Developer Help
    Replies: 11
    Last Post: 2008-11-28, 03:12 PM
  3. How to update a related module in a post_save hook?
    By SanderMarechal in forum Developer Help
    Replies: 4
    Last Post: 2008-10-21, 10:08 AM
  4. Replies: 1
    Last Post: 2008-07-11, 05:56 PM
  5. Logic Hook create/update
    By dricrm in forum Developer Help
    Replies: 2
    Last Post: 2007-02-13, 08:57 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •