function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vishal_ThoriyaVishal_Thoriya 

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.

 

 

SFDCStarSFDCStar

The String strartAddress takes between quotes ' ' or " ", probably because of used three quotes for startAddress

string startAddress = 'Tom\'s Towing and Recovery';
Vishal_ThoriyaVishal_Thoriya

Any solution for this kind of situation?

SFDCStarSFDCStar

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

 

Vishal_ThoriyaVishal_Thoriya

Hi Pandu,

 

your solutions are not working.

sfdcfoxsfdcfox

Why are you using dynamic SOSL instead of just doing a search:

 

[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)]

Regardless, if you're insisting on using the dynamic apex method, use the String.escapeSingleQuotes method to ensure your search is valid:

 

Database.query(
String.format(
'FIND \'{0}\' IN ALL FIELDS RETURNING '+
'Account(Id,Stage__c,Tower_ID__c,Name,ShippingStreet, '+
'ShippingCity,ShippingState,ShippingPostalCode,ShippingCountry '+
'where Tower_ID__c = \'{0}\' OR Name = '\{0}\' 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 = \'{0}\' '+
'OR Opportunity__c = \'{0}\' LIMIT 1)',
new String[] { String.escapeSingleQuotes(startAddress) } ) );

Oh, and your original error... you forgot to escape the string TWICE. This is because:

 

string x = 'Tom\'s Hardware'; // x is now "Tom's Hardware"

When  you insert it into the query, you get this:

 

FIND 'Tom's Hardware' IN ALL FIELDS ...

What you meant to do is to escape the quote within the string:

 

String x = 'Tom\\\'s Hardware'; // x is now "Tom\'s Hardware"

This will correctly translate to the following:

 

FIND 'Tom\'s Hardware' IN ALL FIELDS ...

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.