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
VSK98VSK98 

Getting error [XCEPTION_THROWN [12]|System.QueryException: expecting a right parentheses, found 'E00000x067Y'] in dynamic apex

Hello All,

I'm trying to write the dynamic parent to child query but getting the error

Snippet Code:
 
String childF   = 'Name'; 
String childO_1 = 'Opportunity'; 
String childF_1   = 'Name'; 
string childO = 'Contacts';
string parentO = 'Account';
string conid = '0035E00000x067Y';
 

// create dynamic SOQL query string 

String query = 'SELECT id, (select ' +childF+ ' from ' +childO+ ' WHERE id = ' + conid + ')   FROM Account';
system.debug('SOQL query: ' +Database.query(query));

Error:
XCEPTION_THROWN [12]|System.QueryException: expecting a right parentheses, found 'E00000x067Y'

Thanks,
VSK98​​​​​​​
ShirishaShirisha (Salesforce Developers) 
Hi,

Greetings!

Can you please try by using the Square brackets instead of single quotes as mentioned in the below query to run the SOQL query:

https://developer.salesforce.com/forums/?id=9060G000000XaaLQAS

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
VSK98VSK98
Hi Shirisha,

Thanks for your response!!!!!!!

I have written dynamic Soql query not normal Soql query.

Regards,
VSK98
SUCHARITA MONDALSUCHARITA MONDAL
Hi VSK98,
In your sub- query reocrd Id is not applied within quotes. Replace with following and it'll work.

String query = 'SELECT id, (select ' +childF+ ' from ' +childO+ ' WHERE id =\''+String.escapeSingleQuotes(conid)+'\')   FROM Account';
System.debug('SOQL query: ' +query);


Share your thoughts!

Thanks,
Sucharita