You need to sign in to do that
Don't have an account?
SOSL Single Quote Issue
Hi All,
I have a problem with sosl
This is working fine :-
List<List<SObject>> searchList = null; string startAddress = 'hello'; searchList = Search.query('FIND \'' +startAddress + '\' IN ALL FIELDS RETURNING Account(Id,Stage__c,Tower_ID__c,Name,ShippingStreet,ShippingCity,ShippingState,ShippingPostalCode,ShippingCountry where Tower_ID__c = :startAddress OR Name = :startAddress LIMIT 1),Opportunity(Id,Name,Vehicle_Year__c,Vehicle_Make__c,Vehicle_Model__c,Vehicle_Sub_Model__c,Claim_Status__c,Vehicle_Street_Address__c,Vehicle_City__c,Vehicle_State__c,Vehicle_Zip__c,Vehicle_Country__c where Name = :startAddress OR Opportunity__c = :startAddress LIMIT 1)'); system.debug('searchList[0]---->'+searchList[0].size()); system.debug('searchList[1]---->'+searchList[1].size());
But it is giving me an error if i write some what like this:
List<List<SObject>> searchList = null; string startAddress = 'Tom\'s Towing and Recovery'; searchList = Search.query('FIND \'' +startAddress + '\' IN ALL FIELDS RETURNING Account(Id,Stage__c,Tower_ID__c,Name,ShippingStreet,ShippingCity,ShippingState,ShippingPostalCode,ShippingCountry where Tower_ID__c = :startAddress OR Name = :startAddress LIMIT 1),Opportunity(Id,Name,Vehicle_Year__c,Vehicle_Make__c,Vehicle_Model__c,Vehicle_Sub_Model__c,Claim_Status__c,Vehicle_Street_Address__c,Vehicle_City__c,Vehicle_State__c,Vehicle_Zip__c,Vehicle_Country__c where Name = :startAddress OR Opportunity__c = :startAddress LIMIT 1)'); system.debug('searchList[0]---->'+searchList[0].size()); system.debug('searchList[1]---->'+searchList[1].size());
Error : System.QueryException: line 1:507 mismatched character '<EOF>' expecting '''
If Please suggest me a solution for this iussue.
Any kind of help would be greatly appriciated.
The String strartAddress takes between quotes ' ' or " ", probably because of used three quotes for startAddress
Any solution for this kind of situation?
Hi Vishal,
1. String startAddress = 'Tom \'s Towing and Recovery';
2. String startAddress = 'Tom' + '\'s' + 'Towing and Recovery';
Above both works.
If above info helps, kindly mark it as the solution
Thanks,
Pandu
Hi Pandu,
your solutions are not working.
Why are you using dynamic SOSL instead of just doing a search:
Regardless, if you're insisting on using the dynamic apex method, use the String.escapeSingleQuotes method to ensure your search is valid:
Oh, and your original error... you forgot to escape the string TWICE. This is because:
When you insert it into the query, you get this:
What you meant to do is to escape the quote within the string:
This will correctly translate to the following:
It's an easy mistake to overlook. You always have to double-escape your query strings in Apex Code if you're using Dynamic Apex. This is why the String.escapeSingleQuotes function exists, to help you properly escape quotes.