Accounts & Cases Relationship πŸ™ƒ

The accounts and cases have in-build relationship between them(account_cases).

In the DB, we have accounts_cases table but it never gets updated.

We want to create before-save logic hook to populate the relationship.

Does anyone know, how to do it? :thinking: :thinking: :thinking:

May be this helps here.

//Load the relationship
$accountBean->load_relationship('cases');
//Can now call methods on the relationship object:
$casesIds = $accountBean->cases->get();
1 Like

Is this looks correct?

<?php
class LogicHook {
    public function beforeSave($bean, $event, $arguments) {
        if ($bean->fetched_row === false) {

            if (!empty($bean->account_id)) {
                $rel_name = 'account_cases'; // <- wrong | please use value from the 'name'=> parameter
                if ($bean->load_relationship($rel_name)) {
                    $bean->$rel_name->add($bean->account_id);
                }
            }
        }
    }
}
?>

Could you please help understand what exactly is the requirement. There is one to many relationship between accounts and cases module. That is one account will have multiple cases associated with it. When we create a case for we select the account to for which the case is being recorded.
We have β€˜cases’ table which as account_id column which links the account with the case. You can query to get the cases for account (account_id)
select account_id,name from cases where account_id = '{account_id}'; Replace {account_id} with actual account id to check the relationship.
It looks β€˜acccounts_cases’ is not used because there is not many-to-many relationship between accounts and cases module.

Are you trying to create a new case record in the database table while saving or updating an account record.

1 Like

Thank a lot for detailed answer! :smiley:

Basically, we created the β€œGenerate a PDF” functionality in the Cases module.

Now, when we add account name, office number, email in the PDF template of Cases module. It is not displaying anything.

So, idea was to create before_save logic hook which will update relationship between case and account and eventually it will display in the downloaded PDF.

Let me know if you need more information!

I think I found the issue.

It was relationship name. For the Accounts & Cases, the relationship name is

accounts

Find at:

SuiteCRM/modules/Cases /vardefs.php

    'accounts' => 
    array (
      'name' => 'accounts',
      'type' => 'link',
      'relationship' => 'account_cases',
      'link_type' => 'one',
      'side' => 'right',
      'source' => 'non-db',
      'vname' => 'LBL_ACCOUNT',
    ),
1 Like

Great to know this. Thanks for sharing. Is the downloaded PDF file showing values for Accounts and Cases records?

Yes, it displays values from the related account module.

1 Like