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 dsumit d 

test class is failing with System.QueryException: unexpected token: 'Won'

hi All,
    can you help me out with my test class.
public class BatchAccountActivationDate implements Database.Batchable<sObject>, Database.Stateful {
    public static void run( Set<Id> AccountIds ) {
        
        List<Account> accountRecords = [ SELECT Id, Name, Activation_Date__c, Expiration_Date__c,
                                        ( SELECT Id, Name, StageName, Type, Contract_Expiration_Date__c, Account.Activation_Date__c, 
                                         Account.Expiration_Date__c, Contract_Activation_Date__c  
                                         FROM Opportunities 
                                         WHERE StageName = 'Closed Won' 
                                         AND ( Type= 'New Bus' OR Type= 'Expand' OR Type= 'Renewal' ) 
                                         ORDER BY createdDate DESC
                                        )
                                        FROM Account 
                                        WHERE Id IN: AccountIds 
                                       ];
        executeHelper( accountRecords );                  
    }
    
    
    
    
    public Database.QueryLocator start( Database.BatchableContext bc ){
        String stgName = 'Closed Won';
        Set<String> oppType = new Set<String>{ 'New Bus', 'Expand', 'Renewal' };
            
            String query=' SELECT Id, Name, Activation_Date__c, Expiration_Date__c,' +
            ' ( SELECT Id, Name, StageName, Type, Contract_Expiration_Date__c, Contract_Activation_Date__c '+
            ' FROM Opportunities WHERE StageName =:'+ stgName +
            ' AND Type IN :'+oppType +
            ') '+
            ' FROM Account';
        
        
        return Database.getQueryLocator( query );
    }
    public void execute( Database.BatchableContext bc, List<Account> accounts ){
        executeHelper( accounts );
    }
    
    
    public void finish(Database.BatchableContext bc){
        
    }
    
    public static void executeHelper( List<Account> accountRecords ){
        System.debug('***** accountRecords : '+accountRecords );
        List<Account> accToUpdate= new List<Account>();
        
        for( Account acc : accountRecords ){
            System.debug('**** accountRecords.Opps : ' +acc.opportunities );
            
            Opportunity opp = acc.opportunities.get(0);
            opp.Account.Activation_Date__c = opp.Contract_Activation_Date__c;
            opp.Account.Expiration_Date__c = opp.Contract_Expiration_Date__c;
            
            System.debug('**** opp.Account.Activation_Date__c : ' +opp.Account.Activation_Date__c );
            System.debug('**** opp.Account.Expiration_Date__c : ' +opp.Account.Expiration_Date__c );
            accToUpdate.add(opp.Account);
            
        }
        
        
        
        if(accToUpdate.size() > 0)
        {
            update accToUpdate;
        }
        
        
    }
}     this is my batch class. and can anyone help me to write its test class
originalangoriginalang
Try binding the stgName variable to the String query, like this:
String query=' SELECT Id, Name, Activation_Date__c, Expiration_Date__c,' +
            ' ( SELECT Id, Name, StageName, Type, Contract_Expiration_Date__c, Contract_Activation_Date__c '+
            ' FROM Opportunities WHERE StageName = :stgName' +
            ' AND Type IN :'+oppType +
            ') '+
            ' FROM Account';


You do not need to place the variable outside the single quotes. Please mark this as the best answer if this helps you.