获得记录状态计数的更有效方法

时间:2013-03-06 20:59:47

标签: salesforce apex-code visualforce

我有一个使用standardSetController进行分页的visualforce页面。在页面上,我还添加了按状态过滤...即。现在,我正在为每个状态值执行count()以在页面上显示状态计数,但这需要查询每个状态,这是低效的。我希望以更有效的方式获得状态计数。

这是我的控制器:

public with sharing class myController {

public ApexPages.StandardSetController setCtrl{get; set;}
public String projId { get; set; }
public String clientName { get; set; }
private List<Ticket__c> TicketList = new List<Ticket__c>();

public myController()
{
    projId = ApexPages.currentPage().getParameters().get('id');
    clientName = [Select Client__r.Name From Project__c Where Id =: projId].Client__r.Name;
    setCtrl = new ApexPages.StandardSetController(Database.getQueryLocator([select Ticket_Number__c, Name, Status__c, Description__c, Description_of_Resolution__c, CreatedDate from Ticket__c Where Project__c =: projId ORDER BY CreatedDate DESC]));
    ticketList = (List<Ticket__c>)setCtrl.getRecords();
    setCtrl.setPageSize(5);
}

public Integer getNewStatusCount() {
    return [select count() from Ticket__c Where Project__c =: projId and status__c = 'New'];
}

public Integer getWorkingStatusCount() {
    return [select count() from Ticket__c Where Project__c =: projId and status__c = 'Working'];
}

public Integer getResolvedStatusCount() {
    return [select count() from Ticket__c Where Project__c =: projId and status__c = 'Resolved'];
}

public Integer getClosedStatusCount() {
    return [select count() from Ticket__c Where Project__c =: projId and status__c = 'Closed'];
}

public Integer getCancelledStatusCount() {
    return [select count() from Ticket__c Where Project__c =: projId and status__c = 'Cancelled'];
}

public Integer getReopenedStatusCount() {
    return [select count() from Ticket__c Where Project__c =: projId and status__c = 'Reopened'];
}

public Boolean hasNext {
    get {
        return setCtrl.getHasNext();
    }
}

public Boolean hasPrevious {
    get {
        return setCtrl.getHasPrevious();
    }
    set;
}

public Integer pageNumber {
    get {
        return setCtrl.getPageNumber();
    }
    set;
}

public Integer totalPages {
    get {
        totalPages = math.ceil((Double)setCtrl.getResultSize() / setCtrl.getPageSize()).intValue();
        return totalPages;
    }
    set;
}

public void first() {
    setCtrl.first();
}

public void last() {
    setCtrl.last();
}

public void previous() {
    setCtrl.previous();
}

public void next() {
    setCtrl.next();
}


public List<Ticket__c> getTickets()
{
    return (List<Ticket__c>)setCtrl.getRecords();
}

public void filterStatus() {
    string myParam = apexpages.currentpage().getparameters().get('myParam');
    setCtrl = new ApexPages.StandardSetController(Database.getQueryLocator([select Ticket_Number__c, Name, Status__c, Description__c, Description_of_Resolution__c, CreatedDate from Ticket__c Where Project__c =: projId AND Status__c =: myParam ORDER BY CreatedDate DESC]));
    setCtrl.setPageSize(5);
}
}

这是VF代码:

            

                <div class="contact-form">


                    <apex:outputPanel id="contactForm">
                        <apex:form >
                            <ul class="filteredView">
                                <li>
                                    <apex:outputLink value="/tickets?id={!projId}">View All</apex:outputLink>
                                </li>
                                <li>
                                    ({!NewStatusCount})
                                    <apex:commandLink action="{!filterStatus}" value=" New" rerender="contactForm">
                                        <apex:param name="myParam" value="New"/>
                                    </apex:commandLink>
                                </li>
                                <li>
                                    ({!WorkingStatusCount})
                                    <apex:commandLink action="{!filterStatus}" value=" Working" rerender="contactForm">
                                        <apex:param name="myParam" value="Working"/>
                                    </apex:commandLink>
                                </li>
                                <li>
                                    ({!ResolvedStatusCount})
                                    <apex:commandLink action="{!filterStatus}" value=" Resolved" rerender="contactForm">
                                        <apex:param name="myParam" value="Resolved"/>
                                    </apex:commandLink>
                                </li>
                                <li>
                                    ({!ClosedStatusCount})
                                    <apex:commandLink action="{!filterStatus}" value=" Closed" rerender="contactForm">
                                        <apex:param name="myParam" value="Closed"/>
                                    </apex:commandLink>
                                </li>
                                <li>
                                    ({!CancelledStatusCount})
                                    <apex:commandLink action="{!filterStatus}" value=" Cancelled" rerender="contactForm">
                                        <apex:param name="myParam" value="Cancelled"/>
                                    </apex:commandLink>
                                </li>
                                <li id="last">
                                    ({!ReopenedStatusCount})
                                    <apex:commandLink action="{!filterStatus}" value=" Reopened" rerender="contactForm">
                                        <apex:param name="myParam" value="Reopened"/>
                                    </apex:commandLink>
                                </li>
                            </ul>
                            <h5>
                                TICKETS
                            </h5> 
                            <table width="95%" border="1" cellpadding="5">
                                <tr align="left">
                                    <th style="width:25px;">Ticket#</th>
                                    <th style="width:200px;">Subject</th>
                                    <th>Description</th>
                                    <th style="width:50px;">Status</th>
                                    <th style="width:75px;"></th>
                                </tr>
                                <apex:repeat value="{!tickets}" var="ticket">
                                    <tr>
                                        <td>{!ticket.Ticket_Number__c}</td>
                                        <td>{!ticket.Name}</td>
                                        <td>{!ticket.Description__c}</td>
                                        <td>{!ticket.Status__c}</td>
                                        <td><apex:outputLink value="/detail?id={!projId}&ticketId={!ticket.Id}">View Details</apex:outputLink></td>
                                    </tr>
                                </apex:repeat>
                            </table>
                            <apex:outputPanel rendered="{!(tickets.empty == false)}" styleClass="pagination" >
                                <ul>
                                    <li><apex:commandLink id="first" value="First" action="{!first}" rendered="{!hasPrevious}" /></li>
                                    <li><apex:commandLink id="prev" value="Prev" action="{!previous}" rendered="{!hasPrevious}" /></li>
                                    <li><apex:commandLink id="next" value="Next" action="{!next}" rendered="{!hasNext}" /></li>
                                    <li><apex:commandLink id="last" value="Last" action="{!last}" rendered="{!hasNext}" /></li>
                                    <li><apex:outputText value="Page {!pageNumber} of {!totalPages}" /></li>
                                </ul>
                            </apex:outputPanel>
                        </apex:form>
                    </apex:outputPanel>                     
                </div>
        </div>

感谢。

1 个答案:

答案 0 :(得分:0)

您可以在单个SOQL查询中获取所有状态的每个状态的计数

[select status__c, count(id) from Ticket__c Where Project__c =: projId group by status__c]

然后,您可以通过现有的getter公开结果,公开查询结果并通过转发器将它们绑定到UI。