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
Frances Day 9Frances Day 9 

add button for related list of custom object

Hello there!
I'm just starting to learn about Apex, SOQL and classes. But I need to create a custom button on a related list to add related objects to the list. 
Using the code in this link: https://developer.salesforce.com/forums?id=906F0000000BKaUIAW, seems to have gotten me most of the way there, but I'm referencing a custom object, not a standard one, and I'm wondering if that could be the source of the problem?

I'm getting two errors:
Line 12 Constructor not defined [PublicationControllerClass.Publications__c].(PublicationControllerClass.Publications__c)
Line 42 Invalid character in Identifer Publications__c

public class PublicationControllerClass {

    //Our collection of the class/wrapper objects cPublication 
    public List<Publications__c> publicationList {get; set;}

    //This method uses a simple SOQL query to return a List of Publications
    public List<Publications__c> getPublications() {
        if(publicationList == null) {
            publicationList = new List<Publications__c>();
            for(Publications__c c: [select Id, Name, Publication_Type__c from Publications__c limit 20]) {
                // As each publication is processed we create a new Publication object and add it to the publicationList
                 publicationList.add(new Publications__c(c));
            }
        }
        return publicationList;
    }


    public PageReference processSelected() {

                //We create a new list of Publications that will be populated only with Publications if they are selected
        List<Publications__c> selectedPublications = new List<Publications__c>();

        //We will cycle through our list of cPublications and will check to see if the selected property is set to true, if it is we add the Publication to the selectedpublications list
        for(Publications__c Pub: getPublications()) {
            if(Pub.selected == true) {
                selectedPublications.add(Pub.Pub);
            }
        }

        // Now we have our list of selected Publications and can perform any type of logic we want
        System.debug('These are the selected Publications...');
        for(Publications__c pub: selectedPublications) {
            system.debug(pub);
        }
        publicationList=null; // we need this line if we performed a write operation  because getPublications gets a fresh list now
        return null;
    }


    // 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 object Publications and a Boolean value
    public class Publications__c {
        public Publications__c pub {get; set;}
        public Boolean selected {get; set;}

        //This is the contructor method. When we create a new cPublication object we pass a Publication that is set to the pub property. We also set the selected value to false
        public Publications__c (Publication c) {
            pub = c;
            selected = false;
        }
    }
}

I'm sure I'm missing something obvious, and appreciate any help.
Thanks!
 
Best Answer chosen by Frances Day 9
Zachery EngmanZachery Engman
Hi Frances - it appears you are trying to name the inner-wrapper class the same as the custom object.  I updated the references to the class as such:

public class PublicationControllerClass {
    
    //Our collection of the class/wrapper objects cPublication 
    public List<PublicationWrapper> publicationList {get; set;}
    
    //This method uses a simple SOQL query to return a List of Publications
    public List<PublicationWrapper> getPublications() {
        if(publicationList == null) {
            publicationList = new List<PublicationWrapper>();
            for(Publications__c c: [select Id, Name, Publication_Type__c from Publications__c limit 20]) {
                // As each publication is processed we create a new Publication object and add it to the publicationList
                publicationList.add(new PublicationWrapper(c));
            }
        }
        return publicationList;
    }
    
    
    public PageReference processSelected() {
        
        //We create a new list of Publications that will be populated only with Publications if they are selected
        List<Publications__c> selectedPublications = new List<Publications__c>();
        
        //We will cycle through our list of cPublications and will check to see if the selected property is set to true, if it is we add the Publication to the selectedpublications list
        for(PublicationWrapper Pub: getPublications()) {
            if(Pub.selected == true) {
                selectedPublications.add(Pub.pub);
            }
        }
        
        // Now we have our list of selected Publications and can perform any type of logic we want
        System.debug('These are the selected Publications...');
        for(Publications__c pub: selectedPublications) {
            system.debug(pub);
        }
        publicationList=null; // we need this line if we performed a write operation  because getPublications gets a fresh list now
        return null;
    }
    
    
    // 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 object Publications and a Boolean value
    public class PublicationWrapper {
        public Publications__c pub {get; set;}
        public Boolean selected {get; set;}
        
        //This is the contructor method. When we create a new cPublication object we pass a Publication that is set to the pub property. We also set the selected value to false
        public PublicationWrapper (Publications__c c) {
            pub = c;
            selected = false;
        }
    }
}

All Answers

Zachery EngmanZachery Engman
Hi Frances - it appears you are trying to name the inner-wrapper class the same as the custom object.  I updated the references to the class as such:

public class PublicationControllerClass {
    
    //Our collection of the class/wrapper objects cPublication 
    public List<PublicationWrapper> publicationList {get; set;}
    
    //This method uses a simple SOQL query to return a List of Publications
    public List<PublicationWrapper> getPublications() {
        if(publicationList == null) {
            publicationList = new List<PublicationWrapper>();
            for(Publications__c c: [select Id, Name, Publication_Type__c from Publications__c limit 20]) {
                // As each publication is processed we create a new Publication object and add it to the publicationList
                publicationList.add(new PublicationWrapper(c));
            }
        }
        return publicationList;
    }
    
    
    public PageReference processSelected() {
        
        //We create a new list of Publications that will be populated only with Publications if they are selected
        List<Publications__c> selectedPublications = new List<Publications__c>();
        
        //We will cycle through our list of cPublications and will check to see if the selected property is set to true, if it is we add the Publication to the selectedpublications list
        for(PublicationWrapper Pub: getPublications()) {
            if(Pub.selected == true) {
                selectedPublications.add(Pub.pub);
            }
        }
        
        // Now we have our list of selected Publications and can perform any type of logic we want
        System.debug('These are the selected Publications...');
        for(Publications__c pub: selectedPublications) {
            system.debug(pub);
        }
        publicationList=null; // we need this line if we performed a write operation  because getPublications gets a fresh list now
        return null;
    }
    
    
    // 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 object Publications and a Boolean value
    public class PublicationWrapper {
        public Publications__c pub {get; set;}
        public Boolean selected {get; set;}
        
        //This is the contructor method. When we create a new cPublication object we pass a Publication that is set to the pub property. We also set the selected value to false
        public PublicationWrapper (Publications__c c) {
            pub = c;
            selected = false;
        }
    }
}
This was selected as the best answer
Frances Day 9Frances Day 9
Thank you! That worked! I really appreciate the help. 
Frances