after_relationship_delete logic hook deleting all records.

I have created following code to delete record from related module,when user click on a remove button, from subpanel.
I am facing a problem that when i remove a single record, it remove all the records from related module, instead of that single record.
i am using "after_relationship_delete" logic hook.

        try {
            if (
                    isset($bean->rel_fields_before_value["cloud_lidmaatschap_con_contacten_1cloud_lidmaatschap_ida"]) &&
                    !empty($bean->rel_fields_before_value["cloud_lidmaatschap_con_contacten_1cloud_lidmaatschap_ida"])
            ) {
                $contactpersonen_id = $bean->rel_fields_before_value["cloud_lidmaatschap_con_contacten_1cloud_lidmaatschap_ida"];
                $contactpersonen = new cloud_Lidmaatschap();
                $contactpersonen = $contactpersonen->retrieve($contactpersonen_id);
                $contactpersonen->load_relationship('cloud_lidmaatschap_con_contacten_1');
                $contactpersonen->cloud_lidmaatschap_con_contacten_1->delete($bean->id);
            }
        }
        catch (Exception $ex) {
            $GLOBALS['log']->fatal($ex->getMessage());
        }
  • My understanding of your scenario is that you have two modules: Contacts and cloud_Lidmaatschap. When you unlink a cloud_Lidmaatschap from a Contact you also want to delete the corresponding cloud_Lidmaatschap record.

    In other words you don't want orphan cloud_Lidmaatschap left behind.

     

    I am assuming you are in version 7.x or higher

    First, instead of using "new ModuleName()" and "retrieve", you should be using the Bean Factory.

    See:

    http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_8.0/Data_Framework/Models/BeanFactory/in… 

    Having said that, I think you are overcomplicating the retrieval of the bean to delete. You have all the information you need in the arguments of your logic hook.

    Let me see if I can explain.

    The after_relationship_delete logic hook executes on both sides of the relationship. So your logic hook can exist on the contactpersonen (Contact) side or the cloud_Lidmaatschap side. Which side you choose to put it on is your choice.

    Let's say you have your logic hook on the Contact side, then the $bean is the Contact bean.

    The after relationship delete logic hook on the Contact executes any time a relationship to a Contact is deleted regardless of WHAT that related record is. So, for example, if you have a Note related to the Contact and you unlink that Note (delete the relationship between Contact and Note), the hook WILL execute.

    So the first thing you need to check is WHAT related record you just unlinked, in other words what relationship was just removed.

    In your Contact logic hook:

    $arguments['related_module'] //is the module of the record related to the Contact for which you just removed the relationship
    $arguments['related_id'] //is the id of that related record.
    $arguments['module'] //is your Contact module
    $arguments['id'] //is the id of your Contact record

    So first, check that the relationship you just unlinked was that with a related cloud_Lidmaatschap and not something else.

     

    //check that we just unlinked a cloud memebership
    if($arguments['related_module']  == 'cloud_Lidmaatschap'){
       //retrieve the bean for that cloud record
       //the retreive returns null if no bean with that id is found
        $cloud_bean = BeanFactory::retrieveBean($arguments['related_module'], $arguments['related_id']);
        if(!empty($cloud_bean)){
          //mark the cloud bean as deleted = 1
          $cloud_bean->mark_deleted($arguments['related_id']);
        }
    }

     

    Hope this helps,
    Francesca