3

Can any one help me out in writing the test class for the Schedualble class.I have written a test class but the system throws an error :

Error: Compile Error: This schedulable class has jobs pending or in progress at line 2 column 8 

Schedule Class :

global class ResponseDaysCalulatorScheduler implements Schedulable
{
    global void execute(SchedulableContext ctx)
    {      
        CalculateDaysToResponse.UpdateResponseDaysForCampaignMember();
    }   
}

Apex Class :

public class CalculateDaysToResponse{
    public static void UpdateResponseDaysForCampaignMember(){
    List<CampaignMember> NewmatchingMembers = new List<CampaignMember> ();
    List<CampaignMember> matchingMembers = [SELECT Id, Response_Date__c, Days_to_Response__c, Campaign_Type__c, Lead_Status__c FROM CampaignMember WHERE Response_Date__c != null AND Campaign_Type__c = 'Inquiry' AND Lead_Status__c = 'Not Contacted'];
        for(CampaignMember matchingMember : matchingMembers)
        {
            system.debug('matchingMembers : ' + matchingMember);
            Date todayDate = system.today();
            System.debug('Today Date : ' + todayDate);
            if(matchingMember.Response_Date__c < todayDate)
            {
                System.debug('matchingMember.Response_Date__c : ' + matchingMember.Response_Date__c );
                Integer diff = matchingMember.Response_Date__c.daysBetween(date.valueof(Date.today()));
                System.debug('diff: ' + diff);
                matchingMember.Days_to_Response__c = diff;             
            } else {
                matchingMember.Days_to_Response__c = 0;
            }            
           NewmatchingMembers.add(matchingMember);
        }  
        update NewmatchingMembers ;
    }
}

Test Class :

 @Istest
public class CalculateDaysToResponseTest{

 public static testmethod void  UpdateResponseDaysForCampaignMemberTest(){

 Test.startTest();

Date todayDate = system.today();

Lead lea = new lead(lastname='Test',company='test', email='[email protected]',Approval_flag__c=false,
                    Approval_Status__c='Pending Approval',Country='USA'); 
        insert lea;

 Contact cont = new Contact(FirstName ='Testing' ,LastName='Response Days Calculation');
 insert cont;

 Campaign camp = new Campaign(Name='testing days',CurrencyIsoCode='INR',Score__c='100',Type='Inquiry',Status='Not Contacted');
 insert camp;

 list<CampaignMember> newcampaignmem = new list<CampaignMember>();
 CampaignMember campmem = new CampaignMember(Days_to_Response__c=8,CurrencyIsoCode='INR',ContactID=cont.id,CampaignID=camp.Id,
                                             Response_Date__c=Date.Today().addDays(-1),Disqualified_Reason__c='other',LeadId=lea.Id );
                                             newcampaignmem.add(campmem);

  insert newcampaignmem;

 // Test.startTest();
 ResponseDaysCalulatorScheduler sh1 = new ResponseDaysCalulatorScheduler();
 String sch = '0 0 23 * * ?'; 
 system.schedule('ResponseDaysCalulatorScheduler Check', sch, sh1); 
 Test.stopTest();

  }
  }

Any help very much appreciated.

2 Answers 2

4

The error Error: Compile Error: This schedulable class has jobs pending or in progress at line 2 column 8 comes when the class is already scheduled and you ar trying to make changes to it.

If it in development environment, what you can do is

GOTO Setup > Jobs > Scheduled Jobs and delete the scheduled job from there by finding the name of the job that is already scheduled.

Scheduled Jobs

Or you can enable "Allow deployments of components when corresponding Apex jobs are pending or in progress" option in Deployment Settings (Not Tested by Me. I recommend the first one.)

Enable Deployments

4
  • :Thanks for your response.In Sandbox we will delete and write the test class and while deploying the test class in production the system will be throwing an error.Any suggestion plz. Mar 9, 2016 at 9:32
  • Please see the second Option
    – manjit5190
    Mar 9, 2016 at 9:34
  • I was suggesting to delete the scheduled job in sandbox not the test class and then schedule it again after making changes
    – manjit5190
    Mar 9, 2016 at 9:41
  • @ manjit_singh:As Suggested i have deleted the schedule job and was able to write the test class and when i run the test class the code coverage is showing as 40%,the if conditions are not getting covered .Any help very much appreciated. Mar 9, 2016 at 11:15
0

Delete the scheduled Job with the below steps,

1) login to "https://workbench.developerforce.com/login.php" 2) On right corner, it will be showing your name and API version. Click on that link 3) There you will find change API version, change it to 32 4) Go to Utilities >> Apex Execute There run this command with job Id

I believe this would help you. Please let me know if you have any other questions, I would love to help you in that as well.

You must log in to answer this question.

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