function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sumit@TCSSumit@TCS 

Only variable references are allowed in dynamic SOQL/SOSL.

Hello Everyone,

I am getting below error when scheduling my job, please suggest

First error: Only variable references are allowed in dynamic SOQL/SOSL.

***************************

global class DeleteAccount_US_PR_scheduled  implements Schedulable{     global void execute(SchedulableContext sc){                  String[] set1= new String[]{'012F0000000qGHn'};         String[] set2= new String[]{'012F0000000wkju'};         String[] set3= new String[]{'012F0000000qEwY'};                  string rec1 = ('012F0000000qGHn');         String rec2 = ('012F0000000wkju');         string rec3 =('012F0000000qEwY');         Set<Id> RecIDs = new Set<Id>{rec1,rec1,rec1};         string c1=('US');         string c2=('PR');                  //String query = 'SELECT Id FROM Account WHERE (RecordTypeId in ('+ \'' + rec1 + \'' + ',' + '"' + rec2 + '"' + ',' + '"' + rec3 + '"' +') AND territory_Country_LLY__c in (' + c1 + ',' + c2 +')';         String query = 'SELECT Id FROM Account WHERE RecordTypeId in: (\'' + set1 + '\'' + ',' + '\'' + set2 + '\'' + ',' + '\'' + set3 + '\'' +')' ;         //String query = 'SELECT Id FROM Account' ;                   System.debug('@@@@@@@@@@@@@@@@@@@' + query );         DeleteAccount_US_PR delBatch = new DeleteAccount_US_PR (query);         Id BatchProcessId = Database.ExecuteBatch(delBatch);     } }

**************************
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
******************************

global class DeleteAccount_US_PR implements Database.Batchable<sObject>{     global final String Query;     global DeleteAccount_US_PR(String q){         Query=q;     }     global Database.QueryLocator start(Database.BatchableContext BC){         system.debug('******** Query ***** ' + query);         return Database.getQueryLocator(query);     }     global void execute(Database.BatchableContext BC,List<Account> scope){          List<Account> accns = new List<Account>();              for(Sobject s : scope){                    Account a = (Account)s;                     {                                                    accns.add(a);                                                }              }                                //update accns;            delete accns;     }     global void finish(Database.BatchableContext BC){}

*******************************
NagaNaga (Salesforce Developers) 
Hi Sumit,

Please follow the below links for a clear understanding on this issue

https://developer.salesforce.com/forums/?id=906F0000000AkFAIA0

https://developer.salesforce.com/forums/?id=906F0000000AfrRIAS

Best Regards
Naga Kiran
ManojjenaManojjena
Hi Sumit ,

Don't hard code any id  in your code ,it is not beat practice .

You can try like below .
 
Set<Id> recTypeIdSet=new Set<Id>();
  for(RecordType rec :[SELECT DeveloperName,Id,Name FROM RecordType WHERE SobjectType = 'Your obj Name' And (Name='Test' OR Name='Test1' OR Name="Test3")  ]){
    recTypeIdSet.add(rec.Id);
  }
  String query = 'SELECT Id FROM Account WHERE Name IN:'+recTypeIdSet;
You can replace test1,test2 and Test3 with your recordtype name .

Let me know if it helps .

Thanks
Manoj