【问题标题】:Open VF page when clicked on lookup icon with conditional records单击带有条件记录的查找图标时打开 VF 页面
【发布时间】:2015-05-03 17:59:15
【问题描述】:

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

我必须获取我的联系人具有联系人角色的帐户及其所属的帐户。

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

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

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

我被击中了,不能进去!!! 请指导我!!!!!!

先谢谢了!!!!!! 我的第二个 vf 页面和课程

    <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>

顶级类:

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');
    }   
}

【问题讨论】:

    标签: set salesforce lookup apex


    【解决方案1】:

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

    包装器可能如下所示:

    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;}
    

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

    不要忘记修改您的 VF 页面以适应包装类成员变量名称。

    更新:如果列表有重复项,您可以通过将 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);
        }
    }
    

    【讨论】:

    • 好的 CaspNz...但令我惊讶的是,它在我的 VF 页面上显示重复帐户。我正在使用 set。此外,当我使用 {!a.name } 或 {!a} 以外的任何内容时,它只会给我错误。未知属性 'setValue.name' 或 accountid。你能指导我编辑 VF 页面吗......只是为了确保我在正确的道路上!!谢谢!
    • 这是我现在 Vf 页面上的内容:AccountWrapper:[accountId=0011700000jhk4nYAAU, name=test account1] 我需要帮助来解决这个问题以及重复项..即使我使用的是 set,我仍然有重复的accounts.accounts。粗略的并非所有帐户都是重复的,但只有我从这里得到的帐户(来自这行代码; allaccts.add(new AccountWrapper( objContact.account.name, objContact.AccountId )); 显示重复帐户。请指导我!!!提前谢谢!!
    • 我不确定您所说的 setValue 是什么意思 - 您可以在您的问题中发布相关更新的顶点代码吗?
    • 一个集合不允许重复的 sObjects,但它无法知道 AccountWrapper 对象是重复的。在添加到包装类之前,您可能必须扫描重复的 id。
    • 添加了一种在添加到列表之前检查欺骗的方法
    猜你喜欢
    • 1970-01-01
    • 2022-09-24
    • 1970-01-01
    • 2019-02-15
    • 2022-01-20
    • 2013-07-19
    • 2013-03-18
    • 2018-05-18
    • 1970-01-01
    相关资源
    最近更新 更多