Announcement

Collapse
No announcement yet.

Count Accounts Assigned To User

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Count Accounts Assigned To User

    Hi Everyone,

    I've added a field called "Goal" in the user entity. Now I want to count all accounts assigned to that user account via formula. I don't see a relationship in the relationship field so hoping someone can assist on the formula to accomplish this. Thank you

  • #2
    Hi, try this:

    Code:
    goal = entity\countRelated('accounts');
    Eymen Elkum | Eblasoft Ltd.
    Espocrm extension & development

    Comment


    • #3
      Thank you eymen-elkum however it returns null. I believe the accounts relationship name you mentioned is being used int he portalusers as you can see in this screenshot. If I knew the relationship name I think it would work tho.

      Comment


      • #4
        I suggest you try with another relationship first and see if you can get it to work before trying Portalusers.

        Comment


        • #5
          There is no relationship between user and account (assigned user is a link added to all entities but doesn't define a reverse relationship). i think best way is to use a hook on account to check if the assigneduser has changed then count the accounts assigned to the user. formula won't work because it is a before script save also you will need an extra formula function (record\findMany) to find all accounts and compare their assigned user to get the count for the current user.

          Comment


          • #6
            hey dodom2

            I found a way to do it without changing anything, i assume this should be triggered everytime the assigned user of an account is changed, therefore you could use the formula below and it will do the job, i have tested this and works fine:

            PHP Code:
            ifThenElse(
              
            entity\isAttributeChanged('assignedUserId') && assignedUserId,
              (
                
            $count record\count('Account''assignedUserId='assignedUserId);
                
            record\update('User'assignedUserId'goal'$count);
              ),
              
            ifThen(
                
            entity\isAttributeChanged('assignedUserId') && !assignedUserId,
                (
                    
            $count record\count('Account''assignedUserId='entity\attributeFetched('assignedUserId'));
                    
            record\update('User'entity\attributeFetched('assignedUserId'), 'goal'$count);
                )
              )
            );
            ‚Äč 
            Hope this helps

            Comment

            Working...
            X