0

初めての 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;
}
}
4

1 に答える 1

2

まず、親オブジェクトへの参照がありません。この 2 つの方法で解決できます。

解決策 1:

URL ボタンを使用して、次のような URL として親の ID を渡します。

/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 );
}
于 2012-05-24T13:25:58.113 に答える