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
Sibanee purohitSibanee purohit 

How to save child records from visualforce page using component?

I need to save records from visualforce page.My requirement is I need to get related objects dynamically based on any parent record id.So I created 1 Vf component and controller for that where I am getting related object and fields which can be displayed on vf page but i need to edit and save some record.How to achieve that.Please help me with this. Here is the code I have tried:
VF component

<apex:component controller="CustomRelatedListControllerNew" allowDML="true" >
    <apex:attribute assignTo="{!typeOf}" name="typeOfParam" required="true" type="String" description="Type of Related Record" />
    <apex:attribute assignTo="{!fieldlist}" name="relatedFieldlistParam" required="true" type="String[]" description="String of related Fields." />
    <apex:attribute assignTo="{!relatedField}" name="relatedFieldParam" required="true" type="String" description="String of related field." />   
    <apex:attribute assignTo="{!relatedId}" name="relatedIdParam" required="true" type="String" description="String of related Id." />
    <!---<apex:attribute assignTo="{!obj}" name="relatedIdobject" required="true" type="sObject[]" description="String of related objects." />-->

    <apex:form >
    <apex:pageblock title="{!typeOf}" id="childobj">
    <apex:commandButton value="Save" action="{!Save}" reRender="childobj"/> 
    <apex:pageBlockTable var="o" value="{!objects}">

      <apex:repeat value="{!fieldlist}" var="fldNames">
       <apex:column > 

                <apex:outputText value="{!$ObjectType[typeOf].fields[fldNames].Name}">

                </apex:outputText>

        </apex:column>
        <br></br>
        <apex:column >
        <apex:inputField value="{!o[fldNames]}">
        <!---<apex:inlineEditSupport showOnEdit="saveButton,cancelButton"
                        hideOnEdit="editButton" event="ondblclick"
                        changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/>-->
                        </apex:inputField> 
        </apex:column>
        <br/>
          </apex:repeat>



    </apex:pageBlockTable>

    </apex:pageblock>

    </apex:form>
</apex:component>

Controller:
public class CustomRelatedListControllerNew {
    private ApexPages.StandardController controller {get; set;}
    public String typeOf { get; set; }  
    public String relatedField { get; set; }
    public List<String> fieldlist{ get; set; }
    public Id relatedId { get; set; }
    public List<sObject> obj{ get; set; }
    public String query;
    public Integer n;
    public List<sObject> objects;

   public List<sObject> getObjects() {
        n = fieldlist.size();
        System.debug(n);
        //String d = ApexPages.currentPage().getParameters().get('id');
        //System.debug(d);
        query = fieldlist.get(0);
        //String query1 = 'SELECT Id'+ ' ' + 'FROM'+ ' ' + 'Attachment';
        for(Integer i=1;i<fieldlist.size();i++){
            query = query+ ' , ' + fieldlist.get(i) ;
            System.debug(query);
        }

        System.debug('qqq****'+'SELECT'+ ' '+ query +' '+'FROM '+ typeOf+' '+'WHERE' +' '+  relatedField + ' = \'' + relatedId +'\'');
        objects = Database.query('SELECT'+ ' '+ query +' '+'FROM '+ typeOf+' '+'WHERE' +' '+  relatedField + ' = \'' + relatedId +'\'');
       //return Database.query('SELECT'+ ' '+ query +' '+'FROM '+ typeOf+' '+'WHERE' +' '+  relatedField + ' = \'' + relatedId +'\'');
       return objects;
    }
    public void setObjects(List<sObject> objects)
    {
        System.debug('ooo'+objects);
        this.objects = objects;
        //objects = s;
        //update objects;
        
    }
   public PageReference Save() {
        setObjects();
        update objects;
        System.debug('ooo'+objects);
        return null;
    }

}

getting child records in 'objects' but it's not getting saved and setobject method also not getting invoked.Where I am doing wrong here please help me with this
SVsfdcSVsfdc
You need to pass argument for  setObjects() calling in Save method.
Sibanee purohitSibanee purohit
yes my mistake..I have tried that by passing arguement but my doubt is why that update statement isn't working when I click save button?