You need to sign in to do that
Don't have an account?

VF page for creating list of Accounts
Hi folks,
I am creating a list of Accounts vf page and showing related contact list for each Accoount record. Below is the VF code and Controller class that I am using.
VF page -
<apex:page standardController="Account" extensions="C9">
<apex:form >
<apex:pageBlock >
<p align="center">
<apex:commandButton action="{!step1}" value="Create New"/>
</p>
<apex:pageBlockTable value="{!acts}" var="a" title="Account">
<apex:column headerValue="List of Accounts">
<apex:commandLink rerender="contactDetails" value="{!a.Name}" action="{!ContactLists}" id="details">
<apex:param name="id" value="{!a.id}"/>
</apex:commandLink>
</apex:column>
</apex:pageBlockTable>
<apex:commandButton rendered="{!setCon.hasPrevious}" value="First" action="{!setCon.first}"/>
<apex:commandButton rendered="{!setCon.hasPrevious}" value="Previous" action="{!setCon.previous}"/>
<apex:outputText rendered="{!(setCon.pageNumber * setCon.pageSize) < setCon.ResultSize}" value="{!setCon.pageNumber * setCon.pageSize} Of {!setCon.ResultSize}"></apex:outputText>
<apex:outputText rendered="{!(setCon.pageNumber * setCon.pageSize) >= setCon.ResultSize}" value="{!setCon.ResultSize} Of {!setCon.ResultSize}"></apex:outputText>
<apex:commandButton rendered="{!setCon.hasNext}" value="Next" action="{!setCon.next}"/>
<apex:commandButton rendered="{!setCon.hasNext}" value="Last" action="{!setCon.last}"/>
</apex:pageBlock>
<apex:pageBlock title="Contact">
<apex:pageBlockButtons location="top">
<apex:commandButton value="Delete" action="{!DeleteChecked}" />
</apex:pageBlockButtons>
<apex:outputPanel id="contactDetails">
<apex:pageBlockTable value="{!contactsWrapper}" var="con" id="contactswrapper" title="Contact">
<apex:column >
<apex:inputCheckbox value="{!con.checked}" />
</apex:column>
<apex:column value="{!con.contact.Name}" />
<apex:column value="{!con.contact.Phone}" />
<apex:column value="{!con.contact.Email}" />
</apex:pageBlockTable>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller :-
public class C9 {
public PageReference Account() {
return null;
}
public C9(ApexPages.StandardController controller) {
}
public ApexPages.StandardSetController setCon {
get {
if(setCon == null) {
setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
[select Name, ID from Account]));
}
return setCon;
}
set;
}
public List<Account> getacts() {
setCon.setpagesize(10);
return (List<Account>) setCon.getRecords();
}
Account account;
public Account getAccount() {
if(account == null) account = new Account();
return account;
}
public PageReference step1() {
return Page.v2;
}
public PageReference save() {
insert account;
PageReference v2Page = new ApexPages.StandardController(account).view();
v2Page.setRedirect(true);
return Page.v9;
}
public List<ContactWrapper> contactsWrapper {get;set;}
public PageReference ContactLists()
{
System.debug('The value is:' + contact.Name);
if (ApexPages.currentPage().getParameters().get('id') != null)
for (Contact contact : [Select id,Name,Phone,Email from contact where accountId =: ApexPages.currentPage().getParameters().get('id')]){
contactsWrapper.add(new ContactWrapper(contact,false));
}
return null;
}
public class ContactWrapper {
public Contact contact {get; set;}
public Boolean checked {get; set;}
public ContactWrapper(Contact contact, Boolean checked){
this.contact = contact;
this.checked = checked;
}
}
public list <Contact> dltcontacts {get;set;}
public PageReference DeleteChecked(){
dltcontacts=new list<contact>();
for(ContactWrapper cc: contactsWrapper){
if(cc.checked){
dltcontacts.add(cc.contact);
}
}
delete dltcontacts;
return null;
}
}
When I am clicking on Account record to get the related contact list - it is showing the following error
System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!ContactLists}' in page v9: Class.C9.ContactLists: line 52, column 1
Class.C9.ContactLists: line 52, column 1
Can anyone help me in solving this?
I am creating a list of Accounts vf page and showing related contact list for each Accoount record. Below is the VF code and Controller class that I am using.
VF page -
<apex:page standardController="Account" extensions="C9">
<apex:form >
<apex:pageBlock >
<p align="center">
<apex:commandButton action="{!step1}" value="Create New"/>
</p>
<apex:pageBlockTable value="{!acts}" var="a" title="Account">
<apex:column headerValue="List of Accounts">
<apex:commandLink rerender="contactDetails" value="{!a.Name}" action="{!ContactLists}" id="details">
<apex:param name="id" value="{!a.id}"/>
</apex:commandLink>
</apex:column>
</apex:pageBlockTable>
<apex:commandButton rendered="{!setCon.hasPrevious}" value="First" action="{!setCon.first}"/>
<apex:commandButton rendered="{!setCon.hasPrevious}" value="Previous" action="{!setCon.previous}"/>
<apex:outputText rendered="{!(setCon.pageNumber * setCon.pageSize) < setCon.ResultSize}" value="{!setCon.pageNumber * setCon.pageSize} Of {!setCon.ResultSize}"></apex:outputText>
<apex:outputText rendered="{!(setCon.pageNumber * setCon.pageSize) >= setCon.ResultSize}" value="{!setCon.ResultSize} Of {!setCon.ResultSize}"></apex:outputText>
<apex:commandButton rendered="{!setCon.hasNext}" value="Next" action="{!setCon.next}"/>
<apex:commandButton rendered="{!setCon.hasNext}" value="Last" action="{!setCon.last}"/>
</apex:pageBlock>
<apex:pageBlock title="Contact">
<apex:pageBlockButtons location="top">
<apex:commandButton value="Delete" action="{!DeleteChecked}" />
</apex:pageBlockButtons>
<apex:outputPanel id="contactDetails">
<apex:pageBlockTable value="{!contactsWrapper}" var="con" id="contactswrapper" title="Contact">
<apex:column >
<apex:inputCheckbox value="{!con.checked}" />
</apex:column>
<apex:column value="{!con.contact.Name}" />
<apex:column value="{!con.contact.Phone}" />
<apex:column value="{!con.contact.Email}" />
</apex:pageBlockTable>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller :-
public class C9 {
public PageReference Account() {
return null;
}
public C9(ApexPages.StandardController controller) {
}
public ApexPages.StandardSetController setCon {
get {
if(setCon == null) {
setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
[select Name, ID from Account]));
}
return setCon;
}
set;
}
public List<Account> getacts() {
setCon.setpagesize(10);
return (List<Account>) setCon.getRecords();
}
Account account;
public Account getAccount() {
if(account == null) account = new Account();
return account;
}
public PageReference step1() {
return Page.v2;
}
public PageReference save() {
insert account;
PageReference v2Page = new ApexPages.StandardController(account).view();
v2Page.setRedirect(true);
return Page.v9;
}
public List<ContactWrapper> contactsWrapper {get;set;}
public PageReference ContactLists()
{
System.debug('The value is:' + contact.Name);
if (ApexPages.currentPage().getParameters().get('id') != null)
for (Contact contact : [Select id,Name,Phone,Email from contact where accountId =: ApexPages.currentPage().getParameters().get('id')]){
contactsWrapper.add(new ContactWrapper(contact,false));
}
return null;
}
public class ContactWrapper {
public Contact contact {get; set;}
public Boolean checked {get; set;}
public ContactWrapper(Contact contact, Boolean checked){
this.contact = contact;
this.checked = checked;
}
}
public list <Contact> dltcontacts {get;set;}
public PageReference DeleteChecked(){
dltcontacts=new list<contact>();
for(ContactWrapper cc: contactsWrapper){
if(cc.checked){
dltcontacts.add(cc.contact);
}
}
delete dltcontacts;
return null;
}
}
When I am clicking on Account record to get the related contact list - it is showing the following error
System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!ContactLists}' in page v9: Class.C9.ContactLists: line 52, column 1
Class.C9.ContactLists: line 52, column 1
Can anyone help me in solving this?
All Answers
Error is because you neither have initialized the variable of ContactWrapper Wrapper class nor checked for null
Thanks,
Amit Singh
Error is resolved but when I click on record it shows it's contact related list and when I click on another record it just add the contact to the list and the contact from previous record is still showing. I want that when the record is clicked then it should show it's contact list only. I think I need to initialize... Can you help ?
Thanks,
Shahrukh
Thanks Amit!
http://blog.jeffdouglas.com/2010/03/04/passing-parameters-with-a-commandbutton/
https://webkul.com/blog/use-apexparam-tag-visualforce/