Count number of cases on contact

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hrdy90
    Junior Member
    • Apr 2021
    • 28

    Count number of cases on contact

    I'm creating a contact when a case is created (email-to-case). When looking at the contact-card I can see a list of which Cases that user has sent in.

    I would like to have an entity that shows a count on how many cases there is and display it in the Contact list layout. Is this possible?
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    #2
    Yeah possible.
    You can create a field (int) in the contact entity and you can simply use formula script to count related case on the contact entity, e.g:
    PHP Code:
    // Assuming that the new field which hold the count is called caseCount
    caseCount = entity\countRelated('cases'); 
    
    Remember that this is a before save script which mean if a new case is created and been related to a contact then you should have a reversed script on the case to update the caseCount on the contact entity, somthing like below, formula on the Case entity:
    PHP Code:
    ifThen(entity\isNew() && contactId,
    $caseCount = record\attribute('Contact', contactId, 'caseCount');
    record\update('Contact', contactId, 'countCase', $caseCount + 1);
    );
    Rabii
    Web Dev

    Comment


    • hrdy90
      hrdy90 commented
      Editing a comment
      Fantastic. Worked like a charm! I forgot to set "Default" or "Minimum value" for the integer field though but that will solve itself over time. Now the first incoming case for already created contact will change from "Null" to 0 and go up from there. But no biggie :'D

      Thank you!
  • VladZP
    Junior Member
    • Mar 2023
    • 6

    #3
    PHP Code:
    record\update('Contact', contactId, 'countCase', $caseCount + 1); 
    
    All clear, but where do you have 'countCase'? New field name called caseCount. In documentation I saw Attribute1, Value1 and tried something like Contact.caseCount, 'caseCount', caseCount, but it doesn't work

    Comment


    • rabii
      rabii commented
      Editing a comment
      you should create a new field on the contact entity using the entity manager and you can call it caseCount or whatever name you like.
Working...