0

There's this error displaying every time I save the apex class: Error: Compile Error: Constructor not defined:[Checkbox_Class.oppwrapper].<Constructor>(Boolean, Opportunity)

The apex class and visualforce page below displays all contact details related to a specific searched account, as well as opportunity table row field with corresponding checkbox for each selected contact.

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 List<oppwrapper> tempWrap2 {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>();
        tempWrap1 = new List<oppwrapper>();
        tempWrap2 = 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() {

        for(oppWrapper ow1 : opportunityList) {
            tempWrap1.add(ow1);
        }

        if(selectedContacts.size() > 0) {

            for(Contact con : selectedContacts) {

                for(Opportunity o2 : tempWrap1) {

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

                }     

            }

        }    

    }

    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="{!GetSelected}" rerender="Selected_PBS,oppSec"/></apex:inputCheckbox>-->
                        <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 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<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

2 Answers 2

1

You swapped the order of your parameters. In your constructor, you have (Opportunity, Boolean), but when calling it, you use (Boolean, Opportunity).

Works

new OppWrapper(someOpportunity, false);

Fails

new OppWrapper(false, someOpportunity);
11
  • Hi, I followed your suggestion and used this line instead new OppWrapper(someOpportunity, false);. But another error occurred : Loop variable must be of type Checkbox_Class.oppwrapper, so instead of Opportunity, I used oppwrapper to the for loop : for(oppwrapper o2 : tempWrap1) but every time I do this, the first error goes back. Nov 5, 2016 at 12:48
  • it displays this error: Constructor not defined: [Checkbox_Class.oppwrapper].<Constructor>(Checkbox_Class.oppwrapper, Boolean) , reversing the original order of the first error Nov 5, 2016 at 12:53
  • You can't instantiate it with another wrapper, it has to be an actual Opportunity record. I'm not sure why you have so many lists flying all over the place. Try to simplify your code
    – Adrian Larson
    Nov 5, 2016 at 13:06
  • I made some adjustments on the code. There's no error any more but every time I click one of the selected contacts, there's no empty opportunity row displayed. Would you mind checking it? Please see edited post. Nov 5, 2016 at 14:02
  • 1
    The code you've posted never actually queries opportunity. I suggest you update your post weight your newest code. Or better yet, ask a new question to resolve that issue, since it is really a new one.
    – Adrian Larson
    Nov 5, 2016 at 14:04
0

@eagerlearner - I skimmed over your method createOpp, in this method you are instantiating o2 again. The scope of this method variable o2 hides the class variable which means that o2 which you have declared in the very beginning is left empty and that is what is being referred to from vf page.

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

Comment out that line from the method createOpp. To debug the contents on the vf page you could type {!o2} to see what is within & later remove it.

Even though it might look clumsy, you will still get an idea of whats going on.

6
  • I commented this line List<oppwrapper> o2 = new List<oppwrapper>(); from the createOpp() method, and debugged through this line System.debug('o2' + o2);. On selecting one of the contacts, here's the result line Expected: 2, Actual : o2. Now I am not sure why there's the number. Nov 8, 2016 at 22:34
  • I do not think that is the output of System.debug. It looks like the output of some assert statement. I was suggesting to have{!o2} on the vf page but this is also a good way of knowing whats happening. I am guessing you must be asserting the size. Try printing {!o2} in the vf page before ' <apex:pageBlockTable value="{!o2}" var="o">'
    – SF Learner
    Nov 8, 2016 at 22:38
  • Is this correct? {!o2} <apex:pageBlockTable value="{!o2}" var="o"> Sorry for asking, I just learned from you that there's this kind of knowing what's happening right on the vf page. By the way, I already added this {!o2}, and removed the assertion from apex class. There's no error whatsoever, but empty row with checkbox still doesn't show. Nov 9, 2016 at 2:51
  • Yes. something like that should do, just try it anywhere that the vf page allows you to.
    – SF Learner
    Nov 9, 2016 at 3:10
  • I noticed that every time I preview the page, there's this empty brackets [] appearing above the opportunity pageblock table. What does this mean? Nov 9, 2016 at 3:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .