0

I need the dynamic SOQL query with open braces and closed. But I am not getting that... need query like

select field1__c, field2__c from Object__c where field1__c = id AND (field2__c == 'abc' OR field2__c == 'jkl').

I tried like this my exact query below

string teamquery = 'select '+packageNamespace+'Response_Date__c,'+packageNamespace+'RBM_Template_Team__r.name,'+packageNamespace+'Participant_Name__c,'+packageNamespace+'Role__r.Name from '+packageNamespace+'RBM_Template_Team__c where '+packageNamespace+'RBM_Template__c = \'' + recid +'\'';
    string temp = packageNamespace+'Role__r.Name = \''+'Project Manager'+'\'';
    temp += ' OR '+ packageNamespace+'Role__r.Name = \''+'Statistician'+'\'';
    temp += ' OR '+ packageNamespace+'Role__r.Name = \''+'Medical Monitor'+'\'';

    teamquery += ' AND (\''+temp+'\')'; 
    MyQuery = DataBase.query(teamquery);

but I am getting error unexpected token: 'CTMax__Role__r.Name = ' An unexpected error has occurred. Your development organization has been notified.

I got the solution, updated below line.. teamquery += ' AND ('+temp+')';

4
  • The error should be pretty prescriptive. What was it
    – Eric
    Commented May 19, 2017 at 4:06
  • @Eric, error showing like this unexpected token: 'field2__c = ' Commented May 19, 2017 at 4:24
  • 1
    Post the exact error message. And you need to put you criteria in quotes. abc should be \'abc\' etc
    – Eric
    Commented May 19, 2017 at 4:26
  • 1
    Missing a space here: OR'+ packageNamespace+'field2__c
    – martin
    Commented May 19, 2017 at 5:08

1 Answer 1

0

Try this.

string teamquery = 'SELECT field1__c, field2__c '+
                   'FROM ' +packageNamespace+'Object__c '+
                   'WHERE '+ packageNamespace+'field1__c = ' + recid + 
                   ' AND (' +packageNamespace+'field2__c = \'abc\' OR '+ packageNamespace+'field2__c  = \'jkl\')'; 
system.debug(teamquery);

MyQuery = DataBase.query(teamquery);

As you updated your actual query, need to modify like this.

string teamquery = 'select '+packageNamespace+'Response_Date__c,'+packageNamespace+'RBM_Template_Team__r.name,'+packageNamespace+'Participant_Name__c,'+packageNamespace+'Role__r.Name '+
                    'from '+packageNamespace+'RBM_Template_Team__c '+
                    'where '+packageNamespace+'RBM_Template__c = ' + recid;

string temp = packageNamespace+'Role__r.Name = \'Project Manager\'';
temp += ' OR '+ packageNamespace+'Role__r.Name = \'Statistician\'';
temp += ' OR '+ packageNamespace+'Role__r.Name = \'Medical Monitor\'';

teamquery += ' AND ('+temp+')';
MyQuery = DataBase.query(teamquery); 
2
  • @VijayKumar Did this work for you?
    – Reshma
    Commented May 19, 2017 at 5:32
  • @VijayKumar so you'll come back to respond to an answer that gave you another error but won't help us help you by posting the info requested?
    – Eric
    Commented May 19, 2017 at 6:13

You must log in to answer this question.

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