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
closenetclosenet 

Not able to get %70 coverage although wrote the expected test method

Hi Guys,

 

I am trying to test the below trigger, however am not able to reach the allowed coverage %70,

basically there three insert and one delete in the trigger, am able to get the first part covered however the second part after isUpdate doesn't get covered 

 

please see the code

trigger setAssignRec on Activity__c (after insert, before update) {
   
   
    List <Activity__Share> activityShared = new List <Activity__Share> ();
   
   //string CurrentUser =    UserInfo.getUserId();
 for (Activity__c act : trigger.new )
 {
         if (trigger.isInsert){
         List <Activity__Share> actShare1 = [SELECT Id, ParentId, UserOrGroupId, AccessLevel, RowCause, LastModifiedDate, LastModifiedById, IsDeleted 
                                             FROM Activity__Share
                                             where ParentId =: act.Id];
         for (integer i = 0; i < actShare1.size(); i++)
         {
             //actShare1.get(i).RowCause != 'Owner'
            // &&  the owner of the activity will be excluded from the share
         if (act.Assigned_To__c != actShare1.get(i).UserOrGroupId)
         {
    	Activity__Share actShare = new Activity__Share();
		actShare.ParentId = act.id;
		actShare.UserOrGroupId = act.Assigned_To__c;
		actShare.AccessLevel = 'Edit';
        actShare.RowCause = Schema.Activity__Share.RowCause.delegate__C ;
        activityShared.add (actShare);
          //}
         }
         }
         Database.SaveResult[] ActivityShareInsertResult = Database.insert(activityShared,false);
     }
    
    //insert activityShared;
       
    
    if (trigger.isUpdate)
    { 
            List <Activity__Share> actShareDel = new List <Activity__Share>() ;
            List <Activity__Share> actShareIns = new List <Activity__Share>() ;
            
            
           List <Activity__Share> actShare2 = [SELECT Id, ParentId, UserOrGroupId, AccessLevel, RowCause, LastModifiedDate, 
                                               LastModifiedById, IsDeleted 
                                               FROM Activity__Share
                                               where ParentId =: act.Id ];
       
               // inset the new assigned employee
                  for (integer y =0 ; y < actShare2.size(); y++)
                  {
                      
                     if (actShare2.get(y).UserOrGroupId == act.Assigned_To__c  && actShare2.get(y).RowCause == 'delegate__c') 
                     {
                        continue;
                     }  
                    else if (actShare2.get(y).UserOrGroupId != act.Assigned_To__c )
                    {
                         Activity__Share actSharetab = new Activity__Share();
					 	 actSharetab.ParentId = act.id;
					 	 actSharetab.UserOrGroupId = act.Assigned_To__c;
					 	 actSharetab.AccessLevel = 'Edit';
        		    	 actSharetab.RowCause = Schema.Activity__Share.RowCause.delegate__C ;
						 actShareIns.add (actSharetab);
                    }
                   
//if( actShare2.get(y).UserOrGroupId == act.Assigned_To__c && actShare2.get(y).RowCause != 'Owner')
             	 }
                // Database.SaveResult[] ActivityShareInsert = Database.insert(actShareIns,false); 
				insert actShareIns;
            
// delete the unassigned employee
        		for (Activity__c actOld: trigger.old)
            		{
               		for (integer i = 0; i < actShare2.size(); i++)
                //for (Activity__Share actItem : actShare)
                		{	
                // getting the recod of the ones who used to have an honorship but 
                // they have been changed to the new owner and exlucding the orginal owner
                			if (actShare2.get(i).UserOrGroupId == actOld.Assigned_To__c && actShare2.get(i).UserOrGroupId != act.Assigned_To__c && actShare2.get(i).RowCause !='Owner' )
           					{
               				 actShareDel.add (actShare2.get(i));
                			}
                         
              	  		}
                 		delete actShareDel;		
            		} 
        system.debug ('delete test' +  actShareDel);
        
    } 
  }
}

 

 

And wrote the below test class, please note I have not made assertion for the delete however I already tried but the code coverage not changed, 

 

 

@isTest //(SeeAllData = true) 
public class TestAssignedToShare {
   
     static testMethod void testAssignedTo()
    {
        
  	 Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
      User u = new User(Alias = 'closenet', Email='AdminClosenet@testorg.com', 
      EmailEncodingKey='UTF-8', LastName='closenet', LanguageLocaleKey='en_US', 
      LocaleSidKey='en_US', ProfileId = p.Id, 
      TimeZoneSidKey='America/Los_Angeles', UserName='closenet.tester@gmail.com');
        
          
        System.runAs(u)
          {
             
        ActivityType__c  actType =  new  ActivityType__c (
            								  OwnerId = '005i0000000fDLHAA2',
            								  Name = 'My test');
        insert actType;  
          system.debug ('xxxx'+ actType);					
         List  <ActivityType__c> actTypeLst = [SELECT Id, OwnerId, IsDeleted, Name
              							       FROM ActivityType__c 
                                               where Id =: actType.id ];
            								
         Activity__c  act = new Activity__c  (
                                            
                                             Assigned_To__c= '005i0000000fDLHAA2', 
                                             Completed_Date__c= null, 
                                             ActivityTypeId__c = actTypeLst.get(0).Id,
                                             Completed__c= false ,
                                             Cancelled__c= false ,
                                             Required_Date__c = Datetime.now() ,
                                             Cancelled_Date__c = null
         									 );
              

              								/*
      									  act.Assigned_To__c= '005i0000000fDLHAA2';
                                          act.completed_Date__c= null;
                                          act.ActivityTypeId__c = 'a07i0000001ymKH';
                                          act.Completed__c= false ;
                                          act.Cancelled__c= false ;
                                          act.Required_Date__c = Datetime.now() ;
                                          act.Cancelled_Date__c = null;
              								*/
                                        
     insert act;
     List <Activity__c> activityLst =  [SELECT  Id, IsDeleted, Name, CreatedDate, CreatedById, LastModifiedDate,
                                    LastModifiedById, SystemModstamp,LastActivityDate, Assigned_To__c,
        							Completed_Date__c,   Required_Date__c, Status__c 
                                    FROM Activity__c];
             
	//test
     system.assertEquals('005i0000000fDLHAA2',activityLst.get(0).Assigned_To__c );  
              
      
     Activity__Share actShare = new Activity__Share (ParentId = activityLst.get(0).id, 
                                                     UserOrGroupId = '005i0000000fDLHAA2', 
                                                     AccessLevel = 'Edit' ,
     												 RowCause = 'delegate__c');
              
    
     insert actShare;
  //  system.debug ('sharing insert ' + actShare ) ;           
              
    //Database.SaveResult sr = Database.insert(actShare,false);
     
              
     List <Activity__Share>  activitySharedLst = [SELECT Id, ParentId, UserOrGroupId, AccessLevel, 
                                                 RowCause, LastModifiedDate, LastModifiedById, IsDeleted
                                                 FROM Activity__Share 
                                                 where UserOrGroupId =: act.Assigned_To__c ];
              
      //test        
     system.assertEquals('delegate__c', activitySharedLst.get(0).RowCause );
       // system.debug ('before delete share' + activitySharedLst ) ; 
              
              List <Activity__Share> activitySharedLst_Del  =  new List  <Activity__Share> ();
              for (integer x = 0 ; x  < activitySharedLst.size() ; x++)
              {
                  if (activitySharedLst.get(0).RowCause == 'delegate__c')
                  	{
                     activitySharedLst_Del.add(activitySharedLst.get(x));
                  	}
                 
              } 
              delete activitySharedLst_Del; 
      
              
                  //List <Activity__Share>  activitySharedLst_Del = [SELECT Id, ParentId, UserOrGroupId, AccessLevel, 
                              //                   RowCause, LastModifiedDate, LastModifiedById, IsDeleted
                                 //                FROM Activity__Share where RowCause = : 'delegate__c' ];
             
                           
    
                
              system.debug ('after delete share' + activitySharedLst_Del ) ; 
      
              
          
     		//System.assertEquals (activitySharedLst.get(0).RowCause, 'Owner');
            
     //system.assertEquals('005i0000000fDLHAA2',activityLst.get(0).Assigned_To__c );  
           
              
              }
    }
}

 

 

Please help me to get increase the coverage to meet at least the minumum allowed

 

Thanks in advance

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
vbsvbs
The clue is in the name 'isUpdate'. I do not see any code in your test class executing an update on the Activity. Try a simple update in your test class the the end as a start.

All Answers

vbsvbs
The clue is in the name 'isUpdate'. I do not see any code in your test class executing an update on the Activity. Try a simple update in your test class the the end as a start.
This was selected as the best answer
closenetclosenet

Many thanks mate, you are A star