function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NetmasterNetmaster 

Passing parent id to child record in VisualForce page

I'm attempting to create my first VF page. It is a line-item entry form that will allow a user to enter multiple child records (Enfants_c) before clicking save. Right now, I'm opening the VF page from a custom button on the parent (Assure_c). However, when the page opens, the lookup field for the parent is not populated - so the user has to click the lookup to select the parent from Assure__c. Is there a way to pass the parent id of the previous page to the new child records on the 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>
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;
    }
}

 

 

 

SRKSRK

U r Redirect it to standard Page

 

U need to look into the sorce code of standard page & need to figureout the ID of Slaesfroce Standard Field  & the use it in below Ref Code
Or u can user firebox to find the ID of the field

 

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

 

//Add Code

         acctpage.getParameters().put('<Salesfroce_ID_of_Field>' , '<Name of the Parent Record>');

         acctpage.getParameters().put('<Salesfroce_ID_of_Field>' , '<ID_of Parent Record>');

//End

  

        acctPage.setRedirect(true);
        return acctPage;
    }

 

Note:- when passing ID of Parent Reocrd _lkid is extra added with ID of the field.

 

 

don't worry this ID never change (until & unless to deploy this sObject & Standard Pagelyouyt to produsction Org)

NetmasterNetmaster

Thanks for your answer.

 

But my problem is before inserting date in the base, that's means I would insert the parent ID where inserting other fields, and the parent ID is required.

 

Other point is that evry parent has, one or many children, so I can't insert the ID in the apex !

SRKSRK

Correct me if i was wrong

But what i understnd from your code is that

you are crate "Enfants__c" record from your visual force page & while creating u r record you are chossing a exsting "Parent__c" record & u want u redirect to the parent record after saving your record

that is my understanding is it correct ??




dmchengdmcheng

There are two ways to do this:
 

1. Write your class as an extension of Account, not Enfants. Then you get the account record using the getRecord method on the standard controller.

public class insererEnfants {
	private final Account acct;
  
	public insererEnfants(ApexPages.StandardController std) {
		this.acct = (Account)std.getRecord();
	}
}


 2.  If you need to do a custom controller, then your button has to pass the account ID as a parameter to the VF page, and in the class you do CurrentPage().getParameters() to get the ID and retrieve the account.