+ Start a Discussion
Sanjat Samal 8Sanjat Samal 8 

System.QueryException: expecting a colon, found 'GroupList'

Here is my Batch class. Getting the error in   return Database.getQueryLocator(query);.

Can Someone help ?



global class ClearHistoricalAccountCaseBatch implements Database.Batchable<SObject>
{
    public List<Account> accountList;
    public Integer accountIndex;
    public String limitTerms; 
    Public List<GDPR_Case_Owner_Exemption__mdt> GDPRList ;
    Public List<Group>GroupList;

    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        GDPRList = [Select MasterLabel from GDPR_Case_Owner_Exemption__mdt];
        GroupList = [SELECT id FROM Group WHERE DeveloperName = 'GDPRList'];
        Account a = accountList[accountIndex];
        String query = 'Select Id From Case Where Account.Id = \'' + a.id + '\' and IsTrueClosed__c = true and ('
          + 'Ownerid' + '!=' + 'GroupList' + ' )' ;
        if(a.Case_Life__c == '3 Months')
        {
            query += ' and CreatedDate < ' + DateTime.Now().addMonths(-3).formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
        }
        else if(a.Case_Life__c == '6 Months')
        {
            query += ' and CreatedDate < ' + DateTime.Now().addMonths(-6).formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
        }
        else if(a.Case_Life__c == '1 Year')
        {
            query += ' and CreatedDate < ' + DateTime.Now().addYears(-1).formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
        }
        else if(a.Case_Life__c == '2 Years')
        {
            query += ' and CreatedDate < ' + DateTime.Now().addYears(-2).formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
        }
        else
        {
            query += ' and AccountId = null';
        }
        
        if(String.isNotBlank(limitTerms))
        {
            query += limitTerms;
        }
        
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<sObject> cases)
    {
        try
        {  
            Database.delete(cases, false);
        }
        catch(Exception ex)
        {
            ExceptionHandler.e('Class', 'ClearHistoricalAccountCaseBatch', ex);
        } 
    }

    global void finish(Database.BatchableContext bc)
    {
        accountIndex++;
        if(accountList.size() > accountIndex)
        {
            ClearHistoricalAccountCaseBatch batchDelete = new ClearHistoricalAccountCaseBatch();
            batchDelete.accountList = accountList;
            batchDelete.accountIndex = accountIndex;
            batchDelete.limitTerms = limitTerms;
            batchDelete.GDPRList = GDPRList;
            batchDelete.GroupList = GroupList;
            ID batchProcessId = Database.executeBatch(batchDelete, 200);
            System.debug('Returned Batch Process ID: ' + batchProcessId);           
        }
    }
}
Steven NsubugaSteven Nsubuga
global class ClearHistoricalAccountCaseBatch implements Database.Batchable<SObject>
{
    public List<Account> accountList;
    public Integer accountIndex;
    public String limitTerms; 
    Public List<GDPR_Case_Owner_Exemption__mdt> GDPRList ;
    Public List<Group>GroupList;

    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        GDPRList = [Select MasterLabel from GDPR_Case_Owner_Exemption__mdt];
        GroupList = [SELECT id FROM Group WHERE DeveloperName = 'GDPRList'];
        Account a = accountList[accountIndex];
        String query = 'Select Id From Case Where Account.Id = \'' + a.id + '\' and IsTrueClosed__c = true and ('
          + 'Ownerid' + '!=:' + 'GroupList' + ' )' ;
        if(a.Case_Life__c == '3 Months')
        {
            query += ' and CreatedDate < ' + DateTime.Now().addMonths(-3).formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
        }
        else if(a.Case_Life__c == '6 Months')
        {
            query += ' and CreatedDate < ' + DateTime.Now().addMonths(-6).formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
        }
        else if(a.Case_Life__c == '1 Year')
        {
            query += ' and CreatedDate < ' + DateTime.Now().addYears(-1).formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
        }
        else if(a.Case_Life__c == '2 Years')
        {
            query += ' and CreatedDate < ' + DateTime.Now().addYears(-2).formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
        }
        else
        {
            query += ' and AccountId = null';
        }
        
        if(String.isNotBlank(limitTerms))
        {
            query += limitTerms;
        }
        
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<sObject> cases)
    {
        try
        {  
            Database.delete(cases, false);
        }
        catch(Exception ex)
        {
            ExceptionHandler.e('Class', 'ClearHistoricalAccountCaseBatch', ex);
        } 
    }

    global void finish(Database.BatchableContext bc)
    {
        accountIndex++;
        if(accountList.size() > accountIndex)
        {
            ClearHistoricalAccountCaseBatch batchDelete = new ClearHistoricalAccountCaseBatch();
            batchDelete.accountList = accountList;
            batchDelete.accountIndex = accountIndex;
            batchDelete.limitTerms = limitTerms;
            batchDelete.GDPRList = GDPRList;
            batchDelete.GroupList = GroupList;
            ID batchProcessId = Database.executeBatch(batchDelete, 200);
            System.debug('Returned Batch Process ID: ' + batchProcessId);           
        }
    }
}

Added a colon in the String query, 
+ 'Ownerid' + '!=:' + 'GroupList' + ' )' ;
Waqar Hussain SFWaqar Hussain SF
global class ClearHistoricalAccountCaseBatch implements Database.Batchable<SObject>
{
    public List<Account> accountList;
    public Integer accountIndex;
    public String limitTerms; 
    Public List<GDPR_Case_Owner_Exemption__mdt> GDPRList ;
    Public List<Group> GroupList;

    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        GDPRList = [Select MasterLabel from GDPR_Case_Owner_Exemption__mdt];
        GroupList = [SELECT id FROM Group WHERE DeveloperName = 'GDPRList'];
        Account a = accountList[accountIndex];
        String query = 'Select Id From Case Where Account.Id = \'' + a.id + '\' and IsTrueClosed__c = true and Ownerid != :GroupList[0].Id ' ;
        if(a.Case_Life__c == '3 Months')
        {
            query += ' and CreatedDate < ' + DateTime.Now().addMonths(-3).formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
        }
        else if(a.Case_Life__c == '6 Months')
        {
            query += ' and CreatedDate < ' + DateTime.Now().addMonths(-6).formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
        }
        else if(a.Case_Life__c == '1 Year')
        {
            query += ' and CreatedDate < ' + DateTime.Now().addYears(-1).formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
        }
        else if(a.Case_Life__c == '2 Years')
        {
            query += ' and CreatedDate < ' + DateTime.Now().addYears(-2).formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
        }
        else
        {
            query += ' and AccountId = null';
        }
        
        if(String.isNotBlank(limitTerms))
        {
            query += limitTerms;
        }
        
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<sObject> cases)
    {
        try
        {  
            Database.delete(cases, false);
        }
        catch(Exception ex)
        {
            ExceptionHandler.e('Class', 'ClearHistoricalAccountCaseBatch', ex);
        } 
    }

    global void finish(Database.BatchableContext bc)
    {
        accountIndex++;
        if(accountList.size() > accountIndex)
        {
            ClearHistoricalAccountCaseBatch batchDelete = new ClearHistoricalAccountCaseBatch();
            batchDelete.accountList = accountList;
            batchDelete.accountIndex = accountIndex;
            batchDelete.limitTerms = limitTerms;
            batchDelete.GDPRList = GDPRList;
            batchDelete.GroupList = GroupList;
            ID batchProcessId = Database.executeBatch(batchDelete, 200);
            System.debug('Returned Batch Process ID: ' + batchProcessId);           
        }
    }
}