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
KrForceKrForce 

Adding space in Dynamic SOQL

Hi ,
Can some one please to add the space before FROM in Dynamic SOQL.
string query ='SELECT ' + qFields +
                       'FROM contact'+
                       ' WHERE user_num__c = \'' + String.escapeSingleQuotes(uNum) + '\''+
                       ' LIMIT 1';
Best Answer chosen by KrForce
ClintLeeClintLee
How are you populating the qFields variable?  Perhaps there's some difference between your fields in sandbox and production.  

As an example, if the qFields variable is null or an empty string in production then your query would be 'SELECT FROM...'  which would cause the error you're seeing. 

 

All Answers

ClintLeeClintLee
String query = 'SELECT ' + 
                qFields +
               ' FROM Contact' +
               ' WHERE user_num__c = \'' + String.escapeSingleQuotes(uNum) + '\'' +
               ' LIMIT 1';
KrForceKrForce
Still not help full..
FYi..Im getting the error while deploying the changes in production...
ERROR:: System.QueryException: unexpected token: 'FROM'
Printed query from sandbox..(170233661)|USER_DEBUG|[44]|INFO|query#SELECT TRA_Given__c, Receive_Customer_Feedback_By_Email__c, Receive_Market_Summary_By_Email__c, Evening_Phone_RK_Provided__c, FirstName, Foreign_Resident__c, Gender__c, HomePhone, Investment_Objective__c FROM contact WHERE user_num__c = '22494878' LIMIT 1 
 
shashi lad 4shashi lad 4
I don't see anything wrong with above query and advised by billyclint. Are you sure your are getting exception from this statement? Please post the code to analyze. 

thanks
shashi
ClintLeeClintLee
How are you populating the qFields variable?  Perhaps there's some difference between your fields in sandbox and production.  

As an example, if the qFields variable is null or an empty string in production then your query would be 'SELECT FROM...'  which would cause the error you're seeing. 

 
This was selected as the best answer
KrForceKrForce
im getting the qfields from the custome setting and CS is part of the same change set.
Same changeset i was able to deploy in a different sandbox but failing in production.
KrForceKrForce
@Billclint..you are right..test class is expecting the custom setting to be available before we deploy the Apex class/test class. Trick is  i created the custom setting first then deployed the code changes and worked.