Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: How to I get the ID a record that is created in a logic hook in the Hook

  1. #1
    chrislynch8's Avatar
    chrislynch8 is offline Sugar Community Member
    Join Date
    Oct 2007
    Location
    Cork, Ireland
    Posts
    747

    Default How to I get the ID a record that is created in a logic hook in the Hook

    The title might not explain extactly what I need.

    I have a before_save logic hook and depending on a field in the record the before_save will create a new contact record. I have it passing values from the current record into the new record as follows

    PHP Code:
    $contact = new Contact();
    $contact->first_name $bean->fdc_first_name;
    $contact->last_name $bean->fdc_las_name;
    and 
    so on 
    $contact
    ->save(); 
    hen below the last line above I have more custom logic but I need to uset he ID of the new contact it in to create a relationship to an account by adding to hte accounts_contacts table .

    I need to get the id of the newly created record.

    Rgds
    Chris
    Linkedin Profile:Chris Lynch

    FDC IT Solutions
    FDC House
    Wellington Road
    Cork
    Ireland

  2. #2
    wdroush is offline Senior Member
    Join Date
    Oct 2009
    Posts
    159

    Default Re: How to I get the ID a record that is created in a logic hook in the Hook

    I'm not sure, can you force the ID? Do a "SELECT UUID()" from MySQL and force the contact's ID to be that?

    Just a guess.

  3. #3
    tellusmore is offline Senior Member
    Join Date
    Jun 2009
    Location
    Pittsburgh
    Posts
    60

    Default Re: How to I get the ID a record that is created in a logic hook in the Hook

    all my logic hooks i did everything "manually" via mysql_query's

    force sugar's hand, it's the only way, just make sure you include all the extra tables they use for every single piece of info

    like the 2 tables they use just to store an email address...

    query away!

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

    Default Re: How to I get the ID a record that is created in a logic hook in the Hook

    At least use $db

    PHP Code:
    global $db
     
    $query 
    "write your query"
    $result $db->query($query);
    while (
    $row $db->fetchByAssoc($result)) {
    ...
    some stuff...

    SugarForge Projects:
    JJWDesign Google Maps
    JJWDesign Tools and Reports

    Follow my blog postings at JJW Design.

  5. #5
    SugarDev.net is offline Sugar Community Member
    Join Date
    Feb 2008
    Posts
    1,401

    Default Re: How to I get the ID a record that is created in a logic hook in the Hook

    Direct querying for stuff like this??



    Here's how it's done:

    PHP Code:
    $bean->id create_guid();
    $bean->new_with_id true;

    $contact = new Contact();
    $contact->first_name $bean->fdc_first_name
    $contact->last_name $bean->fdc_las_name
    and 
    so on  
    $contact
    ->save(); 
    $bean->id is yours for the taking.
    Developers go here
    Businesses go there (Dutch)

    Modules:
    SugarDev.net Developer Tools | Config | Dutch Language Pack
    "Nothing gets fixed unless there is a bug"

  6. #6
    datasponge is offline Sugar Community Member
    Join Date
    Mar 2008
    Location
    San Jose, CA, USA
    Posts
    553

    Default Re: How to I get the ID a record that is created in a logic hook in the Hook

    I create the guid beforehand and save it with the record so I know it.

    Here's a function to create a guid

    PHP Code:
    function guid(){
        if (
    function_exists('com_create_guid')){
            return 
    com_create_guid();
        }else{
            
    mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
            
    $charid strtolower(md5(uniqid(rand(), true)));
            
    $hyphen chr(45);// "-"
            
    $uuid substr($charid08).$hyphen
                    
    .substr($charid84).$hyphen
                    
    .substr($charid,124).$hyphen
                    
    .substr($charid,164).$hyphen
                    
    .substr($charid,20,12);
            return 
    $uuid;
        }

    Then you just add a line between your lines like:

    PHP Code:
    $contact = new Contact();
    $contact->id guid();
    $contact->first_name $bean->fdc_first_name
    Then you can reference $contact->id or save it to another temporary variable to use later.

    Phil

    [Edit - looks like SugarDev snuck in before me with a similar answer and better data regarding using the bean with hooks]
    Last edited by datasponge; 2010-01-29 at 04:55 AM.

  7. #7
    chrislynch8's Avatar
    chrislynch8 is offline Sugar Community Member
    Join Date
    Oct 2007
    Location
    Cork, Ireland
    Posts
    747

    Default Re: How to I get the ID a record that is created in a logic hook in the Hook

    Quote Originally Posted by SugarDev.net View Post
    Direct querying for stuff like this??



    Here's how it's done:

    PHP Code:
    $bean->id create_guid();
    $bean->new_with_id true;

    $contact = new Contact();
    $contact->first_name $bean->fdc_first_name
    $contact->last_name $bean->fdc_las_name
    and 
    so on  
    $contact
    ->save(); 
    $bean->id is yours for the taking.
    Si I add
    PHP Code:
    $bean->id create_guid();
    $bean->new_with_id true
    Then that is the ID that will be given to the contact with the $contact->save();

    I don't fully understand, how - because is $bean->id not the ID of the record the logic is currently running for. Could you explain a little further please.

    Rgds
    Chris
    Linkedin Profile:Chris Lynch

    FDC IT Solutions
    FDC House
    Wellington Road
    Cork
    Ireland

  8. #8
    Gyro.Gearless is offline Senior Member
    Join Date
    Apr 2009
    Posts
    47

    Default Re: How to I get the ID a record that is created in a logic hook in the Hook

    Quote Originally Posted by chrislynch8 View Post
    The title might not explain extactly what I need.

    I have a before_save logic hook and depending on a field in the record the before_save will create a new contact record. I have it passing values from the current record into the new record as follows

    PHP Code:
    $contact = new Contact();
    $contact->first_name $bean->fdc_first_name;
    $contact->last_name $bean->fdc_las_name;
    and 
    so on 
    $contact
    ->save(); 
    hen below the last line above I have more custom logic but I need to uset he ID of the new contact it in to create a relationship to an account by adding to hte accounts_contacts table .

    I need to get the id of the newly created record.

    Rgds
    Chris


    IIRR, SugarBean::save() returns the id of the newly created record, so just take the return value from $contact->save() ; otherwise $contact->id should also do the trick. See data/SugarBean.php....

    HTH
    Gyro

  9. #9
    chrislynch8's Avatar
    chrislynch8 is offline Sugar Community Member
    Join Date
    Oct 2007
    Location
    Cork, Ireland
    Posts
    747

    Default Re: How to I get the ID a record that is created in a logic hook in the Hook

    Quote Originally Posted by Gyro.Gearless View Post
    IIRR, SugarBean::save() returns the id of the newly created record, so just take the return value from $contact->save() ; otherwise $contact->id should also do the trick. See data/SugarBean.php....

    HTH
    Gyro
    That is perfect the $contact->id is the correct ID I was looking for. Thanks

    Rgds
    Chris
    Linkedin Profile:Chris Lynch

    FDC IT Solutions
    FDC House
    Wellington Road
    Cork
    Ireland

  10. #10
    wdroush is offline Senior Member
    Join Date
    Oct 2009
    Posts
    159

    Default Re: How to I get the ID a record that is created in a logic hook in the Hook

    Quote Originally Posted by Gyro.Gearless View Post
    IIRR, SugarBean::save() returns the id of the newly created record
    FUUUUUUUUUUUUUUUUUUUU

    I didn't know that, oh the pain I've had forcing UUID generation through MySQL.

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. What logic hook should I use before create new record
    By AlexCh in forum Developer Help
    Replies: 4
    Last Post: 2010-08-10, 09:12 PM
  2. Logic hook - process record.
    By kinshibuya in forum Help
    Replies: 1
    Last Post: 2009-07-15, 09:07 PM
  3. Replies: 2
    Last Post: 2009-04-07, 09:29 AM
  4. How make logic hook for create new record
    By meeric in forum Developer Help
    Replies: 6
    Last Post: 2009-01-02, 07:49 AM
  5. How to delete a record in a logic hook
    By twguk1516 in forum Developer Help
    Replies: 2
    Last Post: 2008-08-08, 08:56 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
  •