How can I troubleshoot and resolve the issue of “only variable references are allowed in dynamic Soql/sosl?”

167    Asked by Bhaanumatishukla in Salesforce , Asked on Jan 30, 2024

 I am currently working on a specific task that is related to working on a dynamic SOQL query. While working on it I was getting an issue message that was showing “Only variable references are allowed in dynamic Soql/sosl”. How can I troubleshoot and resolve this particular issue?

Answered by Daniel Cameron

 In the context of Salesforce, you can resolve and troubleshoot the issue of “only variable references are allowed in dynamic Soql/sosl” by using the string concatenation or even template strings for building your dynamic Query which can resolve the issue. Here is the example given in Apex:-

String searchTerm = ‘Example’;
String dynamicQuery = ‘SELECT Id, Name FROM CustomObject__c WHERE Name LIKE ’%’ + searchTerm + ‘%’’;
List results = Database.query(dynamicQuery);

In this above example the “searchTerm” is a variable that would help in creating a flexible search condition.



Your Answer

Interviews

Parent Categories