-2

I am new to Apex and don't have much experience with test classes. Can anybody please suggest as to how to cover this trigger? Which points should I keep in mind or share a sample code which can help me understand how various aspects of this trigger can be covered. Any help is appreciated.

Trigger EnquiryCountTrigger on Enquiry_Form__c(After insert,After Delete,After update, After Undelete)
{

          CAP_AG_Toggle_logic__c cs = 
          CAP_AG_Toggle_logic__c.getInstance(userinfo.getProfileId());

          if ( cs == null || cs.CAP_AG_Trigger__c == true){

          Set<Id> setOppsIds = new Set<Id>();
          List<Opportunity> oppsToUpdate;
          List<Opportunity> statusUpdate;
          Map<Id,Opportunity> OppMap;
          Map<Id,Opportunity> StatusMap;
          Map<Id,Integer> opptyWithUpdatedCounterMap = new Map<Id, Integer>();
          Map<Id,Boolean> opptyWithUpdatedStatus = new Map<Id, Boolean>();
          if (Trigger.isInsert || Trigger.isUndelete || Trigger.isUpdate) {
              for (Enquiry_Form__c ef : Trigger.new) {
                  if(ef.Form_Type__c!=null && ef.Form_Type__c!='' && ef.Status__c!=null && ef.Status__c!=''){
                      if(ef.Form_Type__c.equalsIgnoreCase('Full') && ef.Status__c.equalsIgnoreCase('Publish') && (Trigger.isInsert || Trigger.isUndelete || (Trigger.isUpdate && ef.Status__c!=Trigger.oldMap.get(ef.Id).Status__c)))
                      {
                          if(opptyWithUpdatedCounterMap.containsKey(ef.Opportunity__c)){
                              opptyWithUpdatedCounterMap.put(ef.Opportunity__c,opptyWithUpdatedCounterMap.get(ef.Opportunity__c)+1);
                          }
                          else{
                              opptyWithUpdatedCounterMap.put(ef.Opportunity__c,1);
                          }
                      }
                    if(ef.Form_Type__c.equalsIgnoreCase('Full') && ef.Status__c.equalsIgnoreCase('Submit') && (Trigger.isInsert || Trigger.isUndelete || (Trigger.isUpdate && ef.Status__c!=Trigger.oldMap.get(ef.Id).Status__c)))
                    {
                        opptyWithUpdatedStatus.put(ef.Opportunity__c,True);
                    }

                  }
              }


          }

          if(Trigger.isDelete){

              for (Enquiry_Form__c ef : Trigger.new) {
                 if(ef.Form_Type__c!=null && ef.Form_Type__c!='' && ef.Status__c!=null && ef.Status__c!=''){
                 if(ef.Form_Type__c.equalsIgnoreCase('Full') && sef.Status__c.equalsIgnoreCase('Publish'))
                     {
                          if(opptyWithUpdatedCounterMap.containsKey(ef.Opportunity__c)){
                              opptyWithUpdatedCounterMap.put(ef.Opportunity__c,opptyWithUpdatedCounterMap.get(ef.Opportunity__c)-1);
                          }
                          else{
                              opptyWithUpdatedCounterMap.put(ef.Opportunity__c,0-1);
                          }
                      }
                  }
              }
          }
          if(opptyWithUpdatedCounterMap!=null && !opptyWithUpdatedCounterMap.isEmpty()){
              oppsToUpdate = new List<Opportunity>();
              OppMap = new Map<Id,Opportunity>([SELECT Id,Cap_AG_APS_CountofEF__c From Opportunity where id =:opptyWithUpdatedCounterMap.keySet()]);
              for(Opportunity opp : OppMap.values()){
                  if (opp.CAP_AG_APS_CountofEF__C == null) {opp.CAP_AG_APS_CountofEF__C=0;}
                  opp.Cap_AG_APS_CountofEF__c +=opptyWithUpdatedCounterMap.get(opp.Id);
                  oppsToUpdate.add(opp);
              }
          }
          if(opptyWithUpdatedStatus!=null && !opptyWithUpdatedStatus.isEmpty()){
              statusUpdate = new List<Opportunity>();
              StatusMap = new Map<Id,Opportunity>([SELECT Id,SEF_Approval__c From Opportunity where id =:opptyWithUpdatedStatus.keySet()]);
              for(Opportunity opp : StatusMap.values()){

                  opp.SEF_Approval__c = opptyWithUpdatedStatus.get(opp.Id);
                  statusUpdate.add(opp);
              }
          }
          if(oppsToUpdate!=null && oppsToUpdate.size()>0){
              database.Update(oppsToUpdate);
          }
          if(statusUpdate!=null && statusUpdate.size()>0){
              database.Update(statusUpdate);
          }
    }

 }

marked as duplicate by Raul, David Reed apex Jan 4 at 12:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Awesome answer by David Reed; shares on how to get started with writing apex test class, please read, give it a try and let us know in case you are stuck or have any specific issue. Link to question- How do I write an Apex unit test? – Raul Jan 4 at 12:04