+ Start a Discussion
Dave BerenatoDave Berenato 

GetUserID in String Select Query

I need to reference a put a Select Query into a String, but it won't process system data like getUserId() (or system.now, although I'm not sure because the error message centers on getUserId()).

Here's what I have:
 
String UserID = UserInfo.getUserId();

String myRequest = 'SELECT id, Owner.Id, type, subject,'+
                ' FROM Event' + 
                ' WHERE Owner.Id = '+UserId+' AND RecordTypeId = \'0126A0000004Qle\' And (ActivityDateTime = LAST_N_DAYS:30 OR (ActivityDate = TODAY And ActivityDateTime < ' + system.now() + ' ))';
Error message is "System.QueryExecption: unexpected token:" and then my UserId.
 
Best Answer chosen by Dave Berenato
Tad Aalgaard 3Tad Aalgaard 3
String UserID = UserInfo.getUserId();

String dateTimeFormat = DateTime.now().format('yyyy-MM-dd\'T\'hh:mm:ss\'Z\'');

String myRequest = 'SELECT  id, Owner.Id, type, subject'+
                ' FROM Event' + 
                ' WHERE Owner.Id = \''+UserId+'\' AND RecordTypeId = \'0121D0000008Tll\' And (ActivityDateTime = LAST_N_DAYS:30 OR (ActivityDate = TODAY And ActivityDateTime <  ' + dateTimeFormat + ' ))';

System.debug(myRequest);

List<sObject> sobjList = Database.query(myRequest);

System.debug(sobjList);

 

All Answers

RD@SFRD@SF
Hi Dave

I think its cause the your UserId needs to be passed in single quotes, notice the way you have passed the Recordtype
RecordTypeId = \'0126A0000004Qle\'
You ll also have to do the same for userid
Owner.Id = \''+UserId+'\' AND

Hope it helps
RD​



 
Dave BerenatoDave Berenato
Hi RD, I tried:
 
' WHERE Owner.Id = \''+UserId+'\' AND RecordTypeId = \'0126A0000004Qle\' And (ActivityDateTime = LAST_N_DAYS:30 OR (ActivityDate = TODAY And ActivityDateTime < ' + system.now() + ' ))' +

And got an Error Message of: 
System.QueryException: line 1:692 no viable alternative at character ' '

I tried putting a space between the single quotes. Does Salesforce distinguish between an open quote and an end quote?
 
Dave BerenatoDave Berenato
Actually the error might not be the UserID.

I tried:
 
' WHERE Owner.Id = \''+UserId+'\' AND RecordTypeId = \'0126A0000004Qle\' And (ActivityDateTime = LAST_N_DAYS:30 OR (ActivityDate = TODAY And ActivityDateTime < \''+ system.now() + '\'))' +

And got:

System.QueryException: value of filter criterion for field 'ActivityDateTime' must be of type dateTime and should not be enclosed in quotes

But if I take away the quotes and leave just
 
ActivityDateTime < '+ system.now() + '))'

I go back to the error message about the no viable alternative to character ''

What do you think?
Tad Aalgaard 3Tad Aalgaard 3
What's with the extra comma after subject on the first line?  Remove it or it will give you an error.

String UserID = UserInfo.getUserId(); String myRequest = 'SELECT id, Owner.Id, type, subject,' +
' FROM Event' + ' WHERE Owner.Id = '+UserId+' AND RecordTypeId = \'0126A0000004Qle\' And (ActivityDateTime = LAST_N_DAYS:30 OR (ActivityDate = TODAY And ActivityDateTime < ' + system.now() + ' ))';
 
Tad Aalgaard 3Tad Aalgaard 3
String UserID = UserInfo.getUserId();

String dateTimeFormat = DateTime.now().format('yyyy-MM-dd\'T\'hh:mm:ss\'Z\'');

String myRequest = 'SELECT  id, Owner.Id, type, subject'+
                ' FROM Event' + 
                ' WHERE Owner.Id = \''+UserId+'\' AND RecordTypeId = \'0121D0000008Tll\' And (ActivityDateTime = LAST_N_DAYS:30 OR (ActivityDate = TODAY And ActivityDateTime <  ' + dateTimeFormat + ' ))';

System.debug(myRequest);

List<sObject> sobjList = Database.query(myRequest);

System.debug(sobjList);

 
This was selected as the best answer