Salesforce Apex 和 Visualforce

Salesforce Apex And Visualforce

我正在研究 visualforce 和 apex,我遇到了一种情况,我想合并 3 个不同的表,其中杂工的名字很常见。此外,勤杂工自定义对象上的专业自定义字段是一个多选列表,因此我无法直接从任何 openorders 或 closeorders 聚合查询中查询它,因为我们无法按专业分组,即多选列表。如果有人能帮我解决这个问题,我会很高兴。

Note: orders custom object has lookup field on handyman.

Visualforce 页面代码

<apex:page standardController="Orders__c" extensions=“HandymanInfo">
  <apex:form>
   <apex:pageBlock>
     <apex:pageBlockSection columns="6" title=“ Handyman Tables">
          <apex:pageBlockTable value=“{!lsthandyman}” var=“h”>
            <apex:column value=“{!h.Name}">
                <apex:facet name="header”>Handyman Name</apex:facet>
        </apex:column>
            <apex:column value=“{!h.Specialities__c}" >
            <apex:facet name="header">Specialities</apex:facet>
            </apex:column> 
            </apex:pageBlockTable>
             
            <apex:pageblocktable value=“{!openorders}" var="oo">  
             <apex:column value="{!oo[’n']}"><apex:facet name="header”>Handyman Name</apex:facet></apex:column> 
             <apex:column value="{!oo[’sumopen']}"><apex:facet name="header”>Total Orders Opened</apex:facet></apex:column>                                                                 
            </apex:pageblocktable>
                
              <apex:pageBlockTable value=“{!closeorders}" var="co">
                  <apex:column value="{!co[’n']}"><apex:facet name="header”>Handyman Name</apex:facet></apex:column> 
                    <apex:column value="{!co[’sumclosed']}"><apex:facet name="header">Total Orders Closed</apex:facet></apex:column> 
            </apex:pageBlockTable>
                  
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

控制器Class代码:

    public class HandymanInfo {
    public List<Handyman__c> lsthandyman {get;set;}
    Public List<AggregateResult> openorders {get; set;}
    Public List<AggregateResult> close orders {get; set;}

    
public HandymanInfo(ApexPages.StandardController controller) {
 
    lsthandyman = [SELECT Name,Specialities__c FROM Handyman__c ORDER BY Name ASC];
   
        openorders = [SELECT Handyman__r.Name n, COUNT(Name) sumopen FROM Orders__c 
        WHERE ((Status__c='New') OR (Status__c ='Assigned’) OR (Status__c='Accepted') OR 
        (Status__c ='In Progress'))
        GROUP BY Handyman__r.Name
        ORDER BY Handyman__r.Name ASC];
       
        closeorders = [SELECT Handyman__r.Name n, COUNT(Name) sumclosed FROM Orders__c 
        WHERE ((Status__c='Completed') OR (Status__c='In Review’) OR (Status__c ='Paid'))
        GROUP BY Handyman__r.Name
        ORDER BY Handyman__r.Name ASC];
    }
}

订单 -> 勤杂工是否在查找关系或主明细中关联?订单可以没有杂工吗?如果杂工将被删除 - 您会希望级联删除订单还是希望保留“孤儿”?

如果您将它作为主从,那么您可以制作 2 个从订单到勤杂工的汇总字段。然后你的控制器变得非常简单,SELECT Name, Specialities__c, OrdersCountOpen__c, OrdersCountClosed__c FROM Handyman__c ORDER BY Name,工作完成。


如果它必须保留为查找...您可以创建 2 Map<Id, Integer>,其中键是杂工的 ID,值是计数。或者甚至用子查询作弊以获取未结订单,然后 运行 另一个查询已关闭。

// make this your class variable
public Map<Id, Integer> closedOrders {get; private set;}

// and then
lsthandyman = [SELECT Name, Specialities__c,
        (SELECT Id FROM Orders__r WHERE Status__c IN ('New', 'Assigned', 'Accepted', 'In Progress'))
    FROM Handyman__c 
    ORDER BY Name ASC];
closedOrders = new Map<Id, Integer>();
// there's no guarantee all handymen have any closed orders and we don't want to display error if map key isn't found. So let's make sure we have something in there even if it's all zeroes
for(Handyman__c h : lsthandyman){
    closedOrders.put(h.Id, 0);
}
// and now let's get some actual closed order numbers
for(AggregateResult ar : [SELECT Handyman__c h, COUNT(Id) cnt
    FROM Orders__c 
    WHERE Status__c IN ('Completed','In Review’,'Paid')
    GROUP BY Handyman__c]){
    closedOrders.put((Id) ar.get('h'), Integer.valueOf(ar.get('cnt')));
}

然后您的 Visualforce 将类似于

<apex:pageBlockTable value=“{!lsthandyman}” var=“h”>
    <apex:column header="Handyman Name" value=“{!h.Name}"/>
    <apex:column header="Specialities" value=“{!h.Specialities__c}"/>
    <apex:column header="# Open" value="{!h.Orders__r.size}" />
    <apex:column header="# Closed" value="{!closedOrders[h.Id]}" />
</apex:pageBlockTable>