You need to sign in to do that
Don't have an account?
HIRAL PUROHIT
How to add and remove Dynamic Row in pageBlocktable
Hello,
I created table in my custom object.But now I want to add new row to add more records.Though I created it with 'AddRow' button.
But when I click on that button it's showing no change. Please help with this.
Here I am posting my code:
Visualforce Error
Help for this Page
System.DmlException: Upsert failed. First exception on row 0 with id a033600000RQRQjAAP; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Courses]: [Courses]
Error is in expression '{!Save}' in component <apex:commandButton> in page student_details_1: Class.addFunction.Save: line 32, column 1
Class.addFunction.Save: line 32, column 1
I created table in my custom object.But now I want to add new row to add more records.Though I created it with 'AddRow' button.
But when I click on that button it's showing no change. Please help with this.
Here I am posting my code:
<apex:page standardController="Student__c" extensions="addFunction"> <apex:form > <apex:sectionHeader title="Student Edit" subtitle="New Student"/> <apex:pageBlock mode="edit" > <apex:pageBlockButtons > <apex:commandButton action="{!Save}" value="Save"/> <apex:commandButton action="{!Cancel}" value="Cancel"/> </apex:pageBlockButtons> <apex:pageBlockSection title="Student Details" columns="1" > <apex:inputField value="{!Student__c.Student_Id__c}"/> <apex:inputField value="{!Student__c.Name}"/> <apex:inputField value="{!Student__c.Last_Name__c}"/> <apex:inputField value="{!Student__c.Contact_Number__c}" /> <apex:inputField value="{!Student__c.Email_Id__c}"/> <apex:inputField value="{!Student__c.Address__c}"/> <apex:inputField value="{!Student__c.Degree__c}"/> <apex:inputField value="{!Student__c.Department__c}"/> </apex:pageBlockSection> <apex:pageBlockSection title="Course Details" id="hp"> <apex:pageMessages /> <apex:variable var="rowNumber" value="{!0}"/> <apex:pageBlockTable value="{!Student__c}" var="stu"> <apex:column headerValue="No." style="width:20px; text-align:center;" headerClass="centertext"> <apex:outputText value="{0}" style="text-align:center;"> <apex:param value="{!rowNumber+1}" /> </apex:outputText> </apex:column> <apex:column headerValue="Courses" > <apex:inputField value="{!stu.Courses__c}" /> </apex:column> <apex:column headerValue="Professor" > <apex:inputField value="{!stu.Professor__c}"/> </apex:column> <apex:column headerValue="Final Grade" > <apex:inputField value="{!stu.Final_Grade__c}" /> </apex:column> <apex:column headerValue="Action" > <apex:commandButton value="Delete" action="{!deleteRow}" reRender="hp"> <apex:param name="rowIndex" value="{!rowNumber}"/> </apex:commandButton> <apex:variable var="rowNumber" value="{!rowNumber+1}"/> </apex:column> </apex:pageBlockTable> <apex:commandButton action="{!addRow}" value="Add Course" reRender="hp"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>Controller Code:
public class addFunction { public Student__c s; public Student__c del; public List<Student__c> addcourseList {get;set;} public List<Student__c> delcourseList {get;set;} public List<Student__c> courseList {get;set;} public Integer totalCount {get;set;} public Integer rowIndex {get;set;} public List<Student__c> delCourse {get; set;} public addFunction(ApexPages.StandardController controller) { s = (Student__c)controller.getRecord(); courseList = [Select Courses__c ,Professor__c,Final_Grade__c from Student__c ]; totalCount = courseList.size(); delcourseList = new List<Student__c>(); delCourse = new List<Student__c>(); } public void addRow(){ addcourseList = new List<Student__c>(); courseList.add(new Student__c ()); } public PageReference Save(){ upsert courseList; delete delcourseList; return (new ApexPages.StandardController(s)).view(); } public void deleteRow(){ rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex')); System.debug('rowbe deleted ' + rowIndex ); System.debug('rowm to be deleted '+courseList[rowIndex]); del = courseList.remove(rowIndex); delcourseList.add(del); } }Error:
Visualforce Error
Help for this Page
System.DmlException: Upsert failed. First exception on row 0 with id a033600000RQRQjAAP; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Courses]: [Courses]
Error is in expression '{!Save}' in component <apex:commandButton> in page student_details_1: Class.addFunction.Save: line 32, column 1
Class.addFunction.Save: line 32, column 1
Kindly once refer to below blogs on how to add and remove rows dynamically from a page block table in a visual force page.Then you can start accelerating on working with the above requirement.
- http://sfdcsrini.blogspot.com/2014/12/adding-and-deleting-rows-dynamically-in.html
- http://www.forcetree.com/2015/03/dynamically-addremove-rows-from-table.html
- https://prats23.wordpress.com/2014/04/27/salesforce-dynamically-addingdeleting-rows-in-visualforce/
Please let us know if this helps.Best Regards,
Nagendra.P
The error is because in Student__c object , Course is required field but from your code in Student Details pageblock . Please add inputfiled in Student Details and then rendered this pageblock instead on "hp"
try the sample
<apex:form >
<apex:pageblock >
<br></br> <br></br>
<apex:commandButton value=" Add Row " action="{!AddRecord}" reRender="pb1"/> <br></br> <br></br> <apex:pageBlockTable id="pb1" value="{!lstWrapper}" var="item"> <apex:column headerValue="Name"> <apex:outputLabel value="{!item.sName}" /> </apex:column> <apex:column headerValue="Gender"> <apex:outputLabel value="{!item.isFlag}" /> </apex:column> </apex:pageBlockTable> </apex:pageblock> </apex:form> </apex:page>
<apex:page controller="dynamicController"> <apex:form > <apex:pageblock > <br></br> <br></br> <apex:commandButton value=" Add Row " action="{!AddRecord}" reRender="pb1"/> <br></br> <br></br> <apex:pageBlockTable id="pb1" value="{!lstWrapper}" var="item"> <apex:column headerValue="Name"> <apex:outputLabel value="{!item.sName}" /> </apex:column> <apex:column headerValue="Gender"> <apex:outputLabel value="{!item.isFlag}" /> </apex:column> </apex:pageBlockTable> </apex:pageblock> </apex:form> </apex:page>
regards,
subbu k
Thanks for reply.
Nagendra I went through the sites and Subbu I tried this code but still facing same Error.....
I have the same requirement, please go through the below code it worked for me.
******************************************* APEX CLASS ***************************************************
public class addCalendar{
public List<ApexTips_Channel__c> lstApxTps = new List<ApexTips_Channel__c>();
public List<innerClass> lstInner {get; set;}
public String selectedRowIndex {get;set;}
public Integer index {get;set;}
public Integer count = 1;
public addCalendar(ApexPages.StandardController ctlr){
lstInner = new List<innerClass>();
addMore();
selectedRowIndex = '0';
}
public PageReference Save(){
PageReference pr = new PageReference('/apex/addCalendar');
for(Integer j = 0;j<lstInner.size();j++){
lstApxTps.add(lstInner[j].axpTip);
}
insert lstApxTps;
pr.setRedirect(True);
return pr;
}
public void add(){
Count=Count+1;
addMore();
}
public void addMore()
{
//call to the iner class constructor
innerClass objInnerClass = new innerClass(count);
//add the record to the inner class list
lstInner.add(objInnerClass);
system.debug('lstInner---->'+lstInner);
}
public void remove(){
system.debug('selected row index---->'+ selectedRowIndex);
lstInner.remove(Integer.valueOf(selectedRowIndex)-1);
count = count - 1;
}
public class innerClass
{
public String recCount {get;set;}
public ApexTips_Channel__c axpTip {get;set;}
/*Inner Class Constructor*/
public innerClass(Integer intCount)
{
recCount = String.valueOf(intCount);
axpTip = new ApexTips_Channel__c();
}
}
public PageReference viewAllRecords() {
PageReference p = new PageReference('/apex/apxTipPage');
p.setRedirect(true);
return p;
}
}