-3

I need help on the test class for this schedule on Opportunity. Getting error on below code with comment and only 35% coverage code.

global class OppJob_Task_1stPhoneFollowup implements Schedulable {
    global void execute(SchedulableContext ctx) {

     List<Opportunity> oppList = [Select ID, Post_1st_Follow_Up_cycle_days_ROPP__c, owner.id, owner.isactive, 
                                 Master_Opportunity__c, X2nd_Follow_Up_cycle_days_ROPP__c
                                 from Opportunity 
                                 where Master_Opportunity__c = null and 
                                 Renewal_Stage__c not in ('Renewed','Canceled','Pending Region') and 
                                 X2nd_Follow_Up_cycle_days_ROPP__c = NEXT_N_DAYS:1 ];
                                // X2nd_Follow_Up_cycle_days_ROPP__c = today ];
     List<Task> taskList = new List<Task>();

        For(Opportunity opp : oppList) {
      if(opp.Owner.isActive == true) {
                   Task newTask = new Task(
                         WhatId = opp.Id,
                        OwnerId = opp.OwnerId,
                   ActivityDate = opp.X2nd_Follow_Up_cycle_days_ROPP__c.adddays(1) ,
                        Subject = '1st Phone Follow-up',
                    Description = 'Call customer for 1st Phone Follow-up');
         taskList.add(newTask);
         }
        }
       system.debug('tasklink'+ tasklist.size());
    if (tasklist.size()>0){
        insert taskList;
    }
}}

Test Class:

@isTest
public class OppJob_Task_1stPhoneFollowupTest {
    static testmethod void testScheduledJob() {
        String CRON_EXP = '0 0 0 3 9 ? 2020';

        Account ac = TestingDataUtil.CreateAccounts(1)[0];
        insert ac;

        Opportunity opp = TestingDataUtil.CreateOpportunities(1, ac.Id, 'Renewal')[0];
        opp.SAP_Contract_Amount__c = 1200;
        opp.Master_Opportunity__c = null;
        opp.Renewal_Stage__c = 'Invoice Sent';
        opp.OwnerId = 'XXXXXXXXXX';
        insert opp;

        Test.StartTest();
        //Schedule the Test Job
        String jobID = System.schedule('OppJob_Task_1stPhoneFollowup123', CRON_EXP, new OppJob_Task_1stPhoneFollowup());
         Task newTask = new Task(
                         WhatId = opp.Id,
                        OwnerId = opp.OwnerId,
                     ActivityDate = date.valueOf('2019-12-24 11:30:20'),
                        Subject = '1st Phone Follow-up',
                    Description = 'Call customer for 1st Phone Follow-up');
        insert newtask;

   /**getting an error on this**
    Unexpected token 'opp.Id'
    Missing 'IDENTIFIER' at ']' 
    expecting a colon, found 'opp.Id'*
   */ 
     //error on this below//
      List<Task> lt = [SELECT Id, whatID FROM Task where whatid = opp.Id];     
      Test.stopTest();


    }    
}
  • 3
    Please edit this post to be very specific about the problem. Right now, the community cannot even tell if this test passes. We need you to help us help you. See How to Ask. – David Reed Dec 10 '19 at 22:23
  • 1
    What is your question? Please edit your post to include the details of your issue. – David Cheng Dec 10 '19 at 22:23
0

The error is easily solved:

List<Task> lt = [SELECT Id, whatID FROM Task where whatid = opp.Id];     

yields

Unexpected token 'opp.Id' Missing 'IDENTIFIER' at ']' expecting a colon, found 'opp.Id'*

because you can't put an Apex identifier (opp.Id) into a SOQL query without using binding notation: a colon prior to the identifier or expression, that is, :opp.Id.

See Using Apex Variables in SOQL and SOSL Queries for details on how to use Apex binding.

Making the test compile and pass will likely change your test coverage total. You should review the resources in How do I increase my code coverage? for more guidance on how to work with code coverage.

  • I have changes this and same error message: List<Task> lt = [SELECT Id, whatID FROM Task where whatid :opp.Id]; – Smurfet Dec 12 '19 at 14:31
  • @Smurfet The line you pasted above is incorrect because the = operator is missing. – David Reed Dec 12 '19 at 14:34
  • thanks, I completely overlook and mistakenly ":=" to "=:" my bad.... it is working now... – Smurfet Dec 12 '19 at 15:41

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.