+ Start a Discussion
subodh chaturvedi 17subodh chaturvedi 17 

how to cover the For Loop & if condition in test class

User-added image

This is My snippet Which i need to cover in My test class .
class  :-
Public class UpdateTerritorySectionFields{
Public static Void  ofOnsiteatPSCUInProgressUp(List<Territory_Plan__c> tpe){
           Set<Id> ownerIds =New set<Id>();
           List<Event> evtList = new List<Event>();
           List<Territory_Plan__c > ObjectRec = new List<Territory_Plan__c >(); 
           List<Territory_Plan__c> tpList = new List<Territory_Plan__c>();
           List<Territory_Plan__c > ObjectBList = new List<Territory_Plan__c >();
           List<Opportunity> oppoList = new List<Opportunity>();           
           for(Territory_Plan__c t:tpe){
               ownerIds.add(t.Account_Executive__c);
            }
            Map<Id, Opportunity> oppoMap = new Map<Id, Opportunity>([SELECT Id, StageName, ownerId FROM Opportunity WHERE OwnerId IN : ownerIds]);
            evtList = [SELECT Id, WhatId, Status__c, Type, ownerId FROM Event WHERE Status__c='Scheduled' AND Type='On-Site at PSCU' AND ownerID IN:ownerIds];
            for(Territory_Plan__c tp:tpe){
               for(Event e: evtList){
                    if(tp.Account_Executive__c==e.ownerId && oppoMap.containsKey(e.WhatId) && tp.Account_Executive__c==oppoMap.get(e.WhatId).ownerId){
                       oppoList.add(oppoMap.get(e.WhatId));
                    }
                }    
tp.of_Onsite_at_PSCU_In_Progress__c = oppoList.size();
                tpList.add(tp);
            }
            update tpList;
            }
}

Test class :-
@isTest
public class testUpTerritorySectionFields {
Public Static testMethod Void UpdateTerFromEvent(){
         User u = getUserRecord();
        territory_Plan__c tp = new territory_Plan__c(Account_Executive__c=u.id);
        insert tp;
        
          
        Opportunity opp = new Opportunity(Name='Chicago Patrolmens FCU AFA',PSCU_Opp_Referred_By__c=u.id,StageName='06 - Sale Closed',CloseDate=Date.Today(),Probability=100,PSCU_Contract_Term__c='2',ownerid=u.id );
        Insert opp;
        List<Event> ev =new List<Event>();
        Event e1 =new Event(Type='On-Site at PSCU',Status__c='Scheduled',OwnerId=u.id,WhatId =opp.Id,DurationInMinutes=5,ActivityDateTime=Date.TOday()+2);
            ev.add(e1);
        Event e2 =new Event(Type='On-Site at PSCU',Status__c='Completed',OwnerId=u.id,WhatId =opp.Id,DurationInMinutes=5,ActivityDateTime=Date.TOday()+2,AddressType__c='Mailing Address');
        ev.add(e2);
        Event e3 =new Event(Type='On-Site at Account',Status__c='Scheduled',OwnerId=u.id,WhatId =opp.Id,DurationInMinutes=5,ActivityDateTime=Date.TOday()+2,AddressType__c='Mailing Address');
        ev.add(e3);
        Event e4 =new Event(Type='On-Site at Account',Status__c='Completed',OwnerId=u.id,WhatId =opp.Id,DurationInMinutes=5,ActivityDateTime=Date.TOday()+2,AddressType__c='Mailing Address');
        ev.add(e4);
        Event e5 =new Event(Type='Call/Webinar with CU',Status__c='Completed',OwnerId=u.id,WhatId =opp.Id,DurationInMinutes=5,ActivityDateTime=Date.TOday()+2,AddressType__c='Mailing Address');
        ev.add(e5);
        Event e6 =new Event(Type='Call/Webinar with CU',Status__c='Scheduled',OwnerId=u.id ,WhatId =opp.Id,DurationInMinutes=5,ActivityDateTime=Date.TOday()+2,AddressType__c='Mailing Address');
        ev.add(e6);
        Insert ev;
        
        List<Event> Evnt = [select Type,Status__c,OwnerId,DurationInMinutes,ActivityDateTime from Event Where WhatId=:opp.Id LIMIT 6 ];
        Update Evnt;
        
        
    } 
}
Akshay_DhimanAkshay_Dhiman

