+ Start a Discussion
Shobhit TShobhit T 

Save Child Records on Parent detail Page

Hi All,

i have two objects 
1. Assigned task - parent object
2. Report Information - child object

I have created a VF page and placed it on the Assigned Task detail page.
My Vf Page has two buttons
1. Add Report - which adds a new dynamic section
2. Save Report - Save all the data in Dynamic section

I am successfull in creating dynamic text boxes but I'm not able to save them. Could anyone please help me that how should I save my child records based on the id of the parent object.
My VF code is : - 
<apex:page standardcontroller="Assigned_tasks__c" extensions="TestTaskController1"> 
<apex:form > 
<apex:pageBlock > <
apex:pageBlockTable value="{!reportlist}" var="acc"> 
<apex:column headerValue="Report Name"> 
<apex:inputField value="{!acc.Report_Name__c}" rendered="{!NOT(saved)}"/> 
<apex:outputField value="{!acc.Report_Name__c}" rendered="{!saved}"/>
 <br /> </apex:column> <apex:column headerValue="Comments">
 <apex:inputField value="{!acc.Comments__c}" rendered="{!NOT(saved)}"/> 
<apex:outputField value="{!acc.Comments__c}" rendered="{!saved}"/> 
<br /> </apex:column> </apex:pageBlockTable>
 <apex:pageBlockButtons > 
<apex:commandButton value="Add Report" action="{!addAccount}"/>
 <apex:commandButton value="Save Report" action="{!saveAccount}" /> 
</apex:pageBlockButtons>
 </apex:pageBlock> 
</apex:form> 
</apex:page>

and APEX code is :- 

public class TestTaskController1 {

Report_Information__c task = new Report_Information__c();
Public boolean saved {get; set;}

public list<Report_Information__c> reportlist{ get; set; }

    public TestTaskController1(ApexPages.StandardController controller) {
              reportlist=new list<Report_Information__c>();
              reportlist.add(task);
              saved=false;
    }


Public void addAccount()
{
Report_Information__c acc = new Report_Information__c();
reportlist.add(acc);
}


public PageReference saveAccount() {
for(Integer i=1; i<reportlist.size(); i++)
{

insert reportlist;
saved=true;
}

 return Page.Allassigned;
}

}

Snapshot of my VF page which will be placed on Parent object.

User-added image
Best Answer chosen by Shobhit T
Naresh YadavNaresh Yadav
Hi Shobhit Tuteja

for(Integer i=1; i<reportlist.size(); i++)
{
//  set parent id here
insert reportlist;
saved=true;
}


In the above code you have to set parent id. Here i suppose that the records insertion is successfull but they are not linked to any parent.

Please follow the below code to linked the record to a parent.
 
public class TestTaskController1 {

Report_Information__c task = new Report_Information__c();
Public boolean saved {get; set;}
public String parentId;
public list<Report_Information__c> reportlist{ get; set; }

    public TestTaskController1(ApexPages.StandardController controller) {
              reportlist=new list<Report_Information__c>();
              reportlist.add(task);
              saved=false;
              parentId = controller.getId();
    }


Public void addAccount()
{
Report_Information__c acc = new Report_Information__c();
reportlist.add(acc);
}


public PageReference saveAccount() {
for(Report_Information__c re : reportlist)
{
     re.assign_task__c = parentId;
}
if(reportlist.size()>0){
insert reportlist;
​ saved=true;
} 
 return Page.Allassigned;
}

}
Let me if it is helpful.
Peace.

 

All Answers

Naresh YadavNaresh Yadav
Hi Shobhit Tuteja

for(Integer i=1; i<reportlist.size(); i++)
{
//  set parent id here
insert reportlist;
saved=true;
}


In the above code you have to set parent id. Here i suppose that the records insertion is successfull but they are not linked to any parent.

Please follow the below code to linked the record to a parent.
 
public class TestTaskController1 {

Report_Information__c task = new Report_Information__c();
Public boolean saved {get; set;}
public String parentId;
public list<Report_Information__c> reportlist{ get; set; }

    public TestTaskController1(ApexPages.StandardController controller) {
              reportlist=new list<Report_Information__c>();
              reportlist.add(task);
              saved=false;
              parentId = controller.getId();
    }


Public void addAccount()
{
Report_Information__c acc = new Report_Information__c();
reportlist.add(acc);
}


public PageReference saveAccount() {
for(Report_Information__c re : reportlist)
{
     re.assign_task__c = parentId;
}
if(reportlist.size()>0){
insert reportlist;
​ saved=true;
} 
 return Page.Allassigned;
}

}
Let me if it is helpful.
Peace.

 
This was selected as the best answer
Shobhit TShobhit T
Hi @Naresh Yadav,

Thanks for your support. My code works now. 
But now the problem is I am not able to save the View State. When I add the records and Save them , they are saved and an entry is made into the child object but when i refresh the page the records are not saved in VF page. Any idea how to save the data on refresh. ?? thanks in advance for your help. Appreciate your time and help. :) 


Best Regards,
Shobhit
Naresh YadavNaresh Yadav
Means you want that when you refreash the page then saved records shows in the VF page right ?
Shobhit TShobhit T
Hi Naresh,

As you can see in the screenshot, when i add the information and Save the data , it is saved in the VF page and this VF page is placed on the detail page of the parent and when i refresh the whole page the data in the Vf page is lost and I still see the editble fields.
Records are created in the child object but are not saved in this VF page. Hope this helps.

User-added image

 
Naresh YadavNaresh Yadav
Yes i understand your requirement.

For this you need to query child records in the constructor.
Shobhit TShobhit T
I need to query those with parent id ?? 

Like this is my constructor :-

public TestTaskController1(ApexPages.StandardController controller) {
              reportlist=new list<Report_Information__c>();
              reportlist.add(task);
              saved=false;
              parentId= controller.getId();
    }


so it will be like :-

reportlist= [select report_name__c,comments__c,Atask__c where Atask__c =:parent.id].

like this ?? Sorry I am not good in coding :(

 
Naresh YadavNaresh Yadav
Yes right.
Shobhit TShobhit T
Ok let me try. thanks. :) 
 
Naresh YadavNaresh Yadav
If this post help you out. 
Please mark it as Best answer.
Shobhit TShobhit T
Yes, I will for sure. :) 
 
Shobhit TShobhit T
Hi Naresh,

Thanks for your help, Now it is saving the records on refresh but they are being saved in editable mode , how to save them in Read Only mode.
when i save them before refresh, they are saved in Read only mode but when i refresh the page it becomes in editable again. Thanks in advance for your help.
Shobhit TShobhit T
HI @Naresh Yadav,

Any help on this will be appreciated. Thank you. :) 

Regards,
Shobhit
Shobhit TShobhit T
Any help on this , will be appreciated. :)