Need some help with security Groups for Custom Module

I created a custom module “buildings” and I’ve been successful in adding Security groups to it (via code). If I add security groups manually it works properly. However, when I save a NEW record in the custom module it doesn’t add the security group of the user.

I’ve done a bunch of research, and there’s tid bits everywhere, but not a complete working solution.

So here’s what I’ve got so far, it’s an after save hook, and it kind of works… it does add the relationship to the table security_groups_records and it appears to be correct. However, id doesn’t show on the front end?

Note: I still have to add code to limit to new record creation and to grab the users security group. However, this is the nuts and bolts (also I’ve got log notes in there so I can see what’s going on).

Anyone done this before or have suggestions?

<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class roleAsHook {

    function updatesecuritygroup (&$bean, $event, $arguments) {
				$field_securityGroup='EVSTART';
				$securityTeam = new SecurityGroup(); 
				$securityTeam->retrieve_by_string_fields(array('name' => $field_securityGroup )); 
				if ( $securityTeam->id == null) { 
                  $GLOBALS['log']->fatal("Group with the name '" . $field_securityGroup . "' wasn't found!"); 
				} else{
				$bean->load_relationship('SecurityGroups'); 
				$bean->SecurityGroups->add($securityTeam->id); 
				$bean->save();
				$GLOBALS['log']->fatal("Bean was saved"); 
                }
							
				

		}

	
}

Any input would be appreciated!!

Ha ha! Got it working… duh, forgot to follow my own instructions.

You don’t need to do the above at all. If you create a custom meta file (see my post linked below) it works without having to crate the relationship on save. It actually does it automatically. I feel so dumb! I added this for the other custom module I was working on, but not this one. So, all you have to do (so far anyway) is add the custom metadata file as in the post below.

https://community.suitecrm.com/t/i-have-created-custom-module-through-module-builder-but-custom-module-s-security-group-sub-panel-not-display-and-when-i-check-in-relationship-the-relationship-already-there/84939

So using both these customizations, gets SecuritySuite working with a custom module (mostly) still have some testing to do.

1 Like