Activity Forums Salesforce® Discussions Error: Only variable references are allowed in dynamic SOQL/SOSL.

  • Error: Only variable references are allowed in dynamic SOQL/SOSL.

    Posted by Vikas Kumar on December 6, 2016 at 10:27 am

    Hi all, Got stuck in Batch classes not able to execute, getting system.queryexception - Only variable references are allowed in dynamic SOQL/SOSL.

    global class SearchAndReplace implements Database.Batchable<sobject>{
    global String Query;
    global List<id>allObjIds ;
    global SearchAndReplace(List<id>allObjectIds){
    allObjIds=allObjectIds;
    }
    global Database.QueryLocator Start(Database.BatchableContext BC){
    query='SELECT Id,Name FROM Sobject WHERE Id in:'+allObjIds;
    return Database.getQueryLocator(query);
    }
    global void execute (Database.BatchableContext BC,List<Account>scope){
    for(Account obj:scope){
    obj.Name='Algoworks';
    }
    update scope;
    }
    global void finish (Database.BatchableContext BC){
    }
    }

    Kumar replied 7 years, 2 months ago 2 Members · 1 Reply
  • 1 Reply
  • Kumar

    Member
    February 4, 2017 at 7:19 am

    Hi Vikas,

    If you use : in your query, Apex expects a variable. So your query should look like this:

    query=’SELECT Id,Name FROM Sobject WHERE Id in’+:allObjIds;

    You had your ':' inside the string, hence 'allObjIds' was not being recognized as a variable.

    Hope this helps to resolve the error - "Only variable references are allowed in dynamic SOQL/SOSL"

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos