Results 1 to 2 of 2

Thread: script to remove first rel'n if change rel'n

  1. #1
    Ramblin is offline Sugar Community Member
    Join Date
    May 2010
    Posts
    98

    Default script to remove first rel'n if change rel'n

    I am almost there and I hope someone can help me with the last twist. I am on SugarCRM CE 6.2.4.

    How can I use script - in a logic hook - to remove a previous relationship made with a Flex Relate field when I make a new selection with the Flex Relate field?
    If I do not do this, I end up with the (in my case Contact) entry being related to two modules/entries.

    in addition to
    Accounts

    I have created 3 custom Modules
    FacilitatorCo
    Venue
    Caterer

    each with a one-to-many relationship to Contacts (one contact, many of the Modules)

    to which a contact can be related,

    I only want to allow one relationship for any one contact, so I created a Flex Relate field and customized the Parent_Type options list so it only shows the above 4 modules (from within the Contacts module).

    I benefited from John Mertic's post at
    SugarCRM Developer Blog » Blog Archive » HOWTO: Create a Flex Relate for other modules
    for creating the right Flex Relate field but I used the Flex_Relate field definition in Module Builder and some custom code in a logic hook to make the relationship connection work.

    But I now find myself in a condition where:

    If the user only uses the Flex_Relate selector once, everything is fine.

    If the user uses the Flex_Relate selector, saves the result, and then later goes back and selects a different choice, unless the user first clicks on the "X" button to the right of the Flex_relate field BEFORE making the new selection (which you cannot count on users to do), then the original relationship stays in place.

    Example:
    I use the Flex_Relate to relate the Contact to Accounts -> Account W and save this
    I later come back and use the Flex_Relate to relate the Contact to Caterers -> Caterer Y and save this
    I end up with the Contact showing in BOTH subpanels in Account W and Caterer Y.

    Is there a script I can use to remove the first relationship. To set the Deleted field to 1 in the Contacts-Accounts relationship table. I have the logic hook ready but do not know the command or setup required to set the deleted field in the relationship table to 1.

  2. #2
    Ramblin is offline Sugar Community Member
    Join Date
    May 2010
    Posts
    98

    Default Re: script to remove first rel'n if change rel'n

    Well,

    I am not sure if this is the correct way to do it or not, but I did find a way to make it work.

    When I set up the additional modules, I created a one-to-many relationship to Contacts for each Module (eg, each Contact can belong to one Facilitator / each Facilitator can have many Contacts = many Contacts to one Facilitators). This way, the Contact name appears in the Subpanel of the other Module (eg Facilitators) once the relationship is established.

    This left me with 4 Relate Fields showing on the Contact Editview and Detailview.

    But I only wanted the user to be able to connect each Contact to ONE of Accounts, Facilitators, Venues or Caterers.

    So, I hid the 4 relate fields and added one Flex_Relate field with the 4 Modules (Accounts, Facilitators, Venues, Caterers) as the Parent_type options.

    I also added a field called Company Type (contact_co_type) so I could later list, sort and search based on Company Type, using descriptors the user would understand.

    To prevent the possibility that a user could create one relationship and then go back later and create another, without removing the first, I added a logic hook that triggered the following code

    Code:
    <?php
    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    
    class ContactsLogicHook 
    	{
    
    	function company_type(&$focus, $event, $arguments) 
    		{
    
    		// Populate the Company Type for this Contact based on the selection from the Flex-Relate Field
    		// If Account Type was already chosen (eg for Converted Lead)
    		//    then set Flex-Relate Field so it will display and link properly
    		//    otherwise set appropriate Company based on Flex_relate selection
    		// This method is used to ensure that Subpanels in the Company views are populated
    		//    using the one-many relationship established manually
    
    		if ( ($focus->parent_type == "") && ($focus->account_name != "") )
    			{
    			$focus->parent_type = "Accounts";
    			$focus->parent_name = $focus->account_name;
    			$focus->parent_id = $focus->account_id;
    			$focus->contact_co_type = "Account";
    			}
    			elseif ($focus->parent_type == "Accounts")
    				{
    				// Set this Contact relationship
    				$focus->contact_co_type = "Account";
    				$focus->account_name = $focus->parent_name;
    				$focus->account_id = $focus->parent_id;
    				// Undo the other Contact Relationships
    				// (this is as a precaution in case another was previously set)
    				$focus->evmgr_fac_contacts_name = "";
    				$focus->evmgr_fac7b_fac_ida = "";
    				$focus->evmgr_ven_contacts_name = "";
    				$focus->evmgr_vc9ba_ven_ida = "";
    				$focus->evmgr_cat_contacts_name = "";
    				$focus->evmgr_ce069_cat_ida = "";
    				}
    			elseif ($focus->parent_type == "EvMgr_Fac")
    				{
    				// Set this Contact relationship
    				$focus->contact_co_type = "Facilitator";
    				$focus->evmgr_fac_contacts_name = $focus->parent_name;
    				$focus->evmgr_fac7b_fac_ida = $focus->parent_id;
    				// Undo the other Contact Relationships
    				// (this is as a precaution in case another was previously set)
    				$focus->account_name = "";
    				$focus->account_id = "";
    				$focus->evmgr_ven_contacts_name = "";
    				$focus->evmgr_vc9ba_ven_ida = "";
    				$focus->evmgr_cat_contacts_name = "";
    				$focus->evmgr_ce069_cat_ida = "";
    				}
    			elseif ($focus->parent_type == "EvMgr_Ven")
    				{
    				// Set this Contact relationship
    				$focus->contact_co_type = "Venue";
    				$focus->evmgr_ven_contacts_name = $focus->parent_name;
    				$focus->evmgr_vc9ba_ven_ida = $focus->parent_id;
    				// Undo the other Contact Relationships
    				// (this is as a precaution in case another was previously set)
    				$focus->account_name = "";
    				$focus->account_id = "";
    				$focus->evmgr_fac_contacts_name = "";
    				$focus->evmgr_fac7b_fac_ida = "";
    				$focus->evmgr_cat_contacts_name = "";
    				$focus->evmgr_ce069_cat_ida = "";
    				}
    			elseif ($focus->parent_type == "EvMgr_Cat")
    				{
    				// Set this Contact relationship
    				$focus->contact_co_type = "Caterer";
    				$focus->evmgr_cat_contacts_name = $focus->parent_name;
    				$focus->evmgr_ce069_cat_ida = $focus->parent_id;
    				// Undo the other Contact Relationships
    				// (this is as a precaution in case another was previously set)
    				$focus->account_name = "";
    				$focus->account_id = "";
    				$focus->evmgr_fac_contacts_name = "";
    				$focus->evmgr_fac7b_fac_ida = "";
    				$focus->evmgr_ven_contacts_name = "";
    				$focus->evmgr_vc9ba_ven_ida = "";
    				}
    
    		}
    
    	}
    ?>
    The convoluted field names )liek evmgr_fac7b_fac_ida) are the system generated (Module Builder) record id fields for the relationship table, which I got by looking in the vardefs file created to establish the relationship.

    Note that I also added a check to see if the Account type was already created - like would be the case or a Lead converted to an Account and set the parameters accordingly if that was the case.

    It works,

    By setting the id field to "", it seems to set the Deleted filed in the relationship to 1. As I said, I am not sure this is how you are supposed to do it, but it works.

    If anyone else has a more elegant (read abides by the SugarCRM programming convention rules that I do not know about) way to do this, please let me know.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 1
    Last Post: 2011-10-30, 02:34 PM
  2. Replies: 0
    Last Post: 2011-05-10, 06:19 PM
  3. Replies: 1
    Last Post: 2008-09-24, 03:48 PM
  4. change/remove colums
    By sugarcare in forum Developer Help
    Replies: 2
    Last Post: 2006-11-26, 05:27 PM
  5. Replies: 1
    Last Post: 2005-12-28, 05:59 PM

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
  •