Suppose I have one code, HI, which is mapped to these values: HIRT, HIFS,HISL,HIST,HIGC,HIAN,HIIS,HICV,HISR,HOLI. When I select HI those values are are passed into a dynamic soql query. For a small list of values, the dynamic query is working. However, a longer list of values causes an error.
SELECT
Id, Name, Hotel_Location_Number__c, Hotel_Inn_Code__c, Future_Brand__c,
Previous_Brand__c, Hotel_Name__c, Hotel_City__c, Hotel_State__c, Hotel_Location__c,
Hotel_Country__c, Project_Type__c, Project_Status_Code__c, Lic_Term__c,
Hotel_Status__c, Brand_Group_Code__c
FROM Project__c
WHERE Name != null
AND Future_Brand__c IN('HIRT', 'HIFS', 'HISL', 'HIST', 'HIGC', 'HIAN', 'HIIS', 'HICV', 'HISR', 'HOLI', ...)
ORDER BY Project_ID__c DESC
Error:
System.QueryException: expecting a colon, found '.'
My code is
List<String> futureBrandGroups=new List<String>();
Map<String,BrandMapping__c> brandCode=BrandMapping__c.getAll();
List<BrandMapping__c> brandValues=brandCode.Values();
List<BrandMapping__c> brandMapping=[select name from BrandMapping__c where Brand_Group_Code__c=:futureBrandGroup];
for(BrandMapping__c brandMappings:brandMapping)
{
String brands= '\''+ String.escapeSingleQuotes(brandMappings.name) + '\'';
futureBrandGroups.add(brands);
}
strTempSoql += 'and Future_Brand__c IN'+futureBrandGroups;
futureBrandGroup='HI' name=HIRT, HIFS,HISL,HIST,HIGC,HIAN,HIIS,HICV,HISR,HOLI.
For small values there is no problem in the soql query in my apex class. However, these longer lists of values causes an error.
Best Answer
Explanation
The core problem here is that implicit type conversion from
List<String>
toString
will cause truncation. It looks like this happens after the tenth element. You can see it for yourself by running the following script inExecute Anonymous
:The above script will yield this debug:
Solution
You need to merge in a joined string:
You want it to merge in something like:
But when using implicit type coercion, eventually the list becomes truncated. So instead it becomes:
If you are doing the query in the same context, you could instead merge in a dynamic reference: