【问题标题】:Passing parent id to child record in VisualForce page在 VisualForce 页面中将父 ID 传递给子记录
【发布时间】:2012-05-24 11:05:22
【问题描述】:

我正在尝试创建我的第一个 VF 页面。这是一个行项目输入表单,允许用户在单击保存之前输入多个子记录 (Enfants_c)。现在,我正在从父级 (Assure_c) 上的自定义按钮打开 VF 页面。但是,当页面打开时,父项的查找字段未填充 - 因此用户必须单击查找以从 Assure__c 中选择父项。有没有办法将上一页的父ID传递给VF页面上的新子记录?

//page    

<apex:page standardController="Enfants__c" extensions="insererEnfants" standardStylesheets="true">
<apex:sectionHeader title="Ajouter un enfant"
    subtitle="{!$User.FirstName}" help="/help/doc/user_ed.jsp?loc=help"></apex:sectionHeader>
<apex:form >
    <apex:pageBlock title="Nouveau enfant" id="thePageBlock" mode="edit">
        <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Enregistrer"></apex:commandButton>
            <apex:commandButton action="{!cancel}" value="   Annuler   "></apex:commandButton>
        </apex:pageBlockButtons>

        <apex:pageBlockTable value="{!accts}" var="a" id="table">
            <apex:facet name="footer">
                <apex:commandLink value="Ajouter" action="{!addRow}" rerender="table,error"/>
            </apex:facet>
            <apex:facet name="header">
                <apex:commandLink value="Supprimer" action="{!deleteRow}" rerender="table,error"/>
            </apex:facet>

            <apex:column headerValue="Nom">
                <apex:inputField/> value="{!a.Parent__c}"/>
            </apex:column>
            <apex:column headerValue="Nom">
                <apex:inputField value="{!a.Name}"/>
            </apex:column>
            <apex:column headerValue="Prénom">
                <apex:inputField value="{!a.Prenom__c}"/>
            </apex:column> 
                            <apex:column headerValue="Né le">
                <apex:inputField value="{!a.Date_de_naissance__c}"/>
            </apex:column>   
                            <apex:column headerValue="Lieu de naissance">
                <apex:inputField value="{!a.Lieu_de_naissance__c}"/>
            </apex:column>   
                            <apex:column headerValue="Situation">
                <apex:inputField value="{!a.Situation__c }"/>
            </apex:column>                          
        </apex:pageBlockTable>
        </apex:pageblock>
</apex:form>
</apex:page>


//Controller


public class insererEnfants{


public List<Enfants__c> accts {get; set;}


public insererEnfants(ApexPages.StandardController controller){
    accts = new List<Enfants__c>();
    accts.add(new Enfants__c();

}

public void addrow(){
    accts.add(new Enfants__c());       
}

public PageReference deleteRow(){
   if (accts.size()>1)
   {
      accts.remove(accts.size()-1);
   }
   return null;
}

public PageReference save()
{
    insert accts;
    Assure__c theParent = new Assure__c(id=accts[0].Parent__c);
    PageReference acctPage = new ApexPages.StandardController(theParent).view();
    acctPage.setRedirect(true);
    return acctPage;
}
}

【问题讨论】:

    标签: salesforce apex-code visualforce force.com


    【解决方案1】:

    首先,您没有对 Parent 对象的引用。您可以通过两种方式解决:

    解决方案 1:

    使用 URL 按钮传递父级的 ID,URL 如下所示:

    /apex/EnfantsPage?assureid={!Assure_c.Id}
    

    将“EnfantsPage”替换为您的 VF 页面的名称。然后,在控制器内:

    String assureid = ApexPages.currentPage().getParameters().get('assureid');
    

    现在您知道了父母的 ID。创建新的子记录时,将父id的值设置为您传递的id。

    public insererEnfants(ApexPages.StandardController controller){
      accts = new List<Enfants__c>();
      if (assureid <> null) {
        for (Assure__c assure: [ SELECT Id FROM Assure__c
                                 WHERE Id = :assureid] ) {
        accts.add(new Enfants__c( Parent__c = assure.Id ));
      } else {
        accts.add(new Enfants__c());  
      }
    }
    
    public void addrow(){
      if (assure.Id <> null) {
        accts.add(new Enfants__c( Parent__c = assure.Id ));
      } else {
        accts.add(new Enfants__c());  
      }      
    }
    

    解决方案 2:

    不要扩展子控制器,而是扩展父控制器:

    public Assure__c parent {get;}
    public List<Enfants__c> accts {get; set;}
    public insererEnfants(ApexPages.StandardController stdController) {
      this.parent = (Assure__c)stdcontroller.getRecord();
      accts.add(new Enfants__c( Parent__c = parent.id );
    }
    
    public void addrow(){
      accts.add(new Enfants__c( Parent__c = parent.id );
    }
    

    【讨论】:

    • 感谢您的帮助,但是我的问题是在base中插入ID,我应该修改控制器以插入父ID。
    • 问题是您在创建子记录时没有定义父记录,也没有在其他任何地方设置它。试试这个:accts.add(new Enfants__c(Parent__c=assureid));
    • 我收到了这个错误Error: Compile Error: Variable does not exist: assureid at line 9 column 44,这是我的问题,直到我创建了这个boards.developerforce.com/t5/Visualforce-Development/…,我才找到解决嘴巴的方法,我不能对这个问题做同样的事情
    • 再看一遍,我现在看到您没有检索标准控制器的记录。因为我现在理解了根本问题,所以我会改变我的答案。
    猜你喜欢
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 2014-10-22
    相关资源
    最近更新 更多