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
koti91koti91 

Problem with test coverage

trigger NMO_CaseOppurtunity_PrimaryLogic_BIBU on Opportunity__c(before insert,beforeupdate){

     

  list<Opportunity__c> lstCaseOpptyToUpdate = new list<Opportunity__c>();//lstCaseOpptyToUpdate is used to update CO that are not a part of trigger.

  list<lead> lstLeadsToUpdate = new list<lead>();//lstLeadsToUpdate is used to update the lead owner whenever a primary CO changes.

  set<Id> setLeadIds = new set<Id>();

  map<Id,lead> mapLeads = new  map<Id,lead>();

  map<id,list<Opportunity__c>> mapLeadWiseCaseOpty  = new map<id,list<Opportunity__c>>();

  map<id,id> mapLeadAssignOwner = new map<id,id> ();

  list<Opportunity__c> colst1=new list<Opportunity__c>() ;

 

  for(Opportunity__c co:trigger.new){

    setLeadIds.add(co.lead__c);// Gather all lead IDs related to the Case Oppurtunities.

  }

 

  for(Lead l : [select id,ownerId,(select id,OwnerId,Primary_Opportunity__c,Primary_Override__c from Case_Opportunities__r) from Lead where id in :setLeadIds]){

    mapLeadWiseCaseOpty.put(l.id,l.Case_Opportunities__r);// map used to have all the related Case Oppurtunities for the lead IDs

    mapLeads.put(l.id,l);

  }

 

   // When Record is inserted

if(trigger.isInsert){

    for(Opportunity__c co:trigger.new){

            colst1=mapLeadWiseCaseOpty.get(co.lead__C);// colst1 contains all the case oppurtunities related to the lead.

            if(colst1.size()!=0){//check if it has any siblings

                  if(co.Primary_Override__c=='Yes'){// check if current case oppurtunity wants to become the primary CO.

                        for(integer i=0;i<colst1.size();i++){ //make all other CO in the system to false

                              colst1[i].Primary_Opportunity__c=false;

                              colst1[i].Primary_Override__c='No';

                              lstCaseOpptyToUpdate.add(colst1[i]);     

                    }

                  co.Primary_Opportunity__c=true;

                  co.Primary_Override__c='No';

                  lead l = mapLeads.get(co.lead__c);

                  l.ownerId = co.ownerId;

                  lstLeadsToUpdate.add(l);

              }

                  else{

                        co.Primary_Opportunity__c=false;

                }

          }

          else{//No Siblings

                  co.Primary_Opportunity__c=true;

                  co.Primary_Override__c='No';

                  lead l = mapLeads.get(co.lead__c);

            l.ownerId = co.ownerId;

            lstLeadsToUpdate.add(l);

          }

      }    

}

 

  // when record is updated

if(trigger.isUpdate) {

      set<id> cOids=new set<id>();

      for(Opportunity__c coSet:trigger.new){

            cOids.add(coSet.id);

      }

      for(Opportunity__c co:trigger.new){

      colst1=mapLeadWiseCaseOpty.get(co.lead__C);// colst1 contains all the case oppurtunities related to the lead.

      if(colst1.size()!=0){//it has siblings

                  if(co.Primary_Override__c=='Yes'){// check if current case oppurtunity wants to become the primary CO.

                  for(integer i=0;i<colst1.size();i++){ //make all other CO in the system to false

                        if((colst1[i].id!=co.id) && (!cOids.contains(colst1[i].id))){

                                    colst1[i].Primary_Opportunity__c=false;

                                    colst1[i].Primary_Override__c='No';

                                    lstCaseOpptyToUpdate.add(colst1[i]);     

                              }

              }

                        co.Primary_Opportunity__c=true;

                        co.Primary_Override__c='No';

                        lead l = mapLeads.get(co.lead__c);

                  l.ownerId = co.ownerId;

                  lstLeadsToUpdate.add(l);

                  }

                  else{

                        if(co.Primary_Opportunity__c!=true){

                              co.Primary_Opportunity__c=false;

                        }

                        else{

                              lead l = mapLeads.get(co.lead__c);

                        l.ownerId = co.ownerId;

                        lstLeadsToUpdate.add(l);

                        }

                  }

      }

      else{//No Sibings

            co.Primary_Opportunity__c=true;

                  co.Primary_Override__c='No';

                  lead l = mapLeads.get(co.lead__c);

            l.ownerId = co.ownerId;

            lstLeadsToUpdate.add(l);

      }

    }

}

//Final DML

 

 if(lstCaseOpptyToUpdate.size()>0){

     try{

           update lstCaseOpptyToUpdate;

     }

      catch(System.DMLException e){

            set<id> exleadIds=new set<id>();

      for(Opportunity__c coError:lstCaseOpptyToUpdate){

          exleadIds.add(coError.lead__c);

      }

      for(Opportunity__c coError1:trigger.new){

            if(exleadIds.contains(coError1.lead__c)){

                  coError1.addError('The System encountered a problem when attempting to update related Case Oppurtunities:'+e.getMessage());

            }

      }

      }    

}

if(lstLeadsToUpdate.size()>0){

      try{

            update lstLeadsToUpdate;

      }

      catch(System.DMLException e){

            set<id> exleadIds=new set<id>();

      for(Lead luerror:lstLeadsToUpdate){

            exleadIds.add(luerror.id);

      }

      for(Opportunity__c coError:trigger.new){

            if(exleadIds.contains(coError.lead__c)){

                  coError.addError('The System encountered a problem when attempting to update lead owner:'+e.getMessage());

            }

      }

      }

}

}

 

 

This is my code and i had written a test class for this which is covering only 76% of code and my org needs above 85%. The problem is i cannot cover the try catch methods in the code. could any one help me on how to write test class to throw an exception and enter the code in to exception....its urgent

Starz26Starz26

To cover the catch blocks you have to create a scenerio that causes the exception..

 

For example

 

1. Create a good record

2. insert it - covers the good parts

3. create a record missing required fields

4. Insert it - covers dml exception

 

They do not have to be the same method. You can write one test method for the good case and another test method for the bad test case. The cumulative results of both methods will be applied....

koti91koti91

my case is little bit different from that. I have two objects Obj A and ObjB.

 

Obj B is child 

Obj A is master.

 

trigger is fired on obj B . when trigger is fired on obj B i am updating the records of ObjA and i am writing the update statement "update objAlistofRecords". this statement is written in try catch method.so i could not create records for obj A with some required values missing , if i create it i need to insert it in test class which will have the error there it self but not in the try catch.

 

 

Starz26Starz26

If you are unable to create the scenerio in test methods then it is unlikly that it could happen in real life. Maybe you are trying to catch an error that will never exist?