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
SATZSATZ 

How to write the test class for following trigger.

Hi all.

how to write test class for follwing trigger.

trigger

---------------

trigger ConvertContactEventToCoachingNotes on Event (before insert,after insert,after update,after delete){


    //*** Prevent the Event Creation with out Existing Education ***//

    if(Trigger.isBefore && Trigger.isInsert) {
        Set<ID> ACCs = new Set<ID> {};
        List<Educations__c> EDLIST = new List<Educations__c>();
        ACCs.add(Trigger.new[0].WhatId);
        String stype = Trigger.New[0].Type__c;

        if(stype =='Included in Program'){
            EDLIST = [Select RecordTypeId, Id From Educations__c   where  Account__c in : ACCs and  RecordTypeId='012Q00000008pHh'];
        }else if(stype =='A la Carte'){
            EDLIST = [Select RecordTypeId, Id From Educations__c   where  Account__c in : ACCs and  RecordTypeId='012Q00000008pHc'];
        }
        
        if(Trigger.isBefore && Trigger.isInsert){
            if(EDLIST.size()==0){
                trigger.new[0].addError('<b>An education record or education tracker record needs to be created before scheduling this type of event.</b>');
            }
        }
    }

    //*** Prevent the Event Creation  with out Tobsesch Program ************//

    if(Trigger.isBefore && Trigger.isInsert) {
        Set<ID> acc = new Set<ID> {};
        acc.add(Trigger.New[0].WhatID);
        List<String> ETobe = new List<String>();
        System.debug('Trigger collection:'+Trigger.new[0]);
        List<EducationTracker__c> EDT = new List<EducationTracker__c> {};
        String stype = Trigger.New[0].Type__c ;
        System.debug('Stype:'+stype);
        if(stype =='Included in Program'){
            EDT = [select id,Account__c,Classes_Scheduled__c, Education__r.RecordTypeID, Classes_Completed__c, To_Be_Scheduled__c from EducationTracker__c where Account__c in :acc and Education__r.RecordTypeID='012Q00000008pHh'];
            System.debug('Exit Education:'+EDT);

        }else if (stype =='A la Carte'){
            EDT = [select id,Account__c,Classes_Scheduled__c, Education__r.RecordTypeID, Classes_Completed__c, To_Be_Scheduled__c from EducationTracker__c where Account__c in :acc and Education__r.RecordTypeID='012Q00000008pHc'];
        }


        for(EducationTracker__c et:EDT){
            ETobe.add(et.To_Be_Scheduled__c);
        }
        System.debug('ETOBE FOR:'+ETobe);

        String st = Trigger.new[0].Subject;
        System.debug('ETobse list:'+ETobe);
        try{
            if(ETobe != Null){
                for(String s:ETobe){
                    if(!s.Contains(st)){
                        Trigger.New[0].addError('<b>What you are scheduling in not in this contact’s Education Tracker. Please fix before continuing.</b>');
                    }
                }
            }
        }catch(Exception e){
            Trigger.New[0].addError('<b>What you are scheduling in not in this contact’s Education Tracker. Please fix before continuing.</b>');
        }
    }

    //*** Call the EducationTracker Util Class  for Inserting Coahcing Notes *******//

    if(!Trigger.isBefore && Trigger.isInsert){
        Event[] e= Trigger.New;
        EducationTrackerUtilClass.addevent(e);
        System.debug('Test3:'+e);
    }

    //*** Update the Event ***********************************************************//

    Set<ID> setCon = new Set<ID> {};
    Set<ID> setCCon = new Set<ID> {};

    if(Trigger.isUpdate ) {
        for(Event e: Trigger.new) {
            System.debug('Event:'+e);
            if(e.Subject == 'Private Coaching Session' && e.Coaching_Session_Number__c != null && e.Coaching_Total_Signeds__c != null ){
                setCon.add(e.AccountId);
            }else {
                if(e.Subject =='Career Guidance Session' && e.Type__c != null){
                    setCCon.add(e.AccountId);
                    System.debug('CGS Account:'+setCCon);
                }
            }
        }

        if(!setCon.isEmpty() && setCon.size()>0) {
            NewStudentProgressTracker SPT = new NewStudentProgressTracker(setCon,'Program');
            SPT.UpdateEducationTracker();
        }

        if(!setCCon.isEmpty() && setCCon.size()>0) {
            NewStudentProgressTracker SPT = new NewStudentProgressTracker(setCCon,'Program');
            SPT.UpdateEducationTracker();
        }
    }

    //***  Delete the Coahing Notes Once Delete the  Related Event ****************//

    if(Trigger.isDelete){

        set<ID> DEWList = new set<ID>();
        set<ID> DESList = new set<ID>();
        List<Coaching_Notes__c> DelCoachList = new List<Coaching_Notes__c>();
        for(Event  ev:Trigger.old) {
            DEWList.add(ev.whatid);
            DESList.add(ev.AccountId);
            System.debug('Delete WhatId:'+DEWList);
            System.debug('Delete Student:'+DESList);
        }  

        DelCoachList = [select id,Name,Account__c from Coaching_Notes__c  where id in :DEWList and Account__c in : DESList];
        if(DelCoachList.size() >0) {
            delete DelCoachList;
        }
    }
}