Good afternoon,

We have a visualforce page that allows us to add multiple contacts at once from a account view. When we add these I would like to be able to associate these contacts with a campaign however have limited experience of coding. From searches I think I need to add an extension to the standard list controller need some guidance on how to write this code. If it helps I can show the existing code.

Thanks Sam

  • 1
    Hi Sam, welcome to SFSE! I recommend the following resources Visualforce Workbook and the Visualforce Developer’s Guide. You should find the resources you're looking for there to help you write the controller. If you get stuck, post back, click on the edit button to paste in your code and tell us where you're having problems. When you paste your code, select it and click on the {} icon so it will display. – crmprogdev Mar 20 '15 at 14:41

You don't need multiple controllers for this. I think one custom controller or controller extension will be suffice in this case.

Giving you an example - how you can proceed:

VisualForce Page

<apex:page standardcontroller="account" extensions="cntShowAccountcontacts" >
    <apex:pageblock title="Account's Contacts">
        Hello {!$User.FirstName}/ You're viewing {!account.name}'s Contacts!
    <apex:form >
        <apex:pageblock >
            <apex:pageblocktable value="{!Contactlist}" var="c">
                <apex:column >
                    <apex:inputCheckbox value="{!c.selected}"/>
                <apex:column headerValue="First Name" >
                    <apex:inputField value="{!c.con.Firstname}"/>
                <apex:column headerValue="Last Name" >
                    <apex:inputField value="{!c.con.Lastname}"/>
                <apex:column headerValue="B'day" >
                    <apex:inputField value="{!c.con.Birthdate}"/>
            <apex:pageBlockButtons >
            <apex:commandButton value="Save Contacts" action="{!saveContacts}"/>

Apex Controller

public class cntShowAccountcontacts {

    public cntShowAccountcontacts(ApexPages.StandardController controller) {


    public List<cContact> ContactwithCheckbox {get; set;}

    public list<cContact> getContactlist()
        if(ContactwithCheckbox == null) 
            ContactwithCheckbox = new list<cContact>();

        contact c1 = new contact();
        contact c2 = new contact();
        contact c3 = new contact();
        ContactwithCheckbox.add(new cContact(c1));
        ContactwithCheckbox.add(new cContact(c2));
        ContactwithCheckbox.add(new cContact(c3));

        Return ContactwithCheckbox;

    public class cContact {

        public Contact con {get; set;}
        public Boolean selected {get; set;}

        public cContact(Contact c) {
            con = c;
            selected = false;

    public void saveContacts() 
        List<Contact> selectedContacts = new List<Contact>();
        for(cContact cCon: getContactlist()) {
            if(cCon.selected == true) {
        if(selectedContacts.size() > 0)
            insert selectedContacts; 

This is slighly different. It just shows 3 contacts to add to an account(this can be changed from controller). It gives flexibility to choose which contacts to add using a checkbox. I've used wrapper class, but without that, it's also possible. This should run in your dev org as it is. Let me know if you still face any doubt and still don't get further clues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.