0

I am working on a custom controller to sort the below apex:repeat list in Desc order by BMCServiceDesk__date__c or CreatedDate. Part of my VF email template and Controller code is below. I am getting a compile error of "Return value must be of type: List" in my dev console. Thanks everyone for your help, much appreciated.

// apex:repeat component to call related list 'Notes' from Incident History object, but the order it display in the email is Ascending order (the oldest on top), I want to sort it in Descending order by the Date and time field BMCServiceDesk__date__c where the newest note on top. //

Here are my revised codes:

VF page


<tr>
    <th colspan="4"><strong>Update</strong></th>
    <c:sortedNoteController ></c:sortedNoteController>
</tr>

Class


public class sortedController {
        public ID incidentId {get; set;}
      public List<BMCServiceDesk__IncidentHistory__c> getIncidentHistories() {
         Return [       
                 SELECT BMCServiceDesk__note__c
                 FROM BMCServiceDesk__IncidentHistory__c
WHERE BMCServiceDesk__actionId__c = 'Notes' AND BMCServiceDesk__incidentId__c =:  this.incidentId
                    ORDER BY BMCServiceDesk__date__c DESC
        ];
    }
}

Component


<apex:component controller="sortedController" access="global">
    <apex:attribute name="IncidentHistoryID" type="ID" assignTo="{!incidentId}" description="Note ID"/>
        <table>
            <apex:repeat value="{!IncidentHistories}" var="CurrItem">
                <apex:outputPanel rendered="{!CurrItem.BMCServiceDesk__actionId__c = 'Notes'}"> 
                     <tr>
                         <td colspan="4" class="hdr"><pre>{!CurrItem.BMCServiceDesk__note__c}</pre>&nbsp;
                             <apex:outputText value="{0,date,MM/dd/yyyy h:mm a}"> 
                             <apex:param value="{!CurrItem.BMCServiceDesk__date__c - (5/24)}"/></apex:outputText>
                         </td>
                    </tr>    
                </apex:outputPanel> 
        </apex:repeat>     
    </table>                                                                                        
</apex:component>
2

The problem that I can see is types of lists, your function says getNotes so, Assuming you want list of Note.(If in case you want to return BMCServiceDesk__IncidentHistory__c, replace Note with BMCServiceDesk__IncidentHistory__c)

  1. Query should be made on Notes in FROM clause of query.
  2. Return type of function must match with data type of list you are returning . In this case you are returning Notes.

    public List<Note> getNote() { 
    
        Return [ 
        SELECT Id, BMCServiceDesk__note__c, BMCServiceDesk__date__c 
        FROM Note 
        WHERE BMCServiceDesk__actionId__c = 'Notes' 
        AND BMCServiceDesk__incidentId__c = :this.attributeNoteID 
        ORDER BY BMCServiceDesk__date__c DESC ]; }
    
  • This answer would be improved if you added some explanation about exactly how this solves the issue stated in the question. – Derek F Jul 24 '16 at 2:29
  • Thanks for suggestions, Added some explanation for quick understanding purpose. – Ishwar Mete Jul 24 '16 at 3:12
  • @Qdo does this solves your problem? – Ishwar Mete Jul 24 '16 at 3:39
  • It doesn't, error now is "No such column 'BMCServiceDesk__note__c" on entity 'Note'." I am just trying to get the note column back from object BMCServiceDesk__IncidentHistory__c Order by BMCServiceDesk__date__c DESC (or the CreateDate field if I can reference it) in my visualforce email template. – Qdu Jul 24 '16 at 4:33
  • @Qdu - I think your problem stems from the type of data your method is trying to return. You've indicated that your getNotes method should return a List of an SObject called Note. However your SOQL query isn't running on the Note object. I posted a new answer below, based on my assumption of your use case. Please let me know if that helps. – Morgan Marchese Jul 24 '16 at 6:07
0

From your SOQL you are querying BMCServiceDesk__IncidentHistory__c object, whereas getNote() returns List<Note>

  • How would I rephrase the query? – Qdu Jul 24 '16 at 0:52
0

If I understand your problem/comments correctly, I believe that your problem is that you are trying to return the value of a single field, but you've indicated the return type of the getNote method to be a List of Notes (<List>Note).

Since your SOQL query in your return line isn't running on the Note object, and isn't returning a List of Notes, it can't return the correct List type, therefore you get the error "Return Value must be of type List"

Sounds to me like what you're trying to return is actually just a List of the contents of a specific field called 'Notes'. In this case, your list should be of Strings, not Note. When you do a List<>, the system assumes you are creating a List of an SObject (Account, Contact, Case, etc) or a Primitive (String, Id, Decimal, etc). In this case, you are making the system think you are trying to get a List of records from an SObject called Note, which is incorrect. You're really just trying to return a list of Strings.

Try this:

public class sortedController {

    public ID attributeNoteID {get; set;}

    public List<String> getNote() {

       Return [         
            SELECT BMCServiceDesk__note__c
            FROM BMCServiceDesk__IncidentHistory__c
            WHERE BMCServiceDesk__actionId__c = 'Notes' AND BMCServiceDesk__incidentId__c = :this.attributeNoteID
            ORDER BY BMCServiceDesk__date__c DESC
        ];

    }
}

Notice the following changes in the code:

  1. I changed your List to a List of Strings (List<String>)
  2. I removed the Id field from your SOQL query. Id is always included when you get data back from SOQL, so you generally don't need to include it in the list of fields in your query.
  3. I also removed BMCServiceDesk__date__c from your SELECT in the query. 2 reasons for this, First - in this case we are returning a String, so we only want to return the one column that will become the String. Second - you don't need to have that field in your SELECT if you just want to use it as a filter in your WHERE, you can still filter based off of the field without returning the values for it.
  • I edited my code but my test do not display any data. – Qdu Jul 25 '16 at 4:11
  • I saved the edited codes above. – Qdu Jul 25 '16 at 4:28

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.