+ Start a Discussion
anuragsinghnsanuragsinghns 

Not able to Escape '@' in Dynamic Query

Hi Guys,
Working on Dynamic SOQL and I have created a query which shows up in the debug like this (I have printed only a part of the query).The calues in Bold are variables that are passed.So when I pass the Email Field and do a Database.Query I get the Following Error

Query:
FirstName =: singh  AND LastName =: odc AND PersonEmail =: odc@test.com)

Error:
|System.QueryException: expecting a right parentheses, found '@'

any help would be appreciated
AshwaniAshwani
Use it like:

FirstName ='singh'  AND LastName ='odc' AND PersonEmail ='odc@test.com';

don't use colon for strings. Colon is used for variable reference.
anuragsinghnsanuragsinghns
@AvilionAvilion
I am Sorry I must have Confused you let me explain exactly what I am doing I have stored field  API Names in a Custom Setting and then getting them dynamically as shown below


for(SObject queryAccCase:CaseList) {
            for(integer i =0; i<CustomSetting.size() ; i++){
            Stringx=String.valueOf(queryAccCase.get(String.valueOf(CustomSetting[i].Case_Fields__c)));  
               system.debug('The value is +'+x);
               // while(CustomSetting[i].Account_Fields__c != Null && CustomSetting[i].Case_Fields__c != Null)
                  if(i>0) {
                    whereClauseStr +=' AND ';
                    whereClauseStr += CustomSetting[i].Account_Fields__c +' =: '+x;
            }
            else{
            whereClauseStr += CustomSetting[i].Account_Fields__c +' =: '+x;
            }
            }
        }

if(queryFieldsStr.length()>0){
            system.debug('The Query is +'+queryFieldsStr + whereClauseStr);
            Database.Query(queryFieldsStr + whereClauseStr);
        }else{
            return null;
        }
AshwaniAshwani
Instead of assigning variable outside of Strin you can use like this:

String x = 'abc@xyz.com';
for(SObject queryAccCase:CaseList) {
            for(integer i =0; i<CustomSetting.size() ; i++){
            Stringx=String.valueOf(queryAccCase.get(String.valueOf(CustomSetting[i].Case_Fields__c)));  
               system.debug('The value is +'+x);
               // while(CustomSetting[i].Account_Fields__c != Null && CustomSetting[i].Case_Fields__c != Null)
                  if(i>0) {
                    whereClauseStr +=' AND ';
                    whereClauseStr += CustomSetting[i].Account_Fields__c+' =:x';
            }
            else{
            whereClauseStr += CustomSetting[i].Account_Fields__c +' =:x';
            }
            }
        }

variable must be in scope of Database.query(queryFieldsStr + whereClauseStr); Therefore, I defined it outside of loop.