0

i'm running a dynamic search, i want to add "where field like" conditions like this

 filter =  ' Name LIKE :(%'+ RowFilter.get('Name') + '%) AND'+
            'Account.Name LIKE :(%' + RowFilter.get('Account.Name') + '%)'

 affairePotentilleList = Database.query(query + 'Where ' + filter);

PS: RowFilter is map

   Map<string,string> RowFilter

I get this error:

Only variable references are allowed in dynamic SOQL/SOSL

1

You are actually not using bind capability below:

' Name LIKE :(%'+ RowFilter.get('Name') + '%) AND'

Bind : is used directly in string or soql but here you are concatenating the value to string.

and since you need to get from map, you cannot use bind :

try this:

filter =  ' Name LIKE \'%'+ RowFilter.get('Name') + '%\' AND'+
        ' Account.Name LIKE \'%' + RowFilter.get('Account.Name') + '%\'';

    affairePotentilleList = Database.query(query + 'Where ' + filter);

corrected the spacing also.

  • it works ! but I have 8 values in my map, is there not a better way for the code to be readable and clean, – Abbes CHOUKCHOU Jul 23 at 9:02
  • changed answer based on your need. Pls mark as accepted if it solved. – salesforce-sas Jul 23 at 9:27
0

In dynamic SOQL you can bind variables but nothing more complex. So you could, for example have:

String nameFilter = '%' + RowFilter.get('Name') + '%';
String acctNameFilter = '%' + RowFilter.get('Account.Name') + '%';
String filter =  ' Name LIKE :nameFilter AND Account.Name LIKE :acctNameFilter';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.