0

I want to create a list of tasks, events, files and notes, so the sales man have just one list for all activities. The list works, but I still have a few questions.

I add to each list a manual field as oName which should contain the objectname. It works so far, but I can't display the oName in my list at the visual force, because I got an error --> Variable does not exist: oName , I don't know why? How can I sort the wrapperlist in the controller by createddate? is there an easy way to sort directly in the table?

public class DokuBox_5 {
    
    public List<wrapper> wrapperList {get; set;}
    public String currentRecordId {get; set;}
    
    public DokuBox_5() {
        currentRecordId = ApexPages.currentPage().getParameters().get('id');
      
        if(wrapperList == null) {
            wrapperList = new List <wrapper>();
            
            for(Task a: [select Id, Subject, CreatedDate from Task where WhoId =: currentRecordId limit 10]) {
                wrapperList.add(new wrapper(a, 'Task'));
            }
            for(Event b: [select Id, Subject, CreatedDate from Event where WhoId =: currentRecordId limit 10]) {
                wrapperList.add(new wrapper(b, 'Event'));
            }
            for(ContentDocumentLink d: [SELECT Id, ContentDocumentId, ContentDocument.Title, ContentDocument.createdDate FROM ContentDocumentLink WHERE LinkedEntityId =: currentRecordId limit 10]) {
                wrapperList.add(new wrapper(d, 'Note/File'));
            }
        }
    }
    
    public class Wrapper implements Comparable {
        
        public Task at {get; set;}
        public Event be {get; set;}
        public ContentDocumentLink dc {get; set;}
        
        public wrapper(Task a, String oName) { 
            at = a;
        }      
        public wrapper(Event b, String oName) {
            be = b;
        }
        public wrapper(ContentDocumentLink d, String oName) {
            dc = d;
        }
        
        public String getSubject() {
            if (at != null) return at.Subject;
            else if (be != null) return be.Subject;
            else if (dc != null) return dc.ContentDocument.Title;
            else if (dc != null) return null;
            else return '';
        }      
        public Datetime getCreatedDate() {
            if (at != null) return at.CreatedDate;
            else if (be != null) return be.CreatedDate;
            else if (dc != null) return dc.ContentDocument.CreatedDate;            
            else if (dc != null) return null;            
            else return null;
        }     
        public String getId() {
            if (at != null) return at.Id;
            else if (be != null) return be.Id;
            else if (dc != null) return dc.ContentDocumentId;            
            else return '';
        }               
        public String getoName() {
            if (at != null) return at.oName;
            else if (be != null) return be.oName;
            else if (dc != null) return dc.oName;            
            else return '';
        }            
      
        public Integer compareTo(Object o) {
            Wrapper that = (Wrapper) o;
            Integer result = this.getSubject().compareTo(that.getSubject());          
            if (result != 0) {
                return result;
            } else {
                if (this.getCreatedDate() > that.getCreatedDate()) return -1;
                else if (this.getCreatedDate() < that.getCreatedDate()) return 1;
                else return 0;

            }
        }
    }   
}
    <apex:form >
        <apex:pageBlock >             
                <apex:pageBlockTable value="{!wrapperList}" var="wl" title="all activities" styleClass="dataTable">
                    <apex:column headerValue="Date">
                        <apex:outputText value="{0,date,dd'.'MM'.'yyyy HH:mm:ss}"> <apex:param value="{!wl.createdDate}"/> </apex:outputText>                       
                    </apex:column>
                    <apex:column headerValue="Title" value="{!wl.subject}" />
                    <apex:column headervalue="LINK"> 
                        <apex:outputLink target="_blank" value="/{!wl.Id}">Details</apex:outputLink> 
                    </apex:column>
                </apex:pageBlockTable>                                            
        </apex:pageBlock>
    </apex:form> 

1 Answer 1

2

You're passing oName to the wrapper constructor, but you are not storing it, so it will be lost.
Moreover there is no oName field on Task, Event and ContentDocumentLink, that's why it doesn't compile due to "Variable does not exist" error.

If you define the properties you need in your wrapper class, instead of relying on SObjects, you can remove all those if-else in your get methods.

In your compareTo method you're checking also subject field, but since you've asked to sort by CreatedDate, I removed it.

Wrapper class.

public class Wrapper implements Comparable {

    private final String objectLabel;
    private final Id id;
    private final String subject;
    private final DateTime createDate;

    public wrapper(Task t, String oName) { 
        objectLabel = oName;
        id = t.Id;
        subject = t.Subject;
        createDate = t.CreatedDate;
    }
    
    public wrapper(Event e, String oName) {
        objectLabel = oName;
        id = e.Id;
        subject = e.Subject;
        createDate = e.CreatedDate;
    }

    public wrapper(ContentDocumentLink f, String oName) {
        objectLabel = oName;
        id = f.ContentDocumentId;
        subject = f.ContentDocument.Title;
        createDate = f.ContentDocument.CreatedDate;
    }

    public Id getId() {
        return id;
    }

    public String getSubject() {
        return subject;
    }

    public DateTime getCreatedDate() {
        return createDate;
    }

    public String getObjectLabel() {
        return objectLabel;
    }            

    // Sort in ascending order
    public Integer compareTo(Object compareTo) {
        Wrapper compareToActivity = (Wrapper) compareTo;
        DateTime compareToCreateDate = compareToActivity.getCreatedDate();
        if (createDate > compareToCreateDate) {
            return 1;
        }
        if (createDate < compareToCreateDate) {
            return -1;
        }
        return 0;
    }
}

If you want to sort the list in descending order, please switch the returned values in the compareTo method.

if (createDate > compareToCreateDate) {
    return -1;
}
if (createDate < compareToCreateDate) {
    return 1;
}

Once the wrapper's list has been filled, you should call .sort() method.

public class DokuBox_5 {
    
    public List<Wrapper> wrapperList {get; set;}
    public String currentRecordId {get; set;}
    
    public DokuBox_5() {
        currentRecordId = ApexPages.currentPage().getParameters().get('id');
      
        if(wrapperList == null) {
            wrapperList = new List<Wrapper>();
            
            for(Task a: [select Id, Subject, CreatedDate from Task where WhoId =: currentRecordId limit 10]) {
                wrapperList.add(new Wrapper(a, 'Task'));
            }
            for(Event b: [select Id, Subject, CreatedDate from Event where WhoId =: currentRecordId limit 10]) {
                wrapperList.add(new Wrapper(b, 'Event'));
            }
            for(ContentDocumentLink d: [SELECT Id, ContentDocumentId, ContentDocument.Title, ContentDocument.createdDate FROM ContentDocumentLink WHERE LinkedEntityId =: currentRecordId limit 10]) {
                wrapperList.add(new Wrapper(d, 'Note/File'));
            }
            
            // records will be sorted in ascending order
            wrapperList.sort();
        }
    }
}
1
  • Many Thanks RubenDG for your effort and your explanations
    – peXeq
    Jul 20, 2021 at 11:42

You must log in to answer this question.

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