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
ITSS RC 12ITSS RC 12 

how to save the list of child records with parent ID and input field in child

Hi All,

I have requirement to save the child records with parent id and with custom input type as text field in child.
Actually my reqirement is i need to show one form with topics,categories, tasks.Please find the below screen shot for referenceUser-added image
In the above form in need to save all the tasks in one child object with parent id and in the above image comments is the input text box.When i click on the save button the child records are going to save but what ever i have given in comments field that is not going to save it is taking only last comment, and save for all records.

Please find the below code for VF Page and Controller.

Page:

<apex:page controller="formOneController" showHeader="false" sidebar="false" docType="html-5.0">
  <apex:form >
  <div align="center"  draggable="false" >
        <apex:commandButton action="{!save}" value="save"/>&nbsp; &nbsp;
        <apex:commandButton value="Cancel" action="{!cancel}"/>
    </div> 
    <!--<apex:pageBlock >-->
        <apex:repeat value="{!technologyWrapperList}" var="T">
           <!-- <apex:column value="{!T.technology.Name}"/>
            <apex:column value="{!T.member.Age__c}"/>
            <apex:column >-->
            <table style="width:100%" border="1" cellpadding="1" cellspacing="1">
                <tr>
                    <td colspan="2" style="background-color:RGB(0, 64, 255);color:White;font-weight:bold" height="20px"  text-align="top" width="100%"> Form : {!T.technology.Name__c}</td>
                </tr>
            
            </table>
            
            <apex:repeat value="{!T.catrgryList}" var="C">
            <table style="width:100%" border="1" cellpadding="1" cellspacing="0">
                <!--<apex:column value="{!C.Name__c}"/>-->
                <tr>
                <td colspan="0" style="background-color:RGB(179, 26, 255);color:white;font-weight:bold" height="20px"  text-align="top" width="25%">Category : {!C.Name__c}</td>
                <td colspan="0" style="background-color:RGB(179, 26, 255);color:white;font-weight:bold" height="20px"  text-align="top" width="25%">Description</td>
                <td colspan="0" style="background-color:RGB(179, 26, 255);color:white;font-weight:bold" height="20px"  text-align="top" width="23%">Check Box</td>
                <td colspan="0" style="background-color:RGB(179, 26, 255);color:white;font-weight:bold" height="20px"  text-align="top" width="15%">Comments</td>
                <td colspan="0" style="background-color:RGB(179, 26, 255);color:white;font-weight:bold" height="20px"  text-align="top" width="20%">Date</td>
                </tr><br/>
                </table>
                    <apex:repeat value="{!C.Topics__r}" var="P">
                        <!--<apex:column value="{!P.Name__c}"/>
                        <apex:column value="{!E.Serial_Number__c}"/>-->
                      <table style="width:100%" border="1" cellpadding="0" cellspacing="0"> 
                        <tr style="background-color:RGB(255, 255, 255);color:black;font-weight:normal"  text-align="top">
                        <td width="30%">{!P.Name__c}</td>   
                        <td width="28%">{!P.Description__c}</td>
                        <td width="27%"><apex:inputCheckbox /></td>
                        <td width="50%"><apex:input type="text" value="{!T.commt}" /> </td>
                        <!--<td width="50%"><apex:inputText value="{!datename}" size="10" id="demo" onfocus="DatePicker.pickDate(false, this , false);" /> </td>-->     
                        <td width ="50%"><apex:input type="date" value="{!dateValue}"/></td>
                        </tr>
                        <!--<br/>-->
                        </table>
                    </apex:repeat>
               
            </apex:repeat>
        
    </apex:repeat>
<!--</apex:pageBlock>-->
</apex:form>
</apex:page>




Controller:

