Remove security group with bean

Hi there! I’m having trouble deleting a relationship from a record in the “Bugs” module with a security group. This is my before_save hook and I don’t understand why it’s not deleting, as it processes but doesn’t have any action.



<?php

if (!defined('sugarEntry') || !sugarEntry) {
    die('Not A Valid Entry Point');
}

class RemoveClass
{

    function removeSecurityGroup($bean, $event, $arguments)
    {



        if ($bean->fetched_row['status'] == 'Assigned' && $bean->status == 'New') {
            $bean->load_relationship('SecurityGroups');
            $bean->SecurityGroups->delete('3248ee1e-2841-5c8d-6cdd-6446d722fdf9'); //Este es el ID del grupo de seguridad a borrar.

        }
    }
}

Thanks!

Hey
Please look at the documentation of the delete function in relationships

According to the documentation you need to pass ids of both the beans. You are passing the id of the security group only

Regards

Hi !
Well, i tried passing both id’s but don’t work. I think the relationship with Security Groups isn’t accesible by this way…

Another thing to consider is that Updating of securitygroups for a bean occurs after the “before_save” logic hook. So maybe it will be better to use an after_save logic hook. And even in the after save logic do a query instead of relationships
For example something like

$bean_id=$bean->id;
$query= "update securitygroups_records set deleted=1 where securitygroup_id='3248ee1e-2841-5c8d-6cdd-6446d722fdf9' and record_id='$bean_id' and module='Bugs'"

Or maybe the relationship name that you use for loading the relationship is wrong? Studio suggests its securitygroups_bugs, but i didn’t test to confirm (the relationship names are strange from time to time, I always have to test first too).

I think that too, but load_relationship(“securitygroups_bugs”) returns null.

class RemoveClass
{

    function removeSecurityGroup($bean, $event, $arguments)
    {


        if ($bean->fetched_row['status'] == 'Assigned' && $bean->status == 'New') {
        //  $bean->load_relationship('SecurityGroups');
            $bean->load_relationship('securitygroups_bugs');
            var_dump($bean->securitygroups_bugs);
            //var_dump($bean->securitygroups_bugs);
            die();

            $bean->SecurityGroups->delete('3248ee1e-2841-5c8d-6cdd-6446d722fdf9');
            $bean->save();
        }
    }
}

image

and load_relationship(‘SecurityGroups’) return a object:

<?php

if (!defined('sugarEntry') || !sugarEntry) {
    die('Not A Valid Entry Point');
}

class RemoveClass
{

    function removeSecurityGroup($bean, $event, $arguments)
    {


        if ($bean->fetched_row['status'] == 'Assigned' && $bean->status == 'New') {
            $bean->load_relationship('SecurityGroups');
            $bean->load_relationship('securitygroups_bugs');
            //var_dump($bean->securitygroups_bugs);
            var_dump($bean->SecurityGroups);
            die();

            $bean->SecurityGroups->delete('3248ee1e-2841-5c8d-6cdd-6446d722fdf9');
            $bean->save();
        }
    }
}

image

Well, I believe it was a logical error in the hooks I was using. I solved it as follows:

I created a before_relationship_add that simply deleted all the previous relationships it had:

<?php

if (!defined('sugarEntry') || !sugarEntry) {
    die('Not A Valid Entry Point');
}

class RemoveClass
{

    function removeSecurityGroup($bean, $event, $arguments)
    {

            $rel = 'SecurityGroups';
            $bean->load_relationship($rel);
            $bean->$rel->delete('$bean->id');
            $bean->SecurityGroups->delete('$bean->id');
            $bean->save();
        }
    }

https://community.suitecrm.com/t/have-suitecrm-remove-the-existing-security-group-from-record/60701

Thank everyone

1 Like