[SalesForce] Need Help in Making Dynamic Sooql using Like Operator

I am trying to create Dynamic Soql Query using Like Operator given as below:

String tempInput ='\'%' + inputValue2  + '%\'';

                      soqlRAD2 = soqlRAD2  + ' and BR_Opportunity__r.Account.name Like :'+tempInput ;   

But getting not able to format in properly and Getting Exception as below:

System.QueryException: Only variable references are allowed in dynamic SOQL/SOSL.

Please Help me on this..

Thanks in Advance.

Best Answer

In your case

String tempInput ='\'%' + inputValue2  + '%\'';

soqlRAD2 = soqlRAD2  + ' and BR_Opportunity__r.Account.name Like :'+tempInput ;  

Just remove colon :

like this

String tempInput ='\'%' + inputValue2  + '%\'';

soqlRAD2 = soqlRAD2  + ' and BR_Opportunity__r.Account.name Like '+tempInput ;  

Example

system.debug(Database.query('SELECT Id FROM Account WHERE Name like \'%test%\''));

With variable

String inputValue2 ='test';
system.debug(Database.query('SELECT Id FROM Account WHERE Name like \'%' + inputValue2 + '%\''));
Related Topic