#409 - Assigning Child Records
I was looking for a way to reassign all child records of an account when it's assigned to another user (I'm a php programmer but I don't know Sugar) and stumbled upon this solution http://greg.ambrose.id.au/2013/11/03/assigning-child-records-in-sugarcrm/ (I tested it and it works fine). Can you provide me with the required lines of code to also reassign the SecurityGroups (I'm guessing it's fairly straightforward)? Also, can you provide the same solution but using the "Primary Group" feature because I'm planning to upgrade from 6.5.13 to .16 in the not too distant future. Thanks.
10 years ago
I actually have Greg's solution bookmarked and may lean on it when I fully implement this feature. I don't have any time to come up with the code right now for you, but Greg pretty much did it all already. You just need to load the SecurityGroups relationship and do what you need to do.
10 years ago
Unfortunately since I have no SugarCRM code knowledge, I'm clueless as to what I'm supposed to do. If you can post something in the next couple of days that would be greatly appreciated.
10 years ago
I can't do that within that time frame. I'm working around the clock right now. If you're familiar with PHP you should be able to get in there and do it. The starting point is learning how to do a before_save logic hook in SugarCRM: http://support.sugarcrm.com/02_Documentation/04_Sugar_Developer/Sugar_Developer_Guide_6.5/03_Module_Framework/Logic_Hooks/02_Module_Hooks/before_save
10 years ago
The hook is not the issue (already implemented Greg's solution and works fine). It's just the few lines of code to remove the child records' current security groups and then add the new user's security groups. Maybe there's an existing sugar php file that contains your code that already does this (or similar to it)?
10 years ago
Awesome! I can help then. Here's how to add a group (taken from AssignGroups.php in modules/SecurityGroups):
~~~~~~ require_once('modules/SecurityGroups/SecurityGroup.php'); $groupFocus = new SecurityGroup(); $security_modules = $groupFocus->getSecurityModules(); //sanity check if(in_array($bean->module_dir,array_keys($security_modules))) { //add each group in securitygroup_list to new record $rel_name = $groupFocus->getLinkName($bean->module_dir,"SecurityGroups");
~~~~~
Just take the parts that you need. Probably don't need the security_modules check.
Removing would be very similar:
$bean->$rel_name->remove($group_bean, $bean);
Or something similar. Exact syntax is in data/Relationships/M2MRelationship.php
10 years ago
Excellent, thanks for the info. I will post back my results.
9 years ago
I actually never got a chance to complete this and dove back into it today. I was able to utilize the load_relationship(), add() & delete() calls to manage the groups on all the related records and it works great. Unfortunately, it does not work for modules that used the "Hookup Module" tool to relate to security groups. The load_relationship() call returns false. The relationship name passed in to load_relationship() comes from the get_linked_fields() function, so I'm pretty sure that's correct. Any ideas on what the problem might be?
9 years ago
It's likely because a "link" type field needs to be created for the related module via the vardefs. If you create this manually like it is done in the stock modules then it should work.
9 years ago
If I look at custom/modules/Accounts/Ext/Vardefs/vardefs.ext.php, I have the following snippet of code that refers to SecurityGroups:
$dictionary['Account']['fields']['SecurityGroups'] = array ( 'name' => 'SecurityGroups', 'type' => 'link', 'relationship' => 'securitygroups_accounts', 'module'=>'SecurityGroups', 'bean_name'=>'SecurityGroup', 'source'=>'non-db', 'vname'=>'LBL_SECURITYGROUPS', );
and in custom/modules/AOS_Invoices/Ext/Vardefs/vardefs.ext.php (using AOS_Invoices as an example from SuiteCRM), I have:
$dictionary["AOS_Invoices"]["fields"]["aos_invoices_securitygroups_1"] = array ( 'name' => 'aos_invoices_securitygroups_1', 'type' => 'link', 'relationship' => 'aos_invoices_securitygroups_1', 'source' => 'non-db', 'module' => 'SecurityGroups', 'bean_name' => 'SecurityGroup', 'vname' => 'LBL_AOS_INVOICES_SECURITYGROUPS_1_FROM_SECURITYGROUPS_TITLE', );
Anything else you can think of?
9 years ago
Are you using "aos_invoices_securitygroups_1'" when you call load_relationship()?
9 years ago
Yes I am but I had a bug somewhere else! Thanks for your help.
9 years ago
Whew! Had me nervous there. Have a great weekend!
9 years ago
Thanks you too