Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a batch class that runs on all leads unless the user has entered a specific lead Record Type into a custom setting field. I'm getting an "System.QueryException: Invalid alias" even though when I have tested out the query in the query editor and it works fine. Below is the code for the base batch query and the expanded one if they have entered a lead type.

    batchquery = 'SELECT Name, Email, id, Compete__CompeteURL__c, Compete__CompeteUVs__c, Compete__CompeteVisits__c, Compete__CompeteLastUpdated__c FROM lead';
    if(setup.Lead_Record_Type__c != null){
      batchquery += 'WHERE RecordType.Name = \'' + setup.Compete__Lead_Record_Type__c + '\'';

Where is the invalid alias error coming from?

share|improve this question
up vote 7 down vote accepted

Perhaps you are missing space in string formation.

batchquery += ' WHERE RecordType.Name = \'' + setup.Compete__Lead_Record_Type__c + '\'';

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.