0

I would like one list of different objects (Task, Event, Notes). In mycode I create a wrapper class for the different objects and I display the list on a visual force, but there are still 3 lists just below each other. How can I concatenate the items in one list for sorting etc.

public class DokuBox_3 {

    public List<wrapper> wrapperList {get; set;}
    
    public DokuBox_3() {
            
        if(wrapperList == null) {
            wrapperList = new List <wrapper>();
            
            for(Task a: [select Id, Subject from Task limit 10]) {
                wrapperList.add(new wrapper(a));
            }
            for(Event b: [select Id, Subject from Event limit 10]) {
                wrapperList.add(new wrapper(b));
            }
            for(Note c: [select Id, Title from Note limit 10]) {
                wrapperList.add(new wrapper(c));
            }
        }
    }
 
    public class wrapper {      
        public Task at {get; set;}
        public Event be {get; set;}
        public Note cn {get; set;}

        public wrapper(Task a) { 
            at = a;
        }

        public wrapper(Event b) {
            be = b;
        }

        public wrapper(Note c) {
            cn = c;
        }
    }
}


<apex:page controller="DokuBox_3">
    <apex:form >
        <apex:pageBlock >             
            <apex:pageblockSection title="all activities" collapsible="false" columns="2">           
                <apex:pageBlockTable value="{!wrapperList}" var="wl" id="table" title="all activities" styleClass="dataTable">
                    <apex:column >
                        <apex:facet name="header">Title</apex:facet>
                        {!wl.at.subject}
                        {!wl.be.subject}
                        {!wl.cn.title}
                    </apex:column>                   
                </apex:pageBlockTable>                                            
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>          
</apex:page>
2
  • What do you mean by "but there are still 3 lists just below each other"? You have a single List wrapperList but adding items in 3 separate places i.e within respective for loops to the SObject.
    – TSmith
    Commented Jul 16, 2021 at 10:32
  • I need a single list with all items from the 3 objects. i want to be able to sort the table by date or subject. What can I do to have all items in one list?
    – peXeq
    Commented Jul 16, 2021 at 11:07

2 Answers 2

1

You already have one list, so the place more development work is likely is in your Wrapper class. For example:

public class Wrapper implements Comparable {
  
    public Task at {get; set;}
    public Event be {get; set;}
    public Note cn {get; set;}

    public wrapper(Task a) { 
        at = a;
    }

    public wrapper(Event b) {
        be = b;
    }

    public wrapper(Note c) {
        cn = c;
    }

    public getSubject() {
        if (at != null) return at.Title;
        else if (be != null) return be.Subject;
        else if (cn != null) return cn.Title;
        else return '';
    }

    public getCreatedDate() {
        // This field will also have to be queried
        if (at != null) return at.CreatedDate;
        else if (be != null) return be.CreatedDate;
        else if (cn != null) return cn.CreatedDate;
        else return '';
    }

    public Integer compareTo(Object o) {
        Wrapper that = (Wrapper) o;
        Integer result = this.getSubject().compareTo(that.getSubject());
        if (result != 0) {
            // Sorting by subject first
            return result;
        } else {
            // ...then by created date if subjects are the same
            if (this.createdDate < that.createdDate) return -1;
            else if (this.createdDate > that.createdDate) return 1;
            else return 0;
        }
    }
}

so in the Visualforce you can just output {!wl.subject} and in the Apex code you can sort by that subject (then by the created date) using just wrapperList.sort();.

1
  • Thank Keith, Works so far, but sort is not clear, but I keep trying
    – peXeq
    Commented Jul 19, 2021 at 14:22
1

To have multiple SObject types in one list, you need to use list of sobjects.

List<SObject> s = new List<SObject>{
        new Event(),
        new Task(),
        new Note()
};

You must log in to answer this question.

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