1

I have Created a Batch and test class for code coverage. However Im getting the following error message while executing the test apex.

"no viable alternative at character '"'"

Batch Apex:

//Overdue opportunity reminder batch

//Owner: samuel global class OverdueOppNotification implements Database.Batchable, Database.Stateful {

 //Variable Section
global FINAL String strQuery;
global List<String> errorMessages = new List<String>();

global OverdueOppNotification() { 
    this.strQuery = getBatchQuery();
}

 //Returns the Query String to Batch constructor to fetch right records.
private String getBatchQuery() {
    String strQuery = 'SELECT Id, CloseDate, StageName, Owner.Email FROM Opportunity WHERE CloseDate < TODAY AND StageName != "5. Won - Execution" AND StageName != "6. Won - Cash" AND StageName  != "Closed Lost" AND StageName != "Closed / Not realized" '; 
    return strQuery;
}
//Batch Start method
global Database.QueryLocator start(Database.BatchableContext BC) {
    return Database.getQueryLocator(strQuery);
}
//Batch Execute method calls findCostForWoD method
global void execute(Database.BatchableContext BC, List<sObject> scopeList) {
    System.debug(LoggingLevel.INFO, '== scopeList size ==' + scopeList.size());
    
    List<Opportunity> oppList = (List<Opportunity>) scopeList;
    if(!oppList.isEmpty()) { 
        List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
        for (Opportunity prod : oppList)
        {               
            
            Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); 
            String[] toAddresses = new String[] {prod.Owner.Email};
                message.setTemplateId('00X1w000000IdaZ');
            Message.setToAddresses(toAddresses); 
            Message.SaveAsActivity = false;
            mailList.add(Message);
            
        }
        if(!mailList.isEmpty()) {
            try{
                Messaging.sendEmail(mailList);
            }
            catch (Exception ex) {
                errorMessages.add('Unable to send email to Tech: '+ ex.getStackTraceString());
            }
        }
    }
}  
 //Batch Finish method for after execution of batch work
global void finish(Database.BatchableContext BC) { 
}

}

Test Calss:

@isTest

public class Test_OverdueOppNotification {

@isTest
static void Test_overdueOppNotification() {
    
    // create custom setting
    Country_sales_margin__c  cs = new Country_sales_margin__c(CS_CCR_Norway__c=7.8);
    insert cs;
    
    // new User
    // Owner of the Opportunity/Account 1
    User testOwnerNorth = TestUtils.createSampleUser('testOwnerOPPNorth', 'CCR Sales User');
    testOwnerNorth.Email = 'testownerbis@ccr.com';
    testOwnerNorth.CCR_Local_Area__c = 'North';
    testOwnerNorth.CCR_Country__c = 'CCR Austria';
    testOwnerNorth.USER_VR_Bypass__c = true;
    Insert testOwnerNorth;
    
    // new Account
    // Account linked to the Opportunity
    Account testAcc = new Account();
    testAcc.Name = 'testAccount';
    testAcc.Ownerid = testOwnerNorth.id;
    testAcc.ACC_Synchronize_Contact_Address__c=false;
    testAcc.BillingCountry = 'Norway'; // Margin Country 7.8        
    testAcc.Customer_Type__c = 'Retailer';
    insert testAcc;
    
   // new Opportunity
     //List<Opportunity> op_List = New List<Opportunity>();       
    Opportunity testOpp = new Opportunity();
    testOpp.ownerId =testOwnerNorth.id;
    testOpp.Name = 'Opp to test';
    testOpp.Opportunity_Short_Description__c  = 'Opptest';
    testOpp.Accountid =testAcc.id;
    testOpp.stageName ='0. Qualification';
    testOpp.type = 'New Business';
    testOpp.Sales_Process__c = 'Light - English';
    testOpp.Market_Segment__c = 'Turnkey Industrial';
    testOpp.CurrencyIsoCode = 'EUR';
    testOpp.CloseDate = Date.today() +1;
    testOpp.Amount = 300000;
    testOpp.Sales_Margin__c = 10;
    Insert testOpp;
    //op_List.add(testOpp);
    
    Test.startTest();
     //Database.SaveResult []str = Database.insert(op_List,false);
   // system.assertEquals(True, str[0].isSuccess());
    OverdueOppNotification overdue = new OverdueOppNotification();
    database.executeBatch(overdue);
    
    Test.stopTest();
    
    
}

}

Please advise.

Thanks,

1

The error seems to be in the query string. You are using double-quotes " character which is not recognized by the apex, it only recognizes single-quote '. There are several ways you can fix this.

Use ' escaped character

String strQuery = 'SELECT Id, CloseDate, StageName, Owner.Email FROM Opportunity WHERE CloseDate < TODAY AND StageName != \'5. Won - Execution\' AND StageName != \'6. Won - Cash\' AND StageName  != \'Closed Lost\' AND StageName != \'Closed / Not realized\'';

Or create a set of string and use it as binding variable

String stageNames= new Set<String>();
// Add values in set
List<sObject> sobjList = Database.query('SELECT Id FROM Opportunity WHERE StageName in  :stageNames');
3
  • @ Manjit5190 this has fixed the issue, however, I could not get 100 % code coverage the Execute part of batch class did not covered with the test class I have writen. Sep 28 '20 at 10:49
  • Your query looks for records where the Close date is less than today but your test data has close date as Date.today() +1 so you get no results. You should create a separate question for this.
    – manjit5190
    Sep 28 '20 at 11:00
  • @ Manjit5190 that worked absolutely fine. Thank you. Sep 28 '20 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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