+ Start a Discussion
Team WorksTeam Works 

How to concatenate strings to form a Query

Hello Guys

Again Stuck with the concatenation :

private final String SOQL_RECENT_REC = 'SELECT LastViewedDate,Type,UserRoleId FROM RecentlyViewed';
private final String CONDITION_USER_SORT = ' Order By LastViewedDate DESC LIMIT 20';
List<RecentlyViewed> recentViewed= new List<RecentlyViewed>();
recentViewed = Database.query(SOQL_RECENT_REC +' WHERE Type = \'Account \' ' + CONDITION_USER_SORT);

Should run a query like the following :
recentViewed = [SELECT LastViewedDate,Type,UserRoleId FROM RecentlyViewed Where Type='Account' Order By LastViewedDate DESC LIMIT 20];

Many Thanks !!
Best Answer chosen by Team Works
Team WorksTeam Works
Solved it guys: Thanks for putting your efforts but here is the solution
\

recentViewed = Database.query(SOQL_RECENT_REC +' WHERE Type = \'Account\' OR Type = \'Contact\' ' + CONDITION_USER_SORT);

All Answers

Dominic Blythe 18Dominic Blythe 18
The code you posted does appear to do exactly what you want. What's the problem?
Ramesh KallooriRamesh Kalloori
Database.query(SOQL_RECENT_REC +' WHERE Type = Account ' + CONDITION_USER_SORT);

Dominic Blythe 18Dominic Blythe 18
Ramesh you haven't quoted Account, so I dont think yours will work.
Ramesh KallooriRamesh Kalloori
Please go through the below code.

Database.query(SOQL_RECENT_REC +' WHERE Type = \''Account '\ ' + CONDITION_USER_SORT);

thanks,
Ramesh
Team WorksTeam Works
Ramesh Thanks but it doesnt work..Please suggest
Team WorksTeam Works
Solved it guys: Thanks for putting your efforts but here is the solution
\

recentViewed = Database.query(SOQL_RECENT_REC +' WHERE Type = \'Account\' OR Type = \'Contact\' ' + CONDITION_USER_SORT);
This was selected as the best answer
Team WorksTeam Works
A Selfie:)