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
Victor Nguyen 13Victor Nguyen 13 

Late Payment Tracking

Hi everyone,

I have a schedule Apex class that is used for tracking pate payment.

Below is the source code. However, the code is not on the Schedule list for some reasons that I don't know. Please give me some advice on this.

Apex Class:
global class LatePayment implements Schedulable {      
    global void execute(SchedulableContext ctx) {
        Date AcceptedDate;
        
        //Schedule the class to run on every Friday at 11:59PM
        String Cron_exp = '59 59 23 ? * FRI';
        String JobID = System.Schedule('Check Late Payment', Cron_exp, new LatePayment());
        
        //Creating Registration Deadlines Date table
        List<hed__Term__c> lstTerm = [SELECT Id, Name, Application_Session__c, Application_Year__c, Registration_Deadlines__c 
                                      FROM hed__Term__c
                                      WHERE Registration_Deadlines__c < TODAY
                                      ORDER BY Registration_Deadlines__c asc];
        
        //Creating a list of all Opp that are under Acceptance Letter Issued and Visa Pass stage
        List<Opportunity> lstOpp = [SELECT Id, Deferred__c, RecordTypeId, Name, StageName, Accepted_Letter_Issued_Date__c, Visa_Pass_Date__c, CloseDate,
                                   		Late_Payment__c, I_am_applying_for_the__c, Year_of_Semester__c
                                   FROM Opportunity
                                   WHERE (StageName = 'Visa Pass' OR StageName ='Acceptance Letter Issued')
                                   AND RecordTypeId != '0126A000000xOkkQAE' AND RecordTypeId != '0126A000000xOkfQAE'
                                   ORDER BY CloseDate asc];
        
        List<Opportunity> lstUpdate = new List<Opportunity>(); 
        if (!lstOpp.IsEmpty()) {
            for (Opportunity opp : lstOpp) {
                if ((opp.RecordTypeId == '0126A000000xOkVQAU' || opp.RecordTypeId == '0126A000000JHdYQAW')) {
                    if (opp.Deferred__c == false) {
                        AcceptedDate = opp.Visa_Pass_Date__c;
                    }
                    else {
                        AcceptedDate = opp.Accepted_Letter_Issued_Date__c;
                    }
                }
                else {
                    AcceptedDate = opp.Accepted_Letter_Issued_Date__c;
                }
                for (hed__Term__c terms : lstTerm) {  
                    if (opp.I_am_applying_for_the__c == terms.Application_Session__c && opp.Year_of_Semester__c == terms.Application_Year__c) {
                        if (AcceptedDate < terms.Registration_Deadlines__c.addDays(-7)) {
                            opp.Late_Payment__c = terms.Registration_Deadlines__c.daysBetween(Date.today()) / 7;
                        }
                        else {
                            opp.Late_Payment__c = AcceptedDate.daysBetween(Date.today()) / 7;
                        }
                    }
                }
                lstUpdate.add(opp);
            }
            update lstUpdate;
        }
    }
}

Test class (This is the updated version, but I can't push it to the Procduction due to the below error)
 
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, hed.TDTM_Opportunity: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Class.OPP_Naming_TDTM.generateContactNamePortion: line 18, column 1 Class.OPP_Naming_TDTM.run: line 84, column 1 Class.hed.TDTM_TriggerHandler.runClass: line 145, column 1 Class.hed.TDTM_TriggerHandler.run: line 75, column 1 Class.hed.TDTM_Global_API.run: line 61, column 1 Trigger.hed.TDTM_Opportunity: line 33, column 1: []
Stack Trace: Class.LatePaymentTest.testScheduleJob: line 22, column 1
Previous (1 - 1 of 1) Next
 
@isTest(SeeAllData=true)
public class LatePaymentTest {
    // Creating dummy CRON expression
    public static String CRON_EXP = '0 0 0 1 1 ? 2022';
    static testmethod void testScheduleJob () {
        List<Opportunity> opptys = new List<Opportunity>();
        List<hed__Term__c> termList = new List<hed__Term__c>([SELECT Id, Name, Registration_Deadlines__c, Application_Session__c, Application_Year__c, hed__Start_Date__c FROM hed__Term__c
                                                             ORDER BY Registration_Deadlines__c asc]);
        System.assert(termList.size() == 10);
        Integer i = 0;
        for (hed__Term__c t : termList) {
            if (i < termList.size() / 2) {
                opptys.add(new Opportunity (Name = 'Opportunity ' + i, CloseDate = t.hed__Start_Date__c, RecordTypeId = '0126A000000xOkVQAU', StageName = 'Visa Pass',
                                           Visa_Pass_Date__c = t.Registration_Deadlines__c.addDays(-7), I_am_applying_for_the__c = t.Application_Session__c, Year_of_Semester__c = t.Application_Year__c));
            }
            else {
                opptys.add(new Opportunity (Name = 'Opportunity ' + i, CloseDate = t.hed__Start_Date__c, RecordTypeId = '0126A000000xOkQQAU', StageName = 'Acceptance Letter Issued',
                                           Accepted_Letter_Issued_Date__c = t.Registration_Deadlines__c.addDays(-7), I_am_applying_for_the__c = t.Application_Session__c, Year_of_Semester__c = t.Application_Year__c));
            }
            i++;
        }
        insert opptys;
        
        Map<Id,Opportunity> OppMap = new Map<Id,Opportunity> (opptys);
        List<Id> oppIds = new List<Id>(OppMap.keySet());
        
        Test.startTest();
        
        String JobId = system.schedule('Test Payment', CRON_EXP, new LatePayment());
        // Verify the scheduled job has not run yet
        List <Opportunity> OppTest = [Select Id, Late_Payment__c FROM Opportunity WHERE Id IN :oppIds];
        for (Opportunity o : OppTest) {
            System.assertEquals(0, o.Late_Payment__c);
        }
        
        Test.stopTest();
        // The job is running synchronously        
        
        OppTest = [SELECT Id, Late_Payment__c, Accepted_Letter_Issued_Date__c, Visa_Pass_Date__c FROM Opportunity WHERE Id IN :oppIds];
        System.debug('The list is ' + OppTest);
        for (Opportunity oppcheck : OppTest) {
            System.debug('Late Payment number is ' + oppcheck.Late_Payment__c);
        }
        
    }
}