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
V100V100 

Trying to get CreatedBy Name from Attachments

I am trying to use visualforce to show Note and Attachments as separate lists have the code below as the controller:

This works, however it returns the CreatedByID (00520000001U7ZIAA0) only and not the name.

 

  public List<Attachment> getAtt() {   
return [SELECT ID, Name, Description, Body, CreatedByID, CreatedDate FROM Attachment WHERE ParentID = :FeedbackID ORDER BY CreatedDate DESC ] ]
}

 

I then tried to do a mp but i'm missing a step or logic somewhere to get the name of the user

 

 

  public List<Attachment> getAtt() {
      List <Attachment> a = [SELECT ID, Name, Description, Body, CreatedByID, CreatedDate FROM Attachment WHERE ParentID = :FeedbackID];
      SET<String> cList = new Set<String>();
          for (Attachment ac: a) {
          cList.add(ac.CreatedByID);
      }
      List <User> u = [SELECT ID, Name FROM User WHERE ID in :cList ];
      Map<String, String> CreatedByMap = new Map<String, String>();
      for (User cb :u) {
           CreatedByMap.put(cb.Id,cb.Name);//populate map
      }
      
      return [SELECT ID, Name, Description, Body, CreatedByID, CreatedDate FROM Attachment WHERE ParentID = :FeedbackID  ORDER BY CreatedDate DESC AND CreatedByID in :CreatedByMap ];
 
  }

 This gives an error: Error: Compile Error: IN operator must be used with an iterable expression at line 50 column 141 which is 

in :CreatedByMap ];

At the limit of my knowledge here so any help much appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
MJ09MJ09

You're trying awfully hard to do something pretty simple. How about this:

 

SELECT ID, Name, Description, Body, CreatedByID, CreatedBy.Name, CreatedDate FROM Attachment

All Answers

MJ09MJ09

You're trying awfully hard to do something pretty simple. How about this:

 

SELECT ID, Name, Description, Body, CreatedByID, CreatedBy.Name, CreatedDate FROM Attachment
This was selected as the best answer
V100V100

Had to take a little while cursing, swaering, hitting my head off walls, etc before rpelying!

 

Thanks you for your VERY simple solution, just couldn't see the obvious.