+ Start a Discussion

How to insert data into a Related List object with VF page and apex controller

I have to Custom Object: ObjectA__c, and ObjecB__c, ObjectB__c related list of ObjectA__c, 
I have data in ObjectA__c now want to add new entery inside the ObjectB__c related list by the use VF page. How can i do that
I assume that you already have records in ObjectA__c, and you are on a VF Page that takes input data for ObjectB__c.
Following is the sample code to have a dropdown for selecting ObjectA__c record for lookup field, so that link objectB__c records with any one record of ObjectA__c.

(Change field names as per Api names in you Org, in apex as well as VF)
public class TestClass2 {
    public ObjectB__c objB{get;set;}
    public TestClass2(){
       objB=new ObjectB__c();
    public List<Selectoption> getObjectARecords(){
        List<SelectOption> objectAList = new List<SelectOption>();
        objectAList.add(new SelectOPtion('','--None--'));
        for(ObjectA__c o: [Select id,Name from ObjectA__c Where yourCOndition])
            objectAList.add(new selectOption(o.id,o.name));
    public void save(){
        // your logic to save data here

<apex:page id="pg" controller="TestClass2">
    <apex:form >
        <apex:pageblock >
            Field1<apex:inputText value="{!objB__c.Name}"/><br/>
            Field2<apex:inputText value="{!objB__c.customField1__c}"/><br/>
            Field3<apex:inputText value="{!objB__c.customField2__c}"/><br/>
            Lookup Field: 
            <apex:selectList value="{!objb__c.lookupFieldApi__c}">
                <apex:selectOption value="{objectARecords}"/>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>

If this helps you out, please mark it Best Answer.