单击带有条件记录的查找图标时打开 VF 页面

Open VF page when clicked on lookup icon with conditional records

我正在尝试对此 运行 进行一些修改。 http://blog.jeffdouglas.com/2011/08/12/roll-your-own-salesforce-lookup-popup-window/

我必须获取我的联系人具有联系人角色的那些帐户以及它所属的帐户。

我已经编写了 soql 查询并拥有来自两个对象的不同帐户。 我将列表保存为设置以避免重复。

我的第二个 vf 页面显示帐户名称,但是当我 select 帐户名称时,在我的第一个 vf 页面中出现此错误。

错误:值“{!Cas.AccountId}”不是有效的 ID 字段 是的,因为我正在添加值以设置为 set.add(variable.name); 这会显示帐户名..如果我添加 ID,它会在用户无法理解的 vf 页面上显示 ID。

我在这里被打动了,无法前进! 请指导我!!!!!!

提前致谢!!!!!! 我的第二个 vf 页面和 class

    <apex:page controller="Customaccountlookupcontroller" tabStyle="Account" showHeader="false" title="Search" id="pg" sidebar="false">
     <apex:form >
         <apex:outputPanel id="page" layout="block" style="margin:5px;padding:10px;padding-top:2px;">
    <apex:tabPanel switchType="client" selectedTab="name1" id="tabbedPanel">

      <!-- SEARCH TAB -->
      <apex:tab label="Search" name="tab1" id="tabOne">

        <apex:actionRegion >  
          <apex:outputPanel id="top" layout="block" style="margin:5px;padding:10px;padding-top:2px;">
            <apex:outputLabel value="Search" style="font-weight:Bold;padding-right:10px;" for="txtSearch"/>
            <apex:inputText id="txtSearch" value="{!searchString}" />
              <span style="padding-left:5px"><apex:commandButton id="btnGo" value="Go" action="{!Search}" rerender="searchResults"></apex:commandButton></span>
          </apex:outputPanel>

            <apex:outputPanel id="pnlSearchResults" style="margin:10px;height:350px;overflow-Y:auto;" layout="block">
            <apex:pageBlock id="searchResults">
            <apex:pageBlockTable value="{!allaccts}" var="a"> 
            <apex:column >           
            <apex:facet name="header">
            <apex:outputPanel >Account Name</apex:outputPanel>                
            </apex:facet> 
              <apex:outputLink value="javascript:top.window.opener.lookupPick2('{!FormTag}','{!TextBox}_lkid','{!TextBox}','{!a}','{!a})', false)" rendered="{!NOT(ISNULL(a))}">{!a}</apex:outputLink>   
            </apex:column>
            </apex:pageBlockTable>
            </apex:pageBlock>
          </apex:outputPanel>
        </apex:actionRegion>

      </apex:tab>
    </apex:tabPanel>
  </apex:outputPanel>          
     </apex:form>      
</apex:page>

顶点class:

Public with sharing class Customaccountlookupcontroller{

    Public set<string> allaccts{get;set;}
    public string searchString{get;set;}
    Public list<account> results{get;set;}
    Public string selectedkey{get;set;}

    public Customaccountlookupcontroller() {

    allaccts =  new set<string>();

  //Contact objcontact =[select accountid,(select AccountId,Account.name from AccountContactRoles) from Contact where id=:userList[0].contactId];
    Contact objContact =[select AccountId,Account.name,(select AccountId,account.name from AccountContactRoles) from Contact where Id='00317000006VtoR'];

    for(AccountContactRole obj: objContact.AccountContactRoles)
    {    
    allaccts.add(obj.account.name);
    allaccts.add(objContact.account.name);
    }
    System.debug('*******objcontact:'+allaccts);    
    }

    public PageReference search() {

    return null;
     }

   }
    public string getFormTag() {
    return System.currentPageReference().getParameters().get('frm');
    }

    public string getTextBox() {
    return System.currentPageReference().getParameters().get('txt');
    }   
}

由于您没有从查询中得到 Account 记录(这是您的代码和 jeff 的代码之间的主要区别),您需要创建一个帐户包装器对象。该对象将是您控制器的内部 class。

包装器可能如下所示:

public class AccountWrapper {
public String name {get; set;}
public Id accountId {get; set;}

    public AccountWrapper(String name, String accountId) {
        this.name = name;
        this.accountId = accountId;
    }
}

然后您将创建 AccountWrapper 对象以在 VF 页面上的列表中使用

for(AccountContactRole obj: objContact.AccountContactRoles) {   
    allaccts.add(new AccountWrapper( obj.account.name, obj.AccountId ));
    allaccts.add(new AccountWrapper( objContact.account.name, objContact.AccountId ));
}

当然,您需要重新定义要在 VF 页面上使用的列表:

Public set<Customaccountlookupcontroller.AccountWrapper> allaccts{get;set;}

希望这能让您更接近您的目标。我还会做的是简化这个例子,以确保这个包装器 class 首先为您正常工作 - 然后重新构建它。

不要忘记修改您的 VF 页面以容纳包装器 class 成员变量名称。

更新: 如果列表有重复项,您可以通过将 ID 存储在一个集合中并在添加到列表之前检查此集合来跟踪它们,如下所示:

Set<Id> idSet = new Set<Id>();

for(AccountContactRole obj: objContact.AccountContactRoles) {   
    //prevent duplicates. Since we are using two different sObjects, we can't just add to a set to remove duplicates.
    //so we add the account ids to a set (and check this set before adding to our list)
    if (!idSet.contains(obj.AccountId)){
        allaccts.add(new AccountWrapper( obj.account.name, obj.AccountId ));
        idSet.add(obj.AccountId);
    }
    if (!idSet.contains(objContact.AccountId)){
        allaccts.add(new AccountWrapper( objContact.account.name, objContact.AccountId ));
        idSet.add(objContact.AccountId);
    }
}