The inheritance of the parent records does not work when creating a record via code

I have the following configuration in SecuritySuite:

Inherit from Created By User = false
Inherit from Assigned To User = false
Inherit from Parent Record = true

When I create a new Contact through the CRM interface and link an Account in the account_name field, the record created correctly inherits the Security Groups of the Account. This works as expected.

However, when I do this same operation programmatically (via Logic Hook, via Scheduler task, via controller.php file, etc) then the Contact created does not inherit the Security Groups of the Account.

The code I use to create the record is:

$contactsBean = BeanFactory::newBean('Contacts');
$contactsBean-> last_name = 'John';
$contactsBean-> account_id = '8bf97d3b-aeac-4522-682e-5f76ffc69b88'; // Account id with Security groups 
$ contactsBean-> save();

Is this the expected behavior in SecuritySuite or is it a bug?

The same thing happens in any other base or custom module.

The SuiteCRM version that we use is 7.11.15

Thanks!

Hi,

Can you try with

$_REQUEST['relate_to']='Accounts';
$_REQUEST['relate_id']= $contactsBean->account_id;

just before $contactsBean->save();

In a punctual case, I could solve it according to your proposal (thanks!), but the situation repeats every time we include the creation of a new record of any module via code: the created record does not inherit the security groups of the parent record.

It seems reasonable that this works automatically, just like when we create a record using EditView or quickCreateView.

Perhaps this behavior has never been included in SecuritySuite because the developers have not considered it appropriate, if so, I would like to know why.

Have a look at function inherit_parent in modules/SecurityGroups/SecurityGroup.php
This only works when creating records with the Web interface as it uses $_REQUEST.

Depending on the type of records you create, you might also have to set $_REQUEST[‘parent_type’] and $_REQUEST[‘parent_id’]

Yes, I have seen that this behavior is defined in the /SecurityGroups/SecurityGroup.php file.

However I think the dependency on the existence of $_REQUEST is unnecessary.

This code could be modified so that the inheritance of the security groups of the parent record does not depend on the existence of $_REQUEST ['relate_to'] and $_REQUEST ['relate_id']. In this way, inheriting the parent record would also work when creating a record through code.

Apart from the fact that we should not modify core files … Would it be a problem for the inheritance mechanism to work also when $_REQUEST ['relate_to'] and $_REQUEST ['relate_id'] (or other $_REQUEST[...]) do not exist?
Why?