1

I have created a custom object that holds a record of the user and a Map (string to integer) of closed case types to number closed. My controller has a list of this custom object and I need to generate a table that will display the name of the user, what they closed, and how many they closed. It should look something like

Username    Case Type    Num Closed
User1       Type1        8
User1       Type2        5
User2       Type1        9
User2       Type3        1
User2       Type1        10

While I don't understand how to do it in VF this is like the sudo code version of it:

for(TeamMember tm : team){
    table.col1Val = tm.Name;
    for(Map<String, Integer> rec : caseTypeToNumClosedMap){
        table.col2Val = rec.Key();
        table.col3Val = rec.Value();
    }
}

I don't know how this would work exactly especially since this table will need to export nicely to excel so I'm not sure if dataTables inside of dataTables would be an option (if that's even possible).

3
  • 1
    you can create wrapper class, populate it in apex. Then display your wrapper list in VF wrapper Commented Jul 5, 2016 at 15:56
  • If you are trying to count number of cases closed grouped by User and Case Type, you should use aggregate function- developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
    – C0DEPirate
    Commented Jul 5, 2016 at 16:00
  • @IlyaLepesh I was hoping to avoid that since this will kind of be a wrapper for a wrapper nobody likes having to go through two wrappers to get to their candy
    – Griffin
    Commented Jul 5, 2016 at 16:46

1 Answer 1

1

I hope you have some custom field created on Case object to store who closed the case. If yes, Please check the code below. else I will also send you screen shot of the workflow rule which updates a custom field with logged-in user's firstname and lastname when case is closed.

<!--- Class Code --->
/**
    @ Author   : Sourabh Yerawar
    @ Purpose : 1] Controls the page --> ClosedCases
                2] Fetches list of closed cases along with details.
*/

public with sharing class ClosedCasesController {

    public List<AggregateResult> listOfClosedCases { get; set; }

    public ClosedCasesController() {

        initializePage();
    }

    private void initializePage() {

        listOfClosedCases = getClosedCases();
    }

    private List<AggregateResult> getClosedCases() {

        List<AggregateResult> listOfClosedCases = new List<AggregateResult>();
        try {
            listOfClosedCases = [ SELECT Case_Closed_By__c, Type, COUNT(Id) numberOfClosedCases
                                  FROM Case 
                                  WHERE isClosed = TRUE 
                                  Group BY Case_Closed_By__c, Type
                                ];
        }
        catch( Exception e ) {
            displayMessageFromException( e );
        }
        return listOfClosedCases;
    }

    /*
        @ Purpose     : Fetches and displays an exception message.
        @ Parameter   : Exception.
        @ Return Type : String [ Details of the exception ].
    */
    public static String displayMessageFromException( Exception e ) {

        String strErrorMessage = '\nError Message:-{0}\nError LineNumber:-{1}';
        List<String> listOfMessageTokens = new List<String>{ e.getMessage(), String.valueOf( e.getLineNumber() ) };

        strErrorMessage = String.format(strErrorMessage, listOfMessageTokens);
        system.debug(strErrorMessage);
        return strErrorMessage;
    }
}


<!-- Page Code --->
<!--
    @ Author    : Sourabh Yerawar
    @ Purpose   : Displays list of closed cases along with details.
    @ Page Name : ClosedCases
-->

<apex:page controller="ClosedCasesController" showHeader="False" sidebar="False" standardStylesheets="True">

    <apex:form >

        <apex:pageBlock title="Closed Cases">

            <apex:pageBlockTable value="{! listOfClosedCases }" var="recordCase">

                <apex:column >  
                    <apex:outputText value="{! recordCase[ 'Case_Closed_By__c' ] }"/>        
                    <apex:facet name="header">Case Closed By </apex:facet>
                </apex:column>

                <apex:column >  
                    <apex:outputText value="{! recordCase[ 'Type' ] }"/>        
                    <apex:facet name="header">Case Type </apex:facet>
                </apex:column>

                <apex:column >  
                    <apex:outputText value="{! recordCase[ 'numberOfClosedCases' ] }"/>        
                    <apex:facet name="header">Number Of Closed Cases </apex:facet>
                </apex:column>

            </apex:pageBlockTable>

        </apex:pageBlock>    

    </apex:form>

</apex:page>

enter image description here

3
  • I have created a workflow rule on case object whenever record is created n updated to meet the criteria. Criteria --> IsClosed = TRUE
    – Soury
    Commented Jul 6, 2016 at 9:56
  • Data type of the custom field --> "Case Closed By" is Text ( 255 ).
    – Soury
    Commented Jul 6, 2016 at 10:08
  • We have both of those cases and that's a good use of the Group By. While I think I have it working a different way you seem incredibly thorough so I won't challenge it.
    – Griffin
    Commented Jul 6, 2016 at 12:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .