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
SFDC INSFDC IN 

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?
 
Best Answer chosen by SFDC IN
Amit Singh 1Amit Singh 1
try this
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(){
	   If(contactsWrapper!=null && contactsWrapper.size() > 0)contactsWrapper.clear();
       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')]){
			if(contactsWrapper == null)
				contactsWrapper = new List<ContactWrapper>();
			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;        
    }
}

 

All Answers

Amit Singh 1Amit Singh 1
Try the given code
Error is because you neither have initialized the variable of ContactWrapper Wrapper class nor checked for null
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')]){
			if(contactsWrapper == null)
				contactsWrapper = new List<ContactWrapper>();
			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;        
    }
}

Thanks,
Amit Singh
SFDC INSFDC IN
Thanks Amit. It is working now :)
Amit Singh 1Amit Singh 1
If this worked please mark as solved :) 
SFDC INSFDC IN
Hi Amit,

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
Amit Singh 1Amit Singh 1
try this
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(){
	   If(contactsWrapper!=null && contactsWrapper.size() > 0)contactsWrapper.clear();
       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')]){
			if(contactsWrapper == null)
				contactsWrapper = new List<ContactWrapper>();
			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;        
    }
}

 
This was selected as the best answer
SFDC INSFDC IN
Yes, it is working.

Thanks Amit!
Amit Singh 1Amit Singh 1
Are you still facing any issue if not please close the thread
SFDC INSFDC IN
Hi Amit, One more request. In the above VF page, I want to add a button on the contact lists that creates the new contact for the Account Id which I am passing to show the related contacts. However, I tried but unable link the account id with the related list. Can you guide me through this ? Thanks, Shahrukh