Count Accounts Assigned To User

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dodom2
    Senior Member
    • Jan 2020
    • 198

    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
  • eymen-elkum
    Active Community Member
    • Nov 2014
    • 472

    #2
    Hi, try this:

    Code:
    goal = entity\countRelated('accounts');
    CEO of Eblasoft
    EspoCRM Expert since 2014
    Full Stack Web Developer since 2008
    Creator of Numerous Successful Extensions & Projects​

    Comment

    • dodom2
      Senior Member
      • Jan 2020
      • 198

      #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

      • esforim
        Active Community Member
        • Jan 2020
        • 2204

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

        Comment

        • rabii
          Active Community Member
          • Jun 2016
          • 1250

          #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.
          Rabii
          Web Dev

          Comment

          • rabii
            Active Community Member
            • Jun 2016
            • 1250

            #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
            Rabii
            Web Dev

            Comment

            Working...