Workflow

Hello forum,
I want to know how you can automatically calculate and record how many contacts an account has within the accounts module. Assuming I already have a custom field to receive the calculation.

Wouldn’t a Report showing the counts be enough?

Or simply looking at the count at the top of the Contacts subpanel in the Accounts list view?

Or the count at the top of the Contacts Listview, after you filter it by account?

If not, if you really want a process to count everything, when do you want to run it? Every day, every minute, every time an account is saved?

Hi dad, again thanks for the feedback.
I need the count in the module, because in addition, I’ll make another calculation happen.
I need every contact saved, + 1 in the related account.
It’s possible?

Look here, son. :lol:

You can check this out to learn how to override a Detail View and make a calculation there:

https://suitecrm.com/suitecrm/forum/suitecrm-7-0-discussion/15154-is-there-logic-hook-for-detailsview#50990

For your case, I would add a direct SQL Query to run a SELECT COUNT to simply make the calculation when you need to show it.

The other possible approach, making the counts when you save records, can be triggered from after_save logic hooks. But I’m not sure how you would do the updates, without making it very slow (like having to go over every account or every contact). But this depends on the structure of your data, you have a better knowledge of that than me.

With these terms I’ve given you I think you can find lots of stuff in Google doing similar things.

Thanks for the support.
I’ll try to apply here and the best tested option I’ll post here

@pgr,

I have tried using SQL query with SELECT COUNT, the problem is that I can not calculate only contacts related to the account.

I also tried to read only in detail.view.php, but obviously I can not use it to complement other calculations because the result is off the field.

You would have some suggestions to perform this calculation in the simplest way possible and without causing great queries that cause slowness.

Thanks

See if this helps:

https://suitecrm.com/suitecrm/forum/developer-help/18236-calculate-and-display-total-lead-scores

it is very similar to what you’re doing.

Another approach would be to start from the current bean, use get_linked_beans to get the related records, and loop through them in PHP to sum. No direct SQL (although of course get_linked_beans will be doing an SQL query for you, so this won’t be more efficient, just cleaner in terms of code layers).

@pgr, I do not want to abuse your goodwill, but I’m not having good ideas with get_linked_beans.
Would you kindly show me an example of use in this case?

Sorry I don’t have examples, and I don’t have time to go into each individual thread in such depth.

Please see the Documentation about that function

https://docs.suitecrm.com/developer/working-with-beans/

and Google for examples, you will find a few. Search also for “sugarcrm get_linked_beans” there are valid examples from SugarCRM.

Doing some research, I even understand how the query works with get_linked_beans, I do not know if I’m using the counter correctly or if there’s something more wrong. I’m still using custom / modules / view / view.detail.php as follows:

    function display() {
        $account = new Account(); 
        $account->retrieve($_REQUEST['record']); 
        $contacts = $account->get_linked_beans('contacts','Contact'); 
        foreach ( $contacts as $contact ) { 
            $total += $contact->total_funcionarios_c;  //add the value of each sale to the variable
        }
        echo "
        <script>
        var total = '$total';
        $(document).ready(function(){
        $('#total_funcionarios_c').after(total); });
        </script>";

        parent::display();
    }

}

but the result is always 0.

You are inside the Accounts view, so you can access the bean using $this->bean , no need to fetch Request parameters.

Perhaps try initializing the $total variable to zero before using it?

I also think you don’t need the javascript injection, you can simply put it in a field with “assign” before displaying.

Okay, I still can not make it work, I’ll keep trying.

Thanks

Hi @pgr,
I came to share my solution.
You were right the first time, you did not have to call the bean, but that did not determine the error. My problem is that in doing the sum of “total” for contacts, I requested the field “total_funcionarios_c”, and the field was zeroed, that is, it always added 0 + 0. I just removed this and kept “total + = $contact” and voila, worked fine.

1 Like

Ok, thanks for sharing, but I don’t understand how “total + = $contact” can work. You are adding the contact bean to an integer? Maybe it just adds one, for some reason, and it looks like it works.

But if that is the case, you don’t need a foreach loop, you can just say

total = count($contacts);

Sorry,
In fact, I include a value of “1” as a counter and not “total += $contact” as I said.
Thanks