3

I am trying to do a dynamic SOQL for this i am adding a user id to it but while it is SOQL it is getting an error that : no viable alternative at character '\'. My SOQL look is like this SELECT name,Contact.CM_F__Profile_Completness__c, Contact.Account.CM_F__Profile_Completeness__c FROM User WHERE id = \'005900\'

My code for creating SOQL :

public String UserprofileSOQL {get; set;}
String userID = userinfo.getUserId(); 
UserprofileSOQL = 'SELECT name,Contact.'+homepgProfile[0].UserProfile_Field__c+', Contact.Account.'+homepgProfile[0].AccountProfile_Field__c+' FROM User WHERE id = \''+ userID +'\'';
userLst = Database.query(UserprofileSOQL);'

I am getting error while i query it. Please guide me to get a solution on this problem.

5
  • Are you having a compiler problem or runtime problem? I ask because there's an extra ' at the end of the code sample.
    – sfdcfox
    Nov 4, 2013 at 8:56
  • Also, you should debug UserprofileSOQL to make sure that the merged values do not contain a ' anywhere, because that would be syntactically incorrect.
    – sfdcfox
    Nov 4, 2013 at 8:58
  • i have debug the UserprofileSOQL and have mentioned the result in the question. this is the SOQL generated for SOQL - SELECT name,Contact.CM_F__Profile_Completness__c, Contact.Account.CM_F__Profile_Completeness__c FROM User WHERE id = \'005900\'SELECT name,Contact.CM_F__Profile_Completness__c, Contact.Account.CM_F__Profile_Completeness__c FROM User WHERE id = \'005900\' it is a runtime problem.
    – AnuRaj
    Nov 4, 2013 at 9:00
  • That's odd, it seems like the backslashes () are making it into the final query string, which would definitely be incorrect. When debugged, the backslashes should not appear.
    – sfdcfox
    Nov 4, 2013 at 9:36
  • right that is what i am trying to get removing the backslashes before SOQL it
    – AnuRaj
    Nov 4, 2013 at 9:44

1 Answer 1

1
public String UserprofileSOQL {get; set;}
String userID = userinfo.getUserId(); 
UserprofileSOQL = 'SELECT name,Contact.'+homepgProfile[0].UserProfile_Field__c+', Contact.Account.'+homepgProfile[0].AccountProfile_Field__c+' FROM User WHERE id=:userID';
userLst = Database.query(UserprofileSOQL);

APEX can run time identify the variables in dynamic queries .There is no need to escape the strings and make it complex solution.

5
  • i try this way as i am trying to take the record from relation ship i am not able to add get the record i am getting the SOQL as this SELECT name,Contact.CM_F__Profile_Completness__c, Contact.Account.CM_F__Profile_Completeness__c FROM User WHERE id =:userID but when i try the SELECT name,Contact.CM_F__Profile_Completness__c, Contact.Account.CM_F__Profile_Completeness__c FROM User WHERE id = '005900' in Developer console i am able to get the result.
    – AnuRaj
    Nov 4, 2013 at 9:31
  • You may like to debug the variable userID and see what you getting? Nov 4, 2013 at 9:34
  • yes it is getting the id of the login user 005900
    – AnuRaj
    Nov 4, 2013 at 9:39
  • can we get on chat and see .I am getting bored today so lets see this weird thing Nov 4, 2013 at 9:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .