You need to sign in to do that
Don't have an account?
Adam Nila
When I run this without "AND Owner.Name = :username '" it works fine. When I test it in the query editor and replace ":username" with my name, it works fine. Not sure what I'm doing wrong here?
Filter SOQL results by active user
What I want to do is pull a list of events scheduled for today but only list the events relevant to the active user.... Here's what I have so far...
public class QuickLogController { public String username{get;set;} public string finduser(){ username = Userinfo.getUserName(); return username; } public list<event> getEventsToday(){ List<event> TodaysEvents = Database.query ( 'SELECT Who.Name, Owner.id, Subject, StartDateTime, Location FROM event WHERE StartDateTime = TODAY AND Owner.Name = :username '); return TodaysEvents; } }
When I run this without "AND Owner.Name = :username '" it works fine. When I test it in the query editor and replace ":username" with my name, it works fine. Not sure what I'm doing wrong here?
List<event> TodaysEvents = new list<event>([SELECT Ownerid FROM event WHERE StartDateTime = TODAY AND Ownerid = :UserInfo.getUserId()]);
that worked for me - then add the other fields as you need.
All Answers
.... AND OwnerId = :UserInfo.getUserId()
List<event> TodaysEvents = new list<event>([SELECT Ownerid FROM event WHERE StartDateTime = TODAY AND Ownerid = :UserInfo.getUserId()]);
that worked for me - then add the other fields as you need.
I think because you were using the database.query() construct. So, your SOQL was in quotes - basically a string. So if you wanted to stay in the string - I think you'd have to do something like....
string SOQLString = 'SELECT Who.Name, Owner.id, Subject, StartDateTime, Location FROM event WHERE StartDateTime = TODAY AND OwnerId = \'' + :userInfo.getuserId() + '\'';
List<event> TodaysEvents = Database.query (SOQLString);
(althought I'm not sure how the TODAY would have worked in that code).