public with sharing class formOneController {

    public PageReference cancel() {
        return null;
    }

    public String datename { get; set; }
    public String dateValue { get; set; }
    public list<site__c> siteList { get; set;}
    public site__c st { get; set;}
    //public string Comments { get; set;}
    public list<Category__c> listctg;
    //public list<Topic__c> listtpc;
     public string commt {get; set;}
     public list<Site__c> stchilds{get;set;}
    public list<site__c> newlistSite = new list<site__c>();
    
    public  PageReference save() {
        for(TechnologyWrapper awrapper : technologyWrapperList){
        system.debug('$$$Comments$$$'+awrapper.commt);
        listctg =[Select Name, Description__c, Name__c,Technology__c, (SELECT Name, Name__c, Description__c FROM Topics__r) FROM Category__c WHERE Technology__c = 'a082800000lbaHV'];
        for(Category__c cgry : listctg){
            for(Topics__c tpic : cgry.Topics__r){
                site__c objsite = new site__c();
                objsite.Technology_ID__c = 'a082800000lbaHV';
                objsite.Category_ID__c = cgry.id;  
                objsite.Topics_ID__c = tpic.id;
                objsite.Comments__c=awrapper.commt;
                newlistSite.add(objsite);
            }            
        }
        }
        /*stchilds = new list<Site__c>();
        for(TechnologyWrapper awrapper : technologyWrapperList){
          awrapper.commt = attendee.id;
          awrapper.atd.LQ_Site_Initiation__c = sivParent.id;
          atndchilds.add(awrapper.atd);         
        }*/
        upsert newlistSite;
        return null;
    }

   public List<Technology__c> technologyList { get; set; }
    public List<Category__c> categorytList { get; set; }
    public Map<Id, List<Category__c>> techIdCategorytListMap { get; set; }
    Set<Id> techIds = new Set<Id>();
    Map<Id, Technology__c> technologyMap = new Map<Id, Technology__c>();
    
    public List<TechnologyWrapper> technologyWrapperList { get; set; }

     public formOneController() {   
        dateValue = Date.Today().format();
        technologyList = [SELECT Name,Description__c,Name__c FROM Technology__c where id ='a082800000lbaHV'];
        techIdCategorytListMap = new Map<Id, List<Category__c>>();
        technologyWrapperList = new List<TechnologyWrapper>();
        if(technologyList.size() > 0) {
            for(Technology__c tngy : technologyList) {
                techIds.add(tngy.Id);
                technologyMap.put(tngy.Id, tngy);
            }
            categorytList = [Select Name, Description__c, Name__c,Technology__c, (SELECT Name, Name__c, Description__c FROM Topics__r) FROM Category__c WHERE Technology__c = 'a082800000lbaHV'];
            system.debug('Category List is ' + categorytList);
        }
        if(categorytList.size() > 0) {
            for(Category__c ctg : categorytList) {
                if(!techIdCategorytListMap.containsKey(ctg.Technology__c)){
                    techIdCategorytListMap.put(ctg.Technology__c, new List<Category__c>());
                }
                techIdCategorytListMap.get(ctg.Technology__c).add(ctg);
            }
            for(Id categoryId : techIdCategorytListMap.keySet()) {
                technologyWrapperList.add(new TechnologyWrapper(technologyMap.get(categoryId), techIdCategorytListMap.get(categoryId),commt));
            }
        }
    } 
    
    public class TechnologyWrapper {
        public Technology__c technology { get; set; }
        public List<Category__c> catrgryList { get; set; }
        public List<Topics__c> topicsList {get; set;}
        public string commt {get; set;}
        public boolean chekbx {get; set;}
        public date dt{get; set;}
        
        public TechnologyWrapper(Technology__c technology, List<Category__c> catrgryList,string commt) {
            this.technology = technology;
            this.catrgryList = catrgryList;
            this.commt = commt;
            chekbx = false;
            dt = system.now().date();
        }
    }
    public class siteWrapper
    {
        public Site__c lqst {get; set;}
        public String lqcmnts {get;  set;}
   
        public siteWrapper(Site__c sts,String cmnts)
        {
            lqcmnts = cmnts;
            lqst = sts;
        }
    }
}


Please give some suggestions.

Thanks,
Bhaskar