2012-05-25 25 views
4

该公司最近需要在Visualforce/Apex中创建并排记录比较。我们经常需要将引导合并到联系人中。以前这是在S-Controls中处理的;然而,最近的举措和希望在未来支持我们的代码促使我们将许多S-Controls转移到Visualforce页面和Apex代码中。如何在VisualForce中创建并排记录比较

我们希望能达到这样的事: enter image description here

我有所尝试(有一点运气)这样使用apex:pageBlockTable标签;然而,我不确定如何在需要一组SObject时获得两组数据。

我必须使用的所有先前的代码都是在使用JavaScript的S-Controls中完成的;虽然此代码现在可以工作 - 我们需要将其移植到VisualForce页面。很明显,我可以使用HTML表格...手动编写此代码,但我相信这会破坏使用股票Salesforce功能的目的。我非常乐意接受其他方法 - 正如我所概述的那样,但需要使用几乎令人痛苦的编码才能使其可行(尤其是在将来更新/删除/添加字段时)。

回答

9

答案最终非常直截了当!

首先 - 事实证明,所述apex:pageBlockTable可以处理几乎任何种类传递到value参数对象的,无论是SObjects的阵列或MyFooBar对象的数组。

- 我们需要一个包装类,同时封装两个记录:

public with sharing class LeadContactCompareWrapper { 
    public static final String SALUTATION = 'Salutation'; 
    public static final String FIRST_NAME = 'First Name'; 
    public static final String LAST_NAME = 'Last Name'; 
    public static final String EMAIL = 'Email'; 
    public static final String PHONE = 'Phone'; 
    public static final String STREET = 'Street'; 
    public static final String CITY = 'City'; 
    public static final String STATE = 'State'; 
    public static final String COUNTRY = 'Country'; 
    public static final String ZIP_POSTAL = 'Zip/Postal Code'; 
    public static final String TITLE = 'Title'; 
    public static final String PRIMARY_FUNCTIONAL_ROLE = 'Primary Functional Role'; 
    public static final String SECONDARY_FUNCTIONAL_ROLE = 'Secondary Functional Role'; 
    public static final String BULLETIN = 'Bulletin'; 
    public static final String CREDIT_MEMO = 'Credit Memo'; 
    public static final String FS_INSIGHTS = 'FS Insights'; 
    public static final String MANUFAC_IND_INSIGHTS = 'Manufact. Ind Insights'; 
    public static final String LIT_AND_FRAUD = 'Lit. & Fraud News'; 
    public static final String REGULATORY_INSIGHTS = 'Regulatory Insights'; 

    private Lead lead; 
    private Contact contact; 

    public List<Compare> information { get; set; } 
    public List<Compare> marketing { get; set; } 
    public List<Compare> furtherDetails { get; set; } 

    public List<SelectOption> names { get;set; } 
    public String newName { get;set; } 

    public Id getContactId() { 
     return this.contact.Id; 
    } 
    public Id getAccountId() { 
     return this.contact.Account.Id; 
    } 
    public Id getLeadId() { 
     return this.lead.Id; 
    } 

    public Lead getLead() { 
     return this.lead; 
    } 

    public Contact getContact() { 
     return this.contact; 
    } 

    public LeadContactCompareWrapper(Lead lead, Contact contact) { 
     this.lead = [Select Id, DeliveryPreference__c, ACE__c,AML__c,BusContinuity__c,CorpGovSOX__c,ERM__c,FinancialRisk__c,InternalAudit__c,ITAsset__c,ITAudit__c,ITSecurity__c,LitSupport__c,ORM__c,SelfAssessment__c,SpendRisk__c, Owner.Name, Company, Bulletin__c,Credit_Memo__c,FSInsights__c,Manufact_Ind_Insights__c,LitFraudNews__c,RegulatoryInsights__c, LastModifiedDate, Salutation, FirstName, LastName, Email, Phone, Street, City, State, Country, PostalCode, Title, Primary_Functional_Role__c, SecondaryFunctionalRole__c From Lead Where Id = :lead.Id]; 
     this.contact = [Select Id, Owner.Name, Account.Id, Account.Name, Bulletin__c,Credit_Memo__c,FSInsights__c,Manufact_Ind_Insights__c,LitFraudNews__c,RegulatoryInsights__c, LastModifiedDate, Salutation, FirstName, LastName, Email, Phone, MailingStreet, MailingCity, MailingState, MailingCountry, MailingPostalCode, Title, Primary_Functional_Role__c, SecondaryFunctionalRole__c From Contact Where Id = :contact.Id]; 

     this.init(); 
    } 

    private void init() { 
     this.information = new List<Compare>(); 
     this.marketing = new List<Compare>(); 
     this.furtherDetails = new List<Compare>(); 

     // this part will suck but it has to be done 
     information.add(this.createCompare(SALUTATION, 
         (this.lead.Salutation != null) ? this.lead.Salutation : '', 
         (this.contact.Salutation != null) ? this.contact.Salutation : '' 
         )); 
     /* Continue adding as many compare fields for the 'information' section as needed... */ 
     // Marking Subscriptions 
     marketing.add(this.createCompare(BULLETIN, 
         (this.lead.Bulletin__c != null) ? this.lead.Bulletin__c : '', 
         (this.contact.Bulletin__c != null) ? this.contact.Bulletin__c : '' 
         )); 
     /* Continue adding as many compare fields for the 'marketing' section as needed... */      

     // Further information - just for display purposes 
     furtherDetails.add(this.createCompare('Owner', 
         (this.lead.Owner.Name != null) ? this.lead.Owner.Name : '', 
         (this.contact.Owner.Name != null) ? this.contact.Owner.Name : '', 
         false, 
         true 
         )); 
     /* Continue adding as many compare fields for the 'further information' section as needed... */      
    } 

    /* 
    * Creates a comparison object 
    */ 
    private Compare createCompare(string label, String val1, String val2, Boolean isVal1, Boolean isVal2) { 
     Compare c = new Compare(label); 
     c.selectVal1 = isVal1; 
     c.selectVal2 = isVal2; 
     c.val1 = val1; 
     c.val2 = val2; 

     return c; 
    } 

    /* 
    * Defaults our comparison to value 1 as selected 
    */ 
    private Compare createCompare(String label, String val1, String val2) { 
     return createCompare(label, val1, val2, true, false); 
    } 
} 

