Results 1 to 4 of 4

Thread: How to create filtered relationships without losing Sugar link management

  1. #1
    edg
    edg is offline Sugar Community Member
    Join Date
    Feb 2007
    Location
    Atlanta, GA
    Posts
    46

    Post How to create filtered relationships without losing Sugar link management

    Another customization that appears not to have a solution posted to the forums is how to get around the limited view functionality on the standard Sugar virtual fields for one-to-many and one-to-one relationships. This is actually quite easy and basically requires only 1 line of code in a logic_hook or custom module save function.

    Here are the steps:

    1) Create a standard relate field name/id pair in the "right hand side" of the relationship.

    2) Add in your favorite flavor of "initial-filter" or, as I have done, change the id field to an enum using a function that returns a filtered array that feeds the select list option list (dropdown).

    3) Create a relationship entry in the primary module.

    4) Deploy the module relationship change with any necessary Quick Repairs.

    5) Go lookup the field name for the rhs_key that was created for the relationship. You can find that either in the database table record or the vardefs directory for the right hand side module(custom/modules/<rhs_module_name>/Ext/Vardefs/vardefs.ext.php).

    6) Add a save() function to the custom module or as a logic_hook. In that function, and before you call parent::save(), add one line of code to assign the value of the relate field id to the virtual rhs_key field.

    For example, if I have custom module 'Subscribers' and I want to relate it to Accounts, create an 'account' relate name field that also creates an relate id field that is actually stored in the database (i.e. account_id_c). Then, add the relationship in studio or modulebuilder. After deploying the relationship, assume there is a rhs_key name of accounts22cfdsubscr_ida.

    In a logic hook or inside the custom module code, add a field equate command between the relate id and the rhs_key field. Deploy this code and let Sugar handle the rest.

    Note: If you're using modulebuilder, be certain to delete any auto-generated virtual (non-db) fields in the view definitions so that you don't duplicate the relate field in the view.

    Sample Code:
    Code:
    <?php
        function save($check_notify = FALSE) {
          $this->accounts22cfdsubscr_ida = $this->account_id_c;
          $parent::save($check_notify);
        } 
    ?>
    Last edited by edg; 2009-05-10 at 03:06 AM. Reason: title type correction

  2. #2
    edg
    edg is offline Sugar Community Member
    Join Date
    Feb 2007
    Location
    Atlanta, GA
    Posts
    46

    Post Re: How to create filtered relationships without losing Sugar link management

    This is a follow-up to my original post with a way to establish this functionality in a custom module or logic hook without requiring post-deployment modifications with specific key values. First, a bit about what got me to this point.

    In trying to establish a pre-defined relationship in modulebuilder, the relationship keys are undefined until the module is deployed. During deployment, the link and non-db fields for the relationship are generated automatically and added to the package file in the custom/Extension/modules/<your_module_name>/Ext/Vardefs directory.

    This Vardefs package file is concatenated (not recreated) at each deployment so care needs to be taken with relationship creations and deletions over multiple deployments as you can end up with invalid field entries here.

    During repair of Extensions for the module, the package file is used to build the vardefs.ext.php file in the custom/modules/<your_module_name>/Ext/Vardefs directory. It is possible to alter the field defintions for the relationship in this package file but because each deployment restates the field values, you would need to edit this file and repair Extensions to implement these changes. Also, if you uninstall the module (including tables) and reinstall it, the key values change and all the field definition modification will need to be done over.

    What I wanted/needed to do was establish a method of relationship that allowed requiring and/or filtering of the related record that survives re-deployment and module re-installation. This also allows "real" relationships to be developed in modulebuilder and installed without modifications across different package installs.

    The revised steps to implement this type of relationship are as follows:

    1) Add the relationship in modulebuilder in the "left hand side" of the relationship. Record the relationship name.

    2) Create a standard relate field name/id pair in the "right hand side" of the relationship.

    3) Add in your favorite flavor of "initialFilter" or, as I have done, change the id field to an enum using a function that returns a filtered array that feeds the select list option list (dropdown) in the modulebuilder vardefs.php file.

    4) In the right hand side module, add the relationship assignment code in a save() function in the custom module code or in a logic hook to get the join_key field name and assign the value.

    Using the example in the original post above with a one-to-many relationship between Accounts and the custom Subscribers module and an account_id_c relate id field, here's sample code:

    Sample Relationship Assignment Code:
    Code:
    <?php
        function save($check_notify = FALSE) {
          $r = new Relationship();
          $r->retrieve_by_name('accounts_subscribers');
          if (isset($r->join_key_lhs)) {
            $lhs_key = $r->join_key_lhs;
            $this->$lhs_key = $this->account_id_c;
            unset($r);
          }
         parent::save($check_notify);
        }
    }
    ?>
    A couple of implementation notes:

    1) As with the first example, remember to remove the non-db fields from editviewdefs.php and detailviewdefs that modulebuider adds when you create the relationship.

    2) IMPORTANT: Remove the select button from the module subpanel. If this button select is on the subpanel, a user can update the relationship without updating the Relate id field in the module db record.
    Last edited by edg; 2009-05-10 at 08:42 PM. Reason: example code correction

  3. #3
    edg
    edg is offline Sugar Community Member
    Join Date
    Feb 2007
    Location
    Atlanta, GA
    Posts
    46

    Post Re: How to create filtered relationships without losing Sugar link management

    If you need to restrict the relationship to specific subset of module records, I have posted a Developer Tutorial that defines how to use custom popups to restrict Related record selections with custom popups - and, therefore, any associated relationship. The tutorial can be found here: http://www.sugarcrm.com/forums/showthread.php?t=47887.

    Extending the above example, this functionality could be used to restrict the type of Account that can be selected by a Subscriber (e.g. Customer Accounts only).

  4. #4
    jleaman's Avatar
    jleaman is offline Senior Member
    Join Date
    May 2009
    Location
    Boston
    Posts
    58

    Default Re: How to create filtered relationships without losing Sugar link management

    I am doing something fairly similar but when I run parent::save() I get a fatal error.

    PHP Code:
    function add_code(&$bean$event$arguments) {
    $bean->opps1_serv4ac1icesopp_ida $parent_id;
    parent::add_code();

    I tried a few different methods. Without the "parent::" line I get through but the relationship doesn't update..

    Any ideas??

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 5
    Last Post: 2008-10-15, 03:18 PM
  2. Unable to create relationships with non-module tables
    By rudibr30 in forum Developer Help
    Replies: 2
    Last Post: 2008-09-26, 12:03 PM
  3. Want custom link fields to create relationships
    By evilolive in forum Developer Help
    Replies: 2
    Last Post: 2008-03-27, 01:42 PM
  4. Replies: 0
    Last Post: 2008-02-03, 04:44 PM

Tags for this Thread

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
  •