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
NapsterNapster 

Need to insert variable into a Dynamic query

Hi Friends, 

I need to be able to put a string variable inside a dyanmic query string so that my batch class can further process it. Could you please help me out? Currently i am hardcoding it. 

      string PreparationLeadRTid = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Preparation').getRecordTypeId();
        this.query
string PreparationLeadRTid = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Preparation').getRecordTypeId();
this.query = 'SELECT Id,individual_id__c,recordtypeid FROM lead WHERE recordtypeid = \'0128A000000CginQAC\' and createdDate = today';
I need to be able to insert the PreparationLeadRTid in the where condition of the query where i am currently hard coding it. I have tried using the 'xxx'+ variable + 'xxx' method but it does not work. 
LBKLBK
Try this code.
string PreparationLeadRTid = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Preparation').getRecordTypeId();
this.query = 'SELECT Id,individual_id__c,recordtypeid FROM lead WHERE recordtypeid = \'' + PreparationLeadRTid + '\' and createdDate = today';
Let me know how it goes.