- 我们需要创建一个拥有基于两个值和两个布尔一个“比较”类在其值被选择(和行标签以显示在表):

public class Compare { 
    public Compare (String label) { 
    this.label = label; 
    } 

    public String label { get; set; } 
    public Boolean selectVal1 { get; set; } 
    public Boolean selectVal2 { get; set; } 
    public String val1 { get; set; } 
    public String val2 { get; set; } 
} 

然后,它是把它全部VF页上具有以下那样简单:

<apex:pageblocktable value="{!leadToContact.information}" var="compare"> 
    <apex:column> 
     <apex:facet name="header">Information</apex:facet> 
     {!compare.label} 
    </apex:column> 
    <apex:column> 
     <apex:facet name="header">Lead</apex:facet> 
     <apex:inputcheckbox id="val1" label="{!compare.val1}" onclick="uncheckOtherCompare(this);" title="{!compare.val1}" value="{!compare.selectVal1}" /> 
     <apex:outputlabel value="{!compare.val1}" /> 
    </apex:column> 
    <apex:column> 
     <apex:facet name="header">Contact</apex:facet> 
     <apex:inputcheckbox id="val2" label="{!compare.val2}" onclick="uncheckOtherCompare(this);" value="{!compare.selectVal2}" /> 
     <apex:outputlabel value="{!compare.val2}" /> 
    </apex:column> 
</apex:pageblocktable> 

最后,我们只需要一点点的JavaScript,使单选按钮举止得体:)

function uncheckOtherCompare(obj) { 
    // Get the id of the object being checked 
    var objId = obj.id; 

    if (objId.indexOf('val1') >= 0) { 
     objId = objId.replace('val1', 'val2'); 
    } else { 
     objId = objId.replace('val2', 'val1'); 
    } 

    if (obj.checked) { 
     document.getElementById(objId).checked = false; 
    } else if (!document.getElementById(objId).checked) { 
     // If the user is trying to uncheck both boxes, recheck the box that is being passed in. 
     // We can't have 'no' boxes checked on a given row 
     obj.checked = true; 
    } 
} 

,JavaScript会放置在页面上几乎任何地方,但最好要么挂靠或将其放置在开始标签后面的文档顶部。

完成此操作后,您可以(从代码中)访问您的LeadContactCompareWrapper.[information|marking|furtherDetails]数组,并逐步遍历每个数组以确定选定的值,或编写其他帮助程序类以加快此过程。

有了这个功能,我们可以创建并排记录比较,让我们能够将我们的线索直接合并到我们的联系人中!

+2

感谢您的详细追踪,有人将这种努力放在跟踪他们自己的问题上!如果您尚未加入51区SFDC提案,那么如果您想要:http://area51.stackexchange,那就太好了。com/suggestions/37589/salesforce – jkraybill

+1

@jkraybill意思是说,我确实加入:) –