You need to sign in to do that
Don't have an account?
Shobhit 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.
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.
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.
Let me if it is helpful.
Peace.
All Answers
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.
Let me if it is helpful.
Peace.
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
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.
For this you need to query child records in the constructor.
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 :(
Please mark it as Best answer.
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.
Any help on this will be appreciated. Thank you. :)
Regards,
Shobhit