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
D ROKAD ROKA 

Visual Force

 I want to create the Visual Force page where i can select the record from lookup and its  child records should get displayed in the datatable with  Add, Save and delete button, there should be a checkbox for every record and after selecting the checkbox it should allow me to delete that particular record. I have design VF page but its not working, code is as below -

 

**********Controller****************
public class TestPageController {

public Account acc {get;set;}

public List conlist{get;set;}
public List con{get;set;}

// Public Contact inList{get;set;}
// Public List l{get;set;}
// List conlist;

public TestPageController()
{
acc=new Account();
conList= new List();
}

public PageReference getContacts() {
con= [select id, firstname, AssistantName,Birthdate,Phone from Contact where Accountid =: acc.parentid];
system.debug('#########'+con);
conlist = new List();
for(Contact c: con)
{
conlist.add(new Contactwrapper(c,false));
}
system.debug('#########'+conlist);
return null;

}
public PageReference getSelected()
{
// conlist.clear();
for(Contactwrapper awp: conlist)
if(awp.selected == true)
{
// conlist.add(accwrapper.acc);
system.debug('********'+awp.cw.firstname);

}
return null;
}

public pageReference getDelete(){
List dList = new List();
for(Contactwrapper wrap: conlist){
if(wrap.selected == true)
{
dList.add(wrap.cw);
}
}
if(dList !=null && dList.size() >0){
delete dList;
}
return null;
}

public void getCancel(){

}

public PageReference AddRow() {
Contact conObj = new Contact();
conlist.add(new Contactwrapper(conObj,false));
return null;
}

public pageReference SaveRecord(){
List ctList = new List();
for(Contactwrapper wrap: conlist){
if(wrap.selected == true)
{
ctList.add(wrap.cw);
}
}
if(ctList!=null && ctList.size() >0){
update ctList;
}
return null;
}

public class Contactwrapper{
public Contact cw{get; set;}
public Boolean selected{get; set;}

public Contactwrapper(Contact c,boolean b){
cw = c;
selected = b;
// system.debug('********'+acc);
}
}

}


***************** VF Page ********************

<apex:page controller="TestPageController" showHeader="false" sidebar="false"> <script> function savemsg() { alert('Record saved!!') } </script> <apex:form >
<apex:pageBlock >
<apex:pageblockSection >
<apex:inputField value="{!acc.parentid}" label="Select Account"/>
</apex:pageblockSection>
<apex:pageBlockButtons location="Bottom" >
<apex:commandButton value="Show Contacts" action="{!getContacts}" reRender="table"/>

</apex:pageBlockButtons>
</apex:pageBlock>
<apex:pageBlock id="table" title="Display Contacts">
<apex:pageBlockSection >
<apex:pageBlockTable value="{!conlist}" var="c">
<apex:column headerValue="Select" >

<apex:inputCheckbox value="{!c.selected}">
<apex:actionSupport event="onclick" action="{!GetSelected}" rerender="Selected_PBS"/>
</apex:inputCheckbox>
</apex:column>
<apex:column >
<apex:inputField value="{!c.cw.firstname}"/>
</apex:column>
<apex:column >
<apex:inputfield value="{!c.cw.AssistantName}" />
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>

<apex:pageblock title="Buttons">

<apex:pageBlockButtons location="Bottom">
<apex:commandButton value="Add New" action="{!AddRow}"/>
<apex:commandButton value="Save" action="{!SaveRecord}" onclick="savemsg()"/>
<apex:commandButton value="Delete" action="{!getDelete}"/>

</apex:pageBlockButtons>

</apex:pageblock>
</apex:form>
</apex:page>
***************************************************************************************************************************************

Arun MKArun MK

Hi,

 

You have wrongly define the lists in teh controllers.

 

Find teh controller and VF Page below.

*********************************************************************************************************

public class TestPageController {
public Account acc {get;set;}

public List<Contactwrapper> conlist{get;set;}
public List<Contact> con{get;set;}

// Public Contact inList{get;set;}
// Public List l{get;set;}
// List conlist;

public TestPageController()
{
acc=new Account();
conList= new List<Contactwrapper>();
}
public PageReference getContacts() {
con= [select id, firstname, LastName,Birthdate,Phone from Contact where Accountid =: acc.parentid];
system.debug('#########'+con);
conlist = new List<Contactwrapper>();
for(Contact c: con)
{
conlist.add(new Contactwrapper(c,false));
}
system.debug('#########'+conlist);
return null;

}
public PageReference getSelected()
{
// conlist.clear();
for(Contactwrapper awp: conlist)
if(awp.selected == true)
{
// conlist.add(accwrapper.acc);
system.debug('********'+awp.cw.firstname);

}
return null;
}

public pageReference getDelete(){
List<Contact> dList = new List<Contact>();
for(Contactwrapper wrap: conlist){
if(wrap.selected == true)
{
dList.add(wrap.cw);
}
}
if(dList !=null && dList.size() >0){
delete dList;
}
return null;
}

public void getCancel(){

}

public PageReference AddRow() {
Contact conObj = new Contact();
conlist.add(new Contactwrapper(conObj,false));
return null;
}
public pageReference SaveRecord(){
List<Contact> ctList = new List<Contact>();
for(Contactwrapper wrap: conlist){
if(wrap.selected == true)
{
wrap.cw.AccountId = acc.parentid;
ctList.add(wrap.cw);
}
}
if(ctList!=null && ctList.size() >0){
upsert ctList;
}
return null;
}

public class Contactwrapper{
public Contact cw{get; set;}
public Boolean selected{get; set;}

public Contactwrapper(Contact c,boolean b){
cw = c;
selected = b;
// system.debug('********'+acc);
}
}
}

 

*********************************************************************************************************

VF Page

*********************************************************************************************************

<apex:page controller="TestPageController" showHeader="false" sidebar="false"> <script> function savemsg() { alert('Record saved!!') } </script> <apex:form >
<apex:pageBlock >
<apex:pageblockSection >
<apex:inputField value="{!acc.parentid}" label="Select Account"/>
</apex:pageblockSection>
<apex:pageBlockButtons location="Bottom" >
<apex:commandButton value="Show Contacts" action="{!getContacts}" reRender="table"/>

</apex:pageBlockButtons>
</apex:pageBlock>
<apex:pageBlock id="table" title="Display Contacts">
<apex:pageBlockSection >
<apex:pageBlockTable value="{!conlist}" var="c">
<apex:column headerValue="Select" >

<apex:inputCheckbox value="{!c.selected}">
<apex:actionSupport event="onclick" action="{!GetSelected}" rerender="Selected_PBS"/>
</apex:inputCheckbox>
</apex:column>
<apex:column >
<apex:inputField value="{!c.cw.firstname}"/>
</apex:column>
<apex:column >
<apex:inputfield value="{!c.cw.LastName}" />
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>

<apex:pageblock title="Buttons">

<apex:pageBlockButtons location="Bottom">
<apex:commandButton value="Add New" action="{!AddRow}"/>
<apex:commandButton value="Save" action="{!SaveRecord}" onclick="savemsg()"/>
<apex:commandButton value="Delete" action="{!getDelete}"/>

</apex:pageBlockButtons>

</apex:pageblock>
</apex:form>
</apex:page>


If this post is helpful please throw Kudos.
If this post solves your problem kindly mark it as solution.

 

Regards,

Arun,