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
RaffusRaffus 

Written a test class but few Lines are not covered?

I have written a test class but a few lines are not covered. I do not understand how to cover it. Please help!!!
@AuraEnabled
    public static demoWrapper getData(String Club){
        String userId= User.getLoggedInUserId();	
        List<User> userRec = [Select Id, Name,accountId,ContactId,ViewActivity__c from User where Id =: userId];
        List<BTS__c> BTSList = new  List<BTS__c>();
        List<Services__c> serviceList = new  List<Services__c>();
        Integer percentage = 0;
 
        demoWrapper wrapperobj = new demoWrapper();
        String accountIdstr = userRec[0].accountId;
        if(accountIdstr <> null && accountIdstr != ''){
            serviceList = [Select id, Account__c,Account__r.RecordType.Name,Account__r.CreatedDate,Broker_of_Record__c, Broker_of_Record__r.Club__c from Services__c where Broker_of_Record__c =: accountIdstr AND Account__r.RecordType.Name = 'Group'];
            BTSList = [Select id, name, RPoints__c,Milestone_Date__c,RecordType.Name,Trip_Status_Eligibility__r.Pinnacle_Target_Points__c,Trip_Status_Eligibility__r.RecordType.Name,
                                     Trip_Status_Eligibility__r.Premier_Target_Points__c,Trip_Status_Eligibility__r.Preferred_Target_Points__c, Trip_Status_Eligibility__r.Active__c,Trip_Status_Eligibility__r.Start_Date__c,
                                     Trip_Status_Eligibility__r.End_Date__c FROM BTS__c WHERE Trip_Status_Eligibility__r.Active__c = true AND Account__c =: accountIdstr AND RecordType.Name = 'Broker Reward' limit 1];
        }
        if(!BTSList.isEmpty() && BTSList[0].RecordType.Name == 'Broker Reward') {
            
             Integer additionaldays = Integer.valueOf(Label.AFocus_NewGrpNumOfDaysForRewardCal);
             Date grpcreatedStartDate = BTSList[0].Trip_Status_Eligibility__r.Start_Date__c - additionaldays;
             Date grpcreatedEndDate =  BTSList[0].Trip_Status_Eligibility__r.End_Date__c + additionaldays;
             Map<Id,Set<Id>> grpAccMap = new  Map<Id,Set<Id>>();
             if(!serviceList.isEmpty()){
                for(Services__c servRec : serviceList ){
                    if((servRec.Account__r.CreatedDate >= grpcreatedStartDate)  && (servRec.Account__r.CreatedDate <= grpcreatedEndDate))
                    { 
                        if(!grpAccMap.containsKey(servRec.Broker_of_Record__c)){
                            grpAccMap.put(servRec.Broker_of_Record__c,  new Set<Id>());
                        }
                        grpAccMap.get(servRec.Broker_of_Record__c).add(servRec.Account__c);  
                        
                    }
                }
             }
            system.debug('serviceList =' + serviceList);
            system.debug('grpAccMap =' + grpAccMap);
            
            
            if(grpAccMap != null && ! grpAccMap.isEmpty()){
                if(grpAccMap.get(accountIdstr) != null){
                    if(grpAccMap.get(accountIdstr).size() >= 2){
                        wrapperobj.GrpAccts = 2;
                    }
                    else if(grpAccMap.get(accountIdstr).size() ==1){
                        wrapperobj.GrpAccts = 1;
                    }
                  
                }
                
            }
            else if(grpAccMap == null || grpAccMap.isEmpty()){
                wrapperobj.GrpAccts = 0;
            }
          
             if(Club == 'premierClub' && BTSList[0].RPoints__c != null && BTSList[0].Trip_Status_Eligibility__r.Premier_Target_Points__c != null && BTSList[0].Trip_Status_Eligibility__c!=null){
                percentage =  Integer.valueOf(((BTSList[0].RPoints__c)*100) / (BTSList[0].Trip_Status_Eligibility__r.Premier_Target_Points__c));
                wrapperobj.tPoints =  Integer.valueOf(BTSList[0].Trip_Status_Eligibility__r.Premier_Target_Points__c);

             }
             else if(Club == 'preferredClub' && BTSList[0].RPoints__c != null && BTSList[0].Trip_Status_Eligibility__r.Preferred_Target_Points__c != null && BTSList[0].Trip_Status_Eligibility__c!=null){
                percentage =  Integer.valueOf(((BTSList[0].RPoints__c)*100) / (BTSList[0].Trip_Status_Eligibility__r.Preferred_Target_Points__c));
                wrapperobj.tPoints =  Integer.valueOf(BTSList[0].Trip_Status_Eligibility__r.Preferred_Target_Points__c);
              }
            
            wrapperobj.rPoints =  Integer.valueOf(BTSList[0].RPoints__c);
            wrapperobj.hasActive = BTSList[0].Trip_Status_Eligibility__r.Active__c;
        }
        
        wrapperobj.ViewRewards = userRec[0].ViewActivity__c;
        wrapperobj.perRewards= percentage;
        system.debug('wrapperobj' +wrapperobj);
        return wrapperobj;
    }
    
    public class demoWrapper
    {   @AuraEnabled
        public Integer tPoints{get;set;}
        @AuraEnabled
        public Integer rPoints{get;set;}
        @AuraEnabled
        public Integer perRewards{get;set;}
        @AuraEnabled
        public Boolean ViewRewards{get;set;}
        @AuraEnabled
        public Boolean hasActive{get;set;}
        @AuraEnabled
        public Integer GrpAccts{get;set;}
    }

These lines are not covered by the above code.
for(Services__c servRec : serviceList ){
                    if((servRec.Account__r.CreatedDate >= grpcreatedStartDate)  && (servRec.Account__r.CreatedDate <= grpcreatedEndDate))
                    { 
                        if(!grpAccMap.containsKey(servRec.Broker_of_Record__c)){
                            grpAccMap.put(servRec.Broker_of_Record__c,  new Set<Id>());
                        }
                        grpAccMap.get(servRec.Broker_of_Record__c).add(servRec.Account__c);  
                        
                    }
                }
 
if(grpAccMap != null && ! grpAccMap.isEmpty()){
                if(grpAccMap.get(accountIdstr) != null){
                    if(grpAccMap.get(accountIdstr).size() >= 2){
                        wrapperobj.GrpAccts = 2;
                    }
                    else if(grpAccMap.get(accountIdstr).size() ==1){
                        wrapperobj.GrpAccts = 1;
                    }
                  
                }
                
            }




 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you share the test class which you have written so I can check and let you know what to change in it.

Thanks,
 
RaffusRaffus
Here is my written test method
@isTest 
    Static void getDatatest1(){ 
        test.startTest();
        Test.setMock(HttpCalloutMock.class, new AFocus_BIWCalloutTokenMock(200));
        User usr = testSetupDataUtil.createPortalUser();
        System.runAs (usr) {
        TriggerStatus__c afocusTrigger = new TriggerStatus__c();
        afocusTrigger.ActiveFlag__c = False;
        afocusTrigger.Name = 'BrokerTripStatusTriggerForRollUp';
         insert afocusTrigger; 
         }
        
        User portalUser = [Select Id, ContactId, Contact.AccountId from User where Id=: usr.Id];
        Contact portalContact = [Select Id, AccountId from Contact where Id =: usr.ContactId ];
        system.debug(portalContact.Accountid+'portal User '+usr.ContactId+'>>>'+usr.Contact.AccountId);
        MyClass.getAgentAccountId(usr.ContactId);
       // Account accUpdate = [Select Id, ParentId from Account where Id=: portalContact.Accountid];
     
		Account accUpdate = [Select Id, ParentId from Account where Id=: portalContact.Accountid]; 
        System.debug('accUpdate =' +accUpdate);
        user adminUser = testSetupDataUtil.createRunAsUser();
        System.runAs (adminUser) {
            //Test Data Setup
            List<Account> acc = testSetupDataUtil.createAgentAccounts(1,'Agency',true);
            Id recordTypeEligiblityPeriod = Schema.SObjectType.Trip_Status_Eligibility__c.getRecordTypeInfosByName().get('Reward Status').getRecordTypeId();
            List<Trip_Status_Eligibility__c> lstEligible = new List<Trip_Status_Eligibility__c>();
            lstEligible.add(new Trip_Status_Eligibility__c(Name='2021',
                                                           Target_Points__c = 3000,
                                                           Active__c= true,Start_Date__c=system.today(),
                                                           End_Date__c=System.today()+90,Pinnacle_Target_Points__c=7500,
                                                           Preferred_Target_Points__c=5000,	Premier_Target_Points__c=2500, RecordTypeId = recordTypeEligiblityPeriod
                                                          ) );
            //N
             lstEligible.add(new Trip_Status_Eligibility__c(Name='Test',
                                                           Target_Points__c = 3000,
                                                           Active__c= true,Start_Date__c=System.today().addDays(-30),
                                                           End_Date__c=System.today(),Pinnacle_Target_Points__c=7500,
                                                           Preferred_Target_Points__c=5000,	Premier_Target_Points__c=2500, RecordTypeId = recordTypeEligiblityPeriod
                                                          ) );


            insert lstEligible;
            
       
            Id recordType = Schema.SObjectType.BTS__c.getRecordTypeInfosByName().get('Broker Reward').getRecordTypeId();
       
            List<BTS__c> lstBTS = new List<BTS__c>();
            
            lstBTS.add(new BTS__c(Broker_Trip_Status_Unique_ID__c='BP-3216304-2018',RPoints__c=8000,
                                                 Trip_Status_Eligibility__c=lstEligible[0].Id,Account__c=accUpdate.Id,
                                                 RecordTypeId = recordType));
            
            lstBTS.add(new BTS__c(Broker_Trip_Status_Unique_ID__c='BP-3216304-2019',RPoints__c=5000,
                                                 Trip_Status_Eligibility__c=lstEligible[0].Id,Account__c=accUpdate.Id,
                                                 RecordTypeId = recordType ));
            
            //N
            lstBTS.add(new BTS__c(Broker_Trip_Status_Unique_ID__c='BP-3216304-2020',RPoints__c=5000,
                                                 Trip_Status_Eligibility__c=lstEligible[1].Id,Account__c=accUpdate.Id,
                                                 RecordTypeId = recordType ));
                    insert lstBTS; 
            
                //N
                Carrier__c c2 = new Carrier__c (Name = 'carrier1',Source_System__c = 'BenefitPoint');
        			insert c2;
                    
            	Plan_Type__c PTC = new Plan_Type__c(Name = 'Medical test', Lines_of_Coverage__c='Accident');
            		insert PTC;
            	Id serviceRecTypeId = Schema.SObjectType.Services__c.getRecordTypeInfosByName().get('Benefits').getRecordTypeId();
                   
                List<Services__c> serviceList = new List<Services__c>();
                    
                        Services__c newService1 = new Services__c(
                            Account__c = accUpdate.Id,
                            Broker_of_Record__c = acc[0].id,
                            Inherit_Account_Team__c = false,
                            BOR_as_of_Date__c=system.today(),
                            Source_System__c = 'BenefitPoint',
                            RecordTypeId = serviceRecTypeId,
                            Commissionable_Broker_1__c = acc[0].id,
                            Commissionable_Broker_1_Split__c = 100,
                            Origination_Reason__c = 'New',
                            Original_Effective_Date__c = system.today(),
                            Effective_Date__c = system.today(),
                            Lives_Submitted__c = 2,
                            Carrier__c = c2.id,
                            Premium_Submitted__c = 2,
                            Plan_Type__c = PTC.id,
                            Product_Name__c = 'Test Product',
                            
                           
                            Large_Group_Benefits__c	= 'Yes',
                            Exp_Rev_Calc_Status__c='Calculated - Final', // MKasu - 10/27/2020 - #142962 - Updated the picklist value
                            Funding_Type__c = 'Level Funded'
                        );
                        serviceList.add(newService1);
            			insert serviceList;
                    

   
           
            
            Id recTypeId = Schema.SObjectType.BrokerSales__c.getRecordTypeInfosByName().get('Trip Status').getRecordTypeId();
            List<BrokerSales__c> lstBS = new List<BrokerSales__c>();
            lstBS.add(new BrokerSales__c(RecordTypeId=recTypeId, Carrier_Name__c='CAREFIRST Unit Test', Broker__c=accUpdate.Id,Weighted_Lifes_Approved__c=34,Effective_Date__c=system.today()+20));
            insert lstBS;
            
        } 
            String Club1 = 'premierClub';
            String Club2 = 'pinnacleClub';
            String Club3 = 'preferredClub';
          
        System.runAs (usr) {
            MyClass.getData(Club1);
        }
        test.stopTest();
    }


 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you confirm what do we have in  AFocus_NewGrpNumOfDaysForRewardCal  label .

Thanks,
 
RaffusRaffus
It is a custom label which contains the value 60
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

I dont think you are getting the Accountid in the test class for the below line.
 
Account accUpdate = [Select Id, ParentId from Account where Id=: portalContact.Accountid]; 
        System.debug('accUpdate =' +accUpdate);

Can you confirm in debug logs if you are getting it or not and the record type should be 'Group'.

Thanks,
 
RaffusRaffus
if group recordtype is not added then this is the error-  USER_DEBUG [155]|DEBUG|accUpdate =Account:{Id=0010400001DdHs6AAF}

After adding record type still not working - 

Id recordTypeAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Group').getRecordTypeId();
        
Account accUpdate = [Select Id, ParentId from Account where Id=: portalContact.Accountid and RecordTypeId =: recordTypeAccount];

Error - System.QueryException: List has no rows for assignment to SObject





 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Which means that you need to create the record Account which satifies the condition in test class.

Thanks,