I'm trying to setup a batch and i'm using query locator to query the records and i'm custom meta to store my query i'm facing the issues while running the job.

I'm getting this exception:

System.QueryException: line 3:70 mismatched character '' expecting '''

My Query:

SELECT Id, OwnerId, Status__c, Status_Reason__c FROM Contact WHERE Account.Campaign__c = null
        AND Account.Opps_Won_Count__c = 0 AND Account.Do_Not_Contact__c = false
        AND (Account.Last_Opportunity_Closed_Lost__c = null OR 
        Account.Last_Opportunity_Closed_Lost__c > LAST_N_DAYS:90)
        AND Debounce_Email_Verification_Status__c IN('Safe to Send','Risky')
        AND ((Account.Good_Size__c = true AND (Account.Working_Contacts__c = 0 OR 
        Account.Working_Contacts__c = null)
        AND Account.CEO_Data_Status__c = 'Sales Ready' AND 
        (Account.Most_recent_contact_activity__c = null OR 
        Account.Most_recent_contact_activity__c > LAST_N_DAYS:30) AND New_Prospecting_Idea__c 
        = false AND New_Sales_Approach_Idea__c = false AND (LastActivityDate = null OR 
        LastActivityDate < LAST_N_DAYS:180)) OR (Account.NumberOfEmployees > 500 AND 
        Account.NumberOfEmployees <= 20000 AND (Account.Working_Contacts__c < 3 OR 
        Account.Working_Contacts__c = null)
        AND (LastActivityDate = null OR LastActivityDate < LAST_N_DAYS:90)))

My batch Job:

global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator(Apex_Queries__mdt.getInstance('ContactsRs').Query_Info__c);
  • 2
    Hi Paven, could you please add some details to your question to describe the issues you're facing? Commented Dec 16, 2022 at 20:16
  • 1
    Is Query_Info__c a long text area field?
    – RubenDG
    Commented Dec 16, 2022 at 21:41
  • Query_info__c is a long area text field
    – Pavan tej
    Commented Dec 19, 2022 at 4:52

1 Answer 1


Custom Metadata Type are cached and using getInstance or getAll you retrieve just the first 255 characters of a text field, so long text area field might be truncated as stated in the documentation:

Only the first 255 characters are returned for any field in a custom metadata type record, so longer text fields get truncated. If you want all the field data from a custom metadata type record, use a SOQL query.

You should retrieve the record via SOQL:

global Database.QueryLocator start(Database.BatchableContext BC) {
    String queryInfo = [SELECT Query_Info__c FROM Apex_Queries__mdt WHERE DeveloperName = 'ContactsRs'].Query_Info__c;
    return Database.getQueryLocator(queryInfo);

Keep in mind that queries containing long text area field count against Apex governor limit, but since it's the start of batch it shouldn't be an issue.

  • Hey thanks for looking in to this. I have a question can we use the raw query posted in the question or we to need to build the query i mean like this (' This_field__c IN(\'deliverable\',\'risky\',\'\',null) AND ' + ' this way')
    – Pavan tej
    Commented Dec 19, 2022 at 11:46
  • @Pavantej You can use the one posted in the question.
    – RubenDG
    Commented Dec 19, 2022 at 13:22

Not the answer you're looking for? Browse other questions tagged .