Hi,

Subodh Chaturvedi

 As I can see at line 29 you get null List of Territory_Plan__c so debug it and check
 

Public static Void  ofOnsiteatPSCUInProgressUp(List<Territory_Plan__c> tpe)
{
System.debug('tpe size--->'+tpe.size()+'  --->tpe '+tpe);  // Check Debug log here which value are coming in tpe List
Set<Id> ownerIds =New set<Id>();
 

Please let me know if you have a query.

Thanks,
Akshay

 

Sohan Raj GuptaSohan Raj Gupta
Hi Subodh,

Please try below test class code:
 
@isTest
public class testUpTerritorySectionFields {
Public Static testMethod Void UpdateTerFromEvent(){
        User u = getUserRecord();
        territory_Plan__c tp = new territory_Plan__c(Account_Executive__c=u.id);
        insert tp;		
          
        Opportunity opp = new Opportunity(Name='Chicago Patrolmens FCU AFA',PSCU_Opp_Referred_By__c=u.id,StageName='06 - Sale Closed',CloseDate=Date.Today(),Probability=100,PSCU_Contract_Term__c='2',ownerid=u.id );
        Insert opp;
        List<Event> ev =new List<Event>();
        Event e1 =new Event(Type='On-Site at PSCU',Status__c='Scheduled',OwnerId=u.id,WhatId =opp.Id,DurationInMinutes=5,ActivityDateTime=Date.TOday()+2);
            ev.add(e1);
        Event e2 =new Event(Type='On-Site at PSCU',Status__c='Completed',OwnerId=u.id,WhatId =opp.Id,DurationInMinutes=5,ActivityDateTime=Date.TOday()+2,AddressType__c='Mailing Address');
        ev.add(e2);
        Event e3 =new Event(Type='On-Site at Account',Status__c='Scheduled',OwnerId=u.id,WhatId =opp.Id,DurationInMinutes=5,ActivityDateTime=Date.TOday()+2,AddressType__c='Mailing Address');
        ev.add(e3);
        Event e4 =new Event(Type='On-Site at Account',Status__c='Completed',OwnerId=u.id,WhatId =opp.Id,DurationInMinutes=5,ActivityDateTime=Date.TOday()+2,AddressType__c='Mailing Address');
        ev.add(e4);
        Event e5 =new Event(Type='Call/Webinar with CU',Status__c='Completed',OwnerId=u.id,WhatId =opp.Id,DurationInMinutes=5,ActivityDateTime=Date.TOday()+2,AddressType__c='Mailing Address');
        ev.add(e5);
        Event e6 =new Event(Type='Call/Webinar with CU',Status__c='Scheduled',OwnerId=u.id ,WhatId =opp.Id,DurationInMinutes=5,ActivityDateTime=Date.TOday()+2,AddressType__c='Mailing Address');
        ev.add(e6);
        Insert ev;
        
        List<Event> Evnt = [select Type,Status__c,OwnerId,DurationInMinutes,ActivityDateTime from Event Where WhatId=:opp.Id LIMIT 6 ];
        Update Evnt;
		
		List<Territory_Plan__c> tpe = new List<Territory_Plan__c>();
		tpe.add(tp);
		
		UpdateTerritorySectionFields territorySectionFieldsObj = new UpdateTerritorySectionFields();
		territorySectionFieldsObj.ofOnsiteatPSCUInProgressUp(tpe);        
    } 
}

Hope this will help you. Let me know if it helped or you need any more assistance. 

Please mark this is as the solution if it solved your purpose.

Thanks,
Sohan Raj Gupta 
subodh chaturvedi 17subodh chaturvedi 17
Hi Sohan ,

Thanks for the Reply , i Used your code &  got an Error: Static method cannot be referenced from a non static context: void UpdateTerritorySectionFields.ofOnsiteatPSCUInProgressUp(List<Territory_Plan__c>)
Sohan Raj GuptaSohan Raj Gupta
Hi Subodh,

In this case call below line
UpdateTerritorySectionFields.ofOnsiteatPSCUInProgressUp(tpe);

Regards,
Sohan Raj