-1

I am unable to get the required code coverage for this trigger.

trigger Site_visit_update_in_campaign on Opportunity (after insert,after 
    update,after delete) 
{
    public Boolean s1NullCheck=false;
    public Boolean s2NullCheck=false;
    public Boolean s3NullCheck=false;
    Public Boolean s4NullCheck=false;
    List <Campaign> campaigns=[select id from campaign];
    Set<Id> keysForOpp=new Set<Id>();
    Map<Id,List<Opportunity>> oppList=new Map<Id,List<Opportunity>>();
    List<Id> filteredIds=new List<Id>();
    Campaign[] campaign=new Campaign[]{};
    Map<Id,Campaign> campaignToUpdate=new Map<Id,Campaign>([Select Id,Completed_Site_Visit_1__c,Completed_Site_Visit_2__c,Completed_Site_Visit_3__c,Completed_Site_Visit_4__c From campaign]);

    for(Opportunity oppor:Trigger.new)
    {
        for(Campaign c:campaigns)
        {
            keysForOpp.add(c.Id);
        }

        for(Opportunity o:[Select 
       CampaignId,Site_Visit_1__c,Site_Visit_2__c,Site_Visit_3__c,Site_Visit_4__c From Opportunity Where CampaignId=:keysForOpp])
        {

            try
            {
                List<Opportunity> temp=oppList.get(o.CampaignId);
                if(temp==null)
                {
                    oppList.put(o.CampaignId, new List<Opportunity>{o});
                    filteredIds.add(o.CampaignId);
                }
                else
                {
                    temp.add(o);
                }

            }
            catch(NullPointerException e)
            {
                System.debug('Exception at custom Map setting : 
            '+e.getMessage());
            }

        }

        System.debug('opportunity debug'+opplist);

        for(Id i:filteredIds)
        {
             Integer siteVisitOneCounter=0;
             Integer siteVisitTwoCounter=0;
             Integer siteVisitThreeCounter=0;
             Integer siteVisitFourCounter=0;

            for(Opportunity o:oppList.get(i))
            {



                System.debug('Opportunity after calling key : '+o);

                try
                {
                    if(o.Site_Visit_1__c !=null)
                    {
                        s1NullCheck=true;
                        System.debug('S1check : '+s1NullCheck);
                    }
                }
                catch(NullPointerException e)
                {
                    System.debug('Exeeption at siteVisitOne : '+e.getMessage());
                    System.debug('S1check : '+s1NullCheck);
                }

                try
                {
                    if(o.Site_Visit_2__c !=null)
                    {
                        s2NullCheck=true;
                        System.debug('S2check : '+s2NullCheck);
                    }
                }
                catch(NullPointerException e)
                {
                    System.debug('Exeeption at siteVisitOne : '+e.getMessage());
                    System.debug('S2check : '+s2NullCheck);
                }

                try
                {
                    if(o.Site_Visit_3__c !=null)
                    {
                        s3NullCheck=true;
                        System.debug('S3check : '+s3NullCheck);
                    }
                }
                catch(NullPointerException e)
                {
                    System.debug('Exeeption at siteVisitOne : '+e.getMessage());
                    System.debug('S13heck : '+s3NullCheck);
                }

                try
                {
                    if(o.Site_Visit_4__c !=null)
                    {
                        s4NullCheck=true;
                        System.debug('S4check : '+s4NullCheck);
                    }
                }
                catch(NullPointerException e)
                {
                    System.debug('Exeeption at siteVisitOne : '+e.getMessage());
                    System.debug('S4check : '+s4NullCheck);
                }

                if(s1NullCheck==true)
                {   
                   if(o.Site_Visit_1__c=='Completed')
                   {
                       System.debug('Opportunity : '+o.Site_Visit_1__c);
                       siteVisitOneCounter=siteVisitOneCounter+1;

                   }
                   s1NullCheck=false;
                }

                if(s2NullCheck==true)
                {   
                    if(o.Site_Visit_2__c=='Completed')
                    {
                        System.debug('Opportunity : '+o.Site_Visit_2__c);
                        siteVisitTwoCounter=siteVisitTwoCounter+1;

                    }
                    s2NullCheck=false;
                }

                if(s3NullCheck==true)
                {   
                    if(o.Site_Visit_3__c=='Completed')
                    {
                        System.debug('Opportunity : '+o.Site_Visit_3__c);
                        siteVisitThreeCounter=siteVisitThreeCounter+1;

                    }
                    s3NullCheck=false;
                }

                if(s4NullCheck==true)
                {   
                    if(o.Site_Visit_4__c=='Completed')
                    {
                        System.debug('Opportunity : '+o.Site_Visit_4__c);
                        siteVisitFourCounter=siteVisitFourCounter+1;

                    }
                    s4NullCheck=false;
                }

            }

            System.debug('Site Visit One Total : '+siteVisitOneCounter);
            System.debug('Site Visit two Total : '+siteVisitTwoCounter);
            System.debug('Site Visit three Total : '+siteVisitThreeCounter);
            System.debug('Site Visit four Total : '+siteVisitFourCounter);
            Campaign cam=campaignToUpdate.get(i);
            cam.Completed_Site_Visit_1__c=siteVisitOneCounter;
            cam.Completed_Site_Visit_2__c=siteVisitTwoCounter;
            cam.Completed_Site_Visit_3__c=siteVisitThreeCounter;
            cam.Completed_Site_Visit_4__c=siteVisitFourCounter;

            campaign.add(cam);

            try
            {
                update campaign;
                System.debug('Campaign update Success');
            }
            catch(Exception e)
            {
                System.debug('Failed to update campaig : '+e.getMessage());
            }
        }
    }
}

Here is my Test class

@isTest
public class OpportunityTriggerTest 
{

    public static testMethod void campaignUpdate()
    {
        List<User> salesExec=[select Id from user];
        Campaign campaign=new Campaign(Name='chekxx campaign',CurrencyIsoCode='INR');
        Opportunity opp=new Opportunity();
        opp.Name='Cheky Voola';
        opp.Account=new Account(id='0010l00000EhKdL');
        opp.CurrencyIsoCode='INR';
        opp.CloseDate=Date.newInstance(2018, 1, 10);
        opp.StageName='Qualification';
        opp.Rating__c='Hot';
        opp.Preferred_Project__c='Test Project';
        opp.Sales_Executive__c=salesExec.get(1).Id;
        opp.Amenities__c='Pool and Badminton';
        opp.Certifications__c='No certifications';
        opp.Brand__c='Under Armour';
        opp.Preferred_Location__c='Kavali';
        opp.Budget__c='70 Lakhs - 75 Lakhs';
        opp.Site_Visit_1__c='Completed';
        opp.CampaignId='7010l0000009iV8';
        System.debug('Campaigns in method 2 : '+campaign);
        try
        {
            insert opp;
        }
        catch(Exception e)
        {
            System.debug('Exception during insert in method 2 : '+e.getMessage());
        }
        opp.Site_visit_2__c='Completed';
        try
        {
            upsert opp;

        }
        catch(Exception e)
        {
            System.debug('Error during upsert in method 2 : '+e.getMessage());
        }


    }

}
  • and what have you tried? Show us your attempts – Novarg Jan 12 '18 at 13:02
  • @Novarg I have updated my question. Please check it. – cheky voola Jan 12 '18 at 13:12
2

One reason your trigger will be very hard to cover is that it is full of try/catch blocks. It is hardly every necessary or desirable to have an explicit try/catch in code. Your unit tests should (amongst other things) ensure that NullPointerException don't happen. It is hard to follow what the code is trying to do as it is coded right now.

Catching exceptions in tests and just logging actually lets the test pass even though something is wrong. Just let exceptions in tests bubble up to the test runner where it can log that there is a failure and report the details of the failure in the rest run output.

And code like this:

    try
    {
        update campaign;
        System.debug('Campaign update Success');
    }
    catch(Exception e)
    {
        System.debug('Failed to update campaig : '+e.getMessage());
    }

essentially results in confusing and silent failure in production where no-one will see the logging. If you expect a DmlException to happen catch that only and set it back on the object the trigger is running for.

PS

The trigger also contains a query inside the main loop which will means it is not bulkified and at risk of hitting a governor limit for bulk changes. I strongly suggest you get that fixed: best to get the tests working first then make the fix using the tests to confirm that the new logic behaves like the old logic.

There are also queries that will query every object on the org and so also break as data volumes rise.

  • Can you optimize the above code following best practise?? – cheky voola Jan 12 '18 at 14:10
  • 2
    @chekyvoola Perhaps but sorry that is your job. – Keith C Jan 12 '18 at 14:20
  • At least a sample code to replace those try-catch blocks. – cheky voola Jan 12 '18 at 14:32
  • @chekyvoola At least some of them don't serve any purpose so remove them. You can make this check o.Site_Visit_1__c=='Completed' perfectly safely whether Site_Visit_1__c is null or not. I suggest you take a step back and write down (in text form) what you are aiming to do then write the code over again. – Keith C Jan 12 '18 at 14:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.