0

I was able to add selected records from Vf page back to relatedlist of a record. Please see the link below where the complete code is posted.

Assign selected records to relatedlist using apex

This is working fine for Accounts and contacts. But when I try the same for custom objects, I am getting errors. (one of them is Constructor not defined).

Please help me in making changes to the code so that it works fine for custom objects also.

Looking forward to hear from you.

Below is my code.

Visualforce

<apex:page controller="CheckAllUsingJavascriptController2" id="muCt"  sidebar="false">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockButtons >
            <apex:commandButton value="Process Selected" action="{!processSelected}" rerender="table"/>
        </apex:pageBlockButtons>
        <!-- In our table we are displaying the cContact records -->
        <apex:pageBlockTable value="{!cobject}" var="c" id="table">
            <apex:column >
                <!-- This is our selected Boolean property in our wrapper class -->
                <apex:inputCheckbox value="{!c.selected}"/>
            </apex:column>
            <!-- This is how we access the contact values within our cContact container/wrapper -->
            <apex:column value="{!c.con.Text1__c}" />
           <!-- <apex:column value="{!c.con.Email}" />
            <apex:column value="{!c.con.Phone}" /> -->
            </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>

Controller

public class CheckAllUsingJavascriptController2{
    public CheckAllUsingJavascriptController2(ApexPages.StandardController controller) {


    }


    public CheckAllUsingJavascriptController2() {

    }


    public CheckAllUsingJavascriptController2(ApexPages.StandardSetController controller) {

    }


    //Our collection of the class/wrapper objects cContact 
    public List<cCobject> cobjectList {get; set;}
    public String currentRecordId {get;set;}
    //This method uses a simple SOQL query to return a List of Contacts
    public List<cCbject> getcbjects() {
    currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
        if(cobjectList == null) {
            cobjecttList = new List<cCobject>();
            for(cCobject c: [select Text1__c from Custom_Obj1__c limit 10]) {
                // As each contact is processed we create a new cContact object and add it to the contactList
                cobjectList.add(new cCobject(c));

            }
        }
        return cobjectList;
    }


    public PageReference processSelected() {

                //We create a new list of Contacts that we be populated only with Contacts if they are selected
        List<Custom_Obj1> selectedContacts = new List<Custom_Obj1>();

        //We will cycle through our list of cContacts and will check to see if the selected property is set to true, if it is we add the Contact to the selectedContacts list
        for(cContact cCon: getContacts()) {
            if(cCon.selected == true) {
                selectedContacts.add(cCon.con);


            }
        }

        // Now we have our list of selected contacts and can perform any type of logic we want, sending emails, updating a field on the Contact, etc
        System.debug('These are the selected Contacts...');
        for(Custom_Obj1 con: selectedContacts) {
      // con.AccountId='0012800000hcTJh';
        con.AccountId=currentRecordId;
            system.debug(con);
        }
    update(selectedContacts);


        cobjectList=null; // we need this line if we performed a write operation  because getContacts gets a fresh list now
       // return null;
       PageReference pageRef = new PageReference(ApexPages.currentPage().getParameters().get('retURL'));     
       return pageref.setRedirect(true);    
  // PageReference retPage = new PageReference('/'+apexPages.CurrentPage().getParameters().get('0012800000hcM69')); 

//return retPage;

}
    // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
    public class Custom_Obj1 {
        public Contact con {get; set;}
        public Boolean selected {get; set;}

        //This is the contructor method. When we create a new cContact object we pass a Contact that is set to the con property. We also set the selected value to false
        public Custom_Obj1(Custom_Obj1 c) {
            con = c;
            selected = false;
         PageReference pageRef = new PageReference(ApexPages.currentPage().getParameters().get('retURL')); 


        }
    }
}

Regards, Sfdcdev

2
  • Please post the code that you are using. I know that it will be similar to what you previously posted but without sight of the specific code and errors it is harder to help you.
    – Dave Humm
    Jul 11 '16 at 7:55
  • Hi Dave, I have added code in the question. Also Object Name is "Custom_Obj1". and API Name is "Custom_Obj1__c".
    – sfdcdev
    Jul 11 '16 at 12:16
0

I was using the Label for my Custom Object in place of its API Name and vice versa, which caused the above errors, including constructor not defined.

4
  • 3
    Could you please add some details about the issue? what you were doing wrong? so other can find your answer useful.
    – Ratan Paul
    Jul 12 '16 at 11:20
  • Hi @Ratan. I was using object name in place of API name and API name in place of Object name, which was causing the issues and constructor not defined error was shown. I carefully checked it and was able to resolve by myself.
    – sfdcdev
    Jul 12 '16 at 13:01
  • @sfdcdev I have added your comment in your answer.. always try to more details so other can understand the issue and there fixes. Thanks!
    – Ratan Paul
    Jul 12 '16 at 13:04
  • 1
    @Ratan Sure. Sorry for not adding the details in previous comments.
    – sfdcdev
    Jul 12 '16 at 13:08

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.