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
Maddy6161Maddy6161 

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
mritzimritzi
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)
Apex:
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
    }
}


VF:
<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:selectList>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageblock>
       
    </apex:form>
</apex:page>


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