• closenet
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies

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

 

 

 

Hi Guys,

 

I have a dazzling issue when inserting a record triggered with After update trigger

bascially I want to create a child record when the user tick the complete box in the parent form.

 

I managed to do create the trigger, however the record gets created twice, after investigating the issue realised that I have a workflow update on the complete tick box Completed__c which set the complete date Completed_Date__c if the complete ticked Completed__c

 

The question is how can I keep the workflow and the trigger works in the same time without causing the trigger to create the same record twice

 

to understand my point more,  read point 11 in the below link

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm

 

Thanks for your help in advance

 

 

//Below is the code for the trigger...
 
 
trigger setNewPoint_Penalty on Activity__c (before update) {

List <User_Points__c> newPoint = new List <User_Points__c> ();

isMinusValue MyNegClass = new isMinusValue ();

for (Activity__c a: trigger.new)
{
List <Activity__c> actLst = [SELECT Id, Name, CreatedDate, CreatedById, Assigned_To__c, Completed_Date__c, ActivityTypeId__c,
Completed__c, Cancelled__c, Required_Date__c, Cancelled_Date__c, Status__c, Details__c 
FROM Activity__c where id =: a.id];
List <User_Points__c> pointsExist = [SELECT Id, OwnerId, Points__c, Member__c, ProgressRel__c, Activity__c, Notes__c 
FROM User_Points__c
where Activity__c = : a.id ] ;


integer NegTotal = MyNegClass.penaltiy(actLst);

for (User_Points__c p: pointsExist)
{
if (p.Points__c > 0 && actLst.get(0).Completed__c && NegTotal <> 0)
{


newPoint.add (new User_Points__c (
 Activity__c = actLst.get(0).id
,Points__c = NegTotal // this is to minus 2 point as a penalty
, Member__c = actLst.get(0).Assigned_To__c
, ProgressRel__c = null 
) ); 

}
else
{
continue;
}

}
insert newPoint;
}

}
 

 

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

 

 

 

Hi Guys,

 

I have a dazzling issue when inserting a record triggered with After update trigger

bascially I want to create a child record when the user tick the complete box in the parent form.

 

I managed to do create the trigger, however the record gets created twice, after investigating the issue realised that I have a workflow update on the complete tick box Completed__c which set the complete date Completed_Date__c if the complete ticked Completed__c

 

The question is how can I keep the workflow and the trigger works in the same time without causing the trigger to create the same record twice

 

to understand my point more,  read point 11 in the below link

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm

 

Thanks for your help in advance

 

 

//Below is the code for the trigger...
 
 
trigger setNewPoint_Penalty on Activity__c (before update) {

List <User_Points__c> newPoint = new List <User_Points__c> ();

isMinusValue MyNegClass = new isMinusValue ();

for (Activity__c a: trigger.new)
{
List <Activity__c> actLst = [SELECT Id, Name, CreatedDate, CreatedById, Assigned_To__c, Completed_Date__c, ActivityTypeId__c,
Completed__c, Cancelled__c, Required_Date__c, Cancelled_Date__c, Status__c, Details__c 
FROM Activity__c where id =: a.id];
List <User_Points__c> pointsExist = [SELECT Id, OwnerId, Points__c, Member__c, ProgressRel__c, Activity__c, Notes__c 
FROM User_Points__c
where Activity__c = : a.id ] ;


integer NegTotal = MyNegClass.penaltiy(actLst);

for (User_Points__c p: pointsExist)
{
if (p.Points__c > 0 && actLst.get(0).Completed__c && NegTotal <> 0)
{


newPoint.add (new User_Points__c (
 Activity__c = actLst.get(0).id
,Points__c = NegTotal // this is to minus 2 point as a penalty
, Member__c = actLst.get(0).Assigned_To__c
, ProgressRel__c = null 
) ); 

}
else
{
continue;
}

}
insert newPoint;
}

}