+ Start a Discussion

Error in Dynamic SOQL

hi ,

 i m creating a daynamic query .its thorughing error

System.QueryException: unexpected token: '.'


Query is like this


soql = 'p.Name, p.Location__c From Property__c p where Name != null';
                string[] locations = t.Location_Tenant__c.split(';',0);
                list<String> loc= new list<String>();
                for(string l :locations){
                    loc.add( '\''+l+'\'');
                   if (loc != null && loc.size() > 0){                                   
                    soql += ' and Location__c in '+loc ;                
                pList = Database.query(soql );


|DEBUG|--soql---Select p.Name, p.Location__c, p.Id From Property__c p where Location__c in ('Art Museum', 'Avenue of the Arts', 'Bella Vista', 'Brewerytown', 'Chestnut Hill', 'Chinatown', 'East Falls', 'East Oak Lane', 'Fairmount', 'Fishtown', ...)

here i m quering for location.its working fine till the last vlaue in location(Fishtown). but after adding one more location it gives me above error.


I m asuming that the dot in the query is giving me error, but should not give.


How can i resolve this error..?






Ronak PatelRonak Patel

Just Check the Next Value Not Contain Any Special Character like(,' " / or Any)...


You dont have to do this concatanation. even this is a query string you can use first level parameters like


WHERE Location__c IN :locList

WHERE Location__c =:locId


but you cant do something like

WHERE Location__c =:loc.Id