0

I have an apex class and a visualforce page that has a search functionality for account. Every time an account record is searched, all its related contacts displays automatically. This works so far.

But there's another requirement wherein for every selected contacts, there must be an empty opportunity table row displayed.

There's no error whatsoever, it's just that empty opportunity table row doesn't show at all.

I have a hint that the problem is right on the createOpp() method, but I am not so sure how to fix this.

Any help will be greatly appreciated.

Controller:

public class Checkbox_Class 
    {
        private ApexPages.StandardController controller;   
        public Object1__c obj1 {get;set;}

        public Account accountqry {get;set;}
        public List<contactwrapper> contactList {get;set;}
        public List<oppwrapper> opportunityList {get;set;}
        public List<Contact> selectedContacts = new List<Contact>();
        public List<oppwrapper> tempWrap1 {get;set;} 

        public Checkbox_Class(ApexPages.StandardController con) {
            controller = con;
            obj1 = new Object1__c();  
            accountqry = new Account(); 
            contactList = new List<contactwrapper>();
            opportunityList = new List<oppwrapper>();
        }

        public void init() {

            if(obj1.Account__c != null) {
                accountqry = [SELECT Id, Name, Phone, Type FROM Account WHERE Id =: obj1.Account__c];
                contactList = getContacts();
            }

            else {
                obj1.clear();
                accountqry.clear();
                contactList.clear();
            }
        }

        public List<contactwrapper> getContacts()
        {

            for(Contact c : [select Id, FirstName, LastName from Contact WHERE AccountId =:  obj1.Account__c]) {
                contactList.add(new contactwrapper(c));
            }    
            return contactList;
        }

        public PageReference twoMethods() {
            getSelected();
            GetSelectedContacts();
            createOpp();
            return null;
        }

        public PageReference getSelected()
        {

            selectedContacts.clear();
            for(contactwrapper conwrapper : contactList)
            if(conwrapper.selected == true)
            selectedContacts.add(conwrapper.con);
            return null;
        }

        public List<Contact> GetSelectedContacts()
        {

            return selectedContacts;

        } 

        public void createOpp() {

            if(selectedContacts.size() > 0) {

                for(Contact con : selectedContacts) {

                    List<Opportunity> o2 = new List<Opportunity>();

                    for(Opportunity o3 : o2) {

                        opportunityList.add(new oppwrapper(o3,false));

                    }     

                }

            }    

        }

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

            public contactwrapper(Contact c)
            {
                con = c;
                selected = false;
            }
        }

        public class oppwrapper 
        {
            public Opportunity opp {get;set;}
            public Boolean selected2 {get;set;}

            public oppwrapper(Opportunity o, Boolean s) 
            {
                opp = o;
                selected2 = s; 
            }
        }
    }

Visualforce Page

<apex:page standardController="Object1__c" extensions="Checkbox_Class">
    <apex:form >

        <apex:pageBlock Title="Contacts with CheckBoxes" id="tbl">

            <apex:pageBlockSection title="New Object1 Record">
                <apex:inputField value="{!obj1.Account__c}">
                    <apex:actionSupport event="onchange" action="{!init}" rerender="tbl"/>
                </apex:inputField>
            </apex:pageBlockSection>

            <apex:pageBlockSection title="Account Information">
                <apex:outputText value="{!accountqry.Name}"/>
                <apex:outputText value="{!accountqry.Phone}"/>
                <apex:outputText value="{!accountqry.Type}"/>             
            </apex:pageBlockSection>

            <apex:pageBlockSection Title="List of Available Contacts">
                <apex:dataTable value="{!contactList}" var="c" columnswidth="50px,50px" cellpadding="4" border="1">
                    <apex:column >
                        <apex:facet name="header"> <apex:inputCheckbox >
                            <apex:actionSupport event="onclick" action="{!twoMethods}" onsubmit="checkAll(this)" rerender="Selected_PBS,oppSec"/>
                            </apex:inputCheckbox>
                        </apex:facet>
                        <apex:inputCheckbox value="{!c.selected}" id="checkedone">
                        <apex:actionSupport event="onclick" action="{!twoMethods}" rerender="Selected_PBS,oppSec"/></apex:inputCheckbox>
                    </apex:column>
                    <apex:column headervalue="First Name" value="{!c.con.FirstName}" />
                    <apex:column headervalue="Last Name" value="{!c.con.LastName}" />
                </apex:dataTable>
            </apex:pageBlockSection>

            <apex:pageBlockSection Title="Selected Contacts" id="Selected_PBS">
                <apex:dataTable value="{!SelectedContacts}" var="s" columnswidth="50px,50px" cellpadding="4" border="1">
                    <apex:column headervalue="First Name" value="{!s.FirstName}" />
                    <apex:column headervalue="Last Name" value="{!s.LastName}" />
                </apex:dataTable>
            </apex:pageBlockSection>

            <apex:pageBlockSection title="New Opportunity" columns="1">
                <apex:pageBlockTable value="{!o2}" var="o">

                    <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox value="{!o.selected2}"/>
                        </apex:facet>
                    </apex:column>     
                    <apex:column headerValue="Opportunity Name">
                        <apex:inputField value="{!o.opp.Name}"/>
                    </apex:column>
                    <apex:column headerValue="Amount">
                        <apex:inputField value="{!o.opp.Amount}"/>
                    </apex:column>
                     <apex:column headerValue="Stage">
                        <apex:inputField value="{!o.opp.StageName}"/>
                    </apex:column>    

                </apex:pageBlockTable>
            </apex:pageBlockSection>

        </apex:pageBlock>

    </apex:form>
    <script>
        function checkAll(cb)
        {
            var inputElem = document.getElementsByTagName("input");
            for(var i=0; i<inputElem.length; i++)
            {
                if(inputElem[i].id.indexOf("checkedone")!=-1)
                inputElem[i].checked = cb.checked;
            }
        }    
    </script>
</apex:page>

Updated Code

public class Checkbox_Class 
    {
        private ApexPages.StandardController controller;   
        public Object1__c obj1 {get;set;}
        public Account accountqry {get;set;}
        public List<contactwrapper> contactList {get;set;}
        public List<Contact> selectedContacts = new List<Contact>();
        public List<oppwrapper> o2 {get;set;} 
        Opportunity oppNew = new Opportunity();


        public Checkbox_Class(ApexPages.StandardController con) {
            controller = con;
            obj1 = new Object1__c();  
            accountqry = new Account(); 
            contactList = new List<contactwrapper>();
        }

        public void init() {

            if(obj1.Account__c != null) {
                accountqry = [SELECT Id, Name, Phone, Type FROM Account WHERE Id =: obj1.Account__c];
                contactList = getContacts();
            }

            else {
                obj1.clear();
                accountqry.clear();
                contactList.clear();
            }
        }

        public List<contactwrapper> getContacts()
        {

            for(Contact c : [select Id, FirstName, LastName from Contact WHERE AccountId =:  obj1.Account__c]) {
                contactList.add(new contactwrapper(c));
            }    
            return contactList;
        }

        public PageReference twoMethods() {
            getSelected();
            GetSelectedContacts();
            createOpp();
            return null;
        }

        public PageReference getSelected()
        {

            selectedContacts.clear();
            for(contactwrapper conwrapper : contactList)
            if(conwrapper.selected == true)
            selectedContacts.add(conwrapper.con);
            return null;
        }

        public List<Contact> GetSelectedContacts()
        {

            return selectedContacts;

        } 


        public void createOpp() {

            List<oppwrapper> o2 = new List<oppwrapper>();

            if(selectedContacts.size() > 0) {

                for(Contact con : selectedContacts) {

                    o2.add(new oppwrapper(oppNew,false));

                }

            }    

        }

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

            public contactwrapper(Contact c)
            {
                con = c;
                selected = false;
            }
        }

        public class oppwrapper 
        {
            public Opportunity opp {get;set;}
            public Boolean selected2 {get;set;}

            public oppwrapper(Opportunity o, Boolean s) 
            {
                opp = o;
                selected2 = s; 
            }
        }
    }
0

You have this block in createOpp():

for(Contact con : selectedContacts) {
    List<Opportunity> o2 = new List<Opportunity>();
    for(Opportunity o3 : o2) {
        opportunityList.add(new oppwrapper(o3,false));
    }     
}

Since o2 is always new list with no values in it, there's nothing to iterate over. The line where you create a new oppwrapper() is not going to be run, so no opportunity wrappers are being created.

  • Please see my edited post. I included my updated code. – eagerlearner Nov 8 '16 at 16:49

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.