+ Start a Discussion
soma s 6soma s 6 

code coverage only 44%

Hi
    I am using after update on task , when ever i create new activity i should fire the trigger after update , my test class run but it cover only 44% couldnt find the reason for that. could you please help me

trigger Trigtask on Task(after update) 
{
Set<String> accids = new Set<String>();


    Set<String> taskaccid= new Set<String>();
       Set<String> acountph= new Set<String>();
           string taskacno ;
    
       for(task acc: Trigger.new) 
    {
         List<Task> accListS = [select accountid from task where accountid  IN :accids  AND CallDisposition LIKE '%Lead%'];
            if(accListS != null && !accListS .isEmpty()) 
            {
                taskacno = acc.whatid;
             taskaccid.add(acc.whatid);
           }
           }

      List<Account> accphone = [Select Id,IsPersonAccount,StrPhr_Do_Not_Call_Group_Practice__c, PersonDoNotCall,StrPhr_Lead_Qualified__c  from Account where Phone IN: taskaccid];       
      if(accphone != null && !accphone .isEmpty()) 
            {
             for(Account accph: accphone ) 
{
                              acountph.add(accph.Phone);
           }
    }
   
    if(!acountph.isEmpty()) 
    {
        List<Account> accList = [Select Id,IsPersonAccount,StrPhr_Do_Not_Call_Group_Practice__c, PersonDoNotCall,StrPhr_Lead_Qualified__c  from Account where Phone IN: acountph];        {
            for(Account acc: accList) 
            {
               if ( acc.IsPersonAccount == true && acc.StrPhr_Do_Not_Call_Group_Practice__c == False && acc.ID ==taskacno )  
            
                {
                acc.StrPhr_Do_Not_Call_Group_Practice__c=True;
                 //acc.PersonDoNotCall = True;

                                   }
                if ( acc.IsPersonAccount == true && acc.StrPhr_Do_Not_Call_Group_Practice__c == False && acc.ID !=taskacno ) 
                {
                acc.StrPhr_Do_Not_Call_Group_Practice__c=True;
                 acc.PersonDoNotCall = True;

                                   }

                                   
                         }
            update accList;
        }
    }
}

test code

@isTest 
public class Tritasktest{

    static testMethod void testMethodOne() 
    {
       
          Account accone = new Account();
        accone .Name='Test Account Two';
        accone .Phone='555555';
        accone .StrPhr_Do_Not_Call_Group_Practice__c = false;
        insert accone ;
        accone .Phone='555555';
                accone .StrPhr_Do_Not_Call_Group_Practice__c = true;
        update accone ;

        Task taskone= new task();
         taskone.whatid= accone .id;
         taskone.CallDisposition ='Leadrep';
                                    insert taskone;
        Account acctwo = new Account();
        accTwo.Name='Test Account Two';
        acctwo.Phone='555555';
        accTwo.StrPhr_Do_Not_Call_Group_Practice__c = false;
        insert accTwo;
        acctwo.Phone='555555';
                accTwo.StrPhr_Do_Not_Call_Group_Practice__c = true;
        update accTwo;

         Task tasktwo= new task();
         tasktwo.whatid= acctwo .id;
         tasktwo.CallDisposition ='Leadrep';
         insert tasktwo;   
         tasktwo.whatid= acctwo.id;
         Task t=[SELECT whatid,CallDisposition FROM Task WHERE whatid=:acctwo.id].get(0);

         tasktwo.CallDisposition='Lead';
          update tasktwo;     
          
              Account acc1 = [select Id,isPersonAccount, StrPhr_Do_Not_Call_Group_Practice__c,phone from Account where Id =: accTwo.Id LIMIT 1];
        System.assertEquals(true, acc1.StrPhr_Do_Not_Call_Group_Practice__c);  
        System.assertEquals('555555', acc1.Phone);  
        
    task acc = [select Id,whatid,accountid, CallDisposition,Account_Ph_No__c from task where whatId =: accone.Id LIMIT 1];
  
      
        System.assertEquals(acc.whatid,accone.id );
         
        



    }
    static testMethod void testMethodTwo() 
    {
        Account accOne = new Account();
        accOne.Name='Test Account One';
        accOne.StrPhr_Do_Not_Call_Group_Practice__c = false;
        accOne.Phone ='5555555555';
        insert accOne;
         

        Account accTwo = new Account();
        accTwo.Name='Test Account Two';
        accTwo.StrPhr_Lead_Qualified__c = false;
        accTwo.Phone ='5555555555';
        accTwo.StrPhr_Do_Not_Call_Group_Practice__c = false;
        accTwo.PersonDoNotCall = false;


        insert accTwo;

        accTwo.StrPhr_Lead_Qualified__c = True;
         accTwo.Phone ='5555555555';
         accTwo.StrPhr_Do_Not_Call_Group_Practice__c = True;
        
          

        update accTwo;

    }



}
Matthew CokeMatthew Coke
there's a check on this in the developer console. First step is to check which sections of your code are not covered
Dario BakDario Bak
The developer console will show you what lines are not being covered, there is a button for that, you will see code in red. That's the most useful tool to fix your test.
soma s 6soma s 6
hi the below are not covered 
   
                taskacno = acc.whatid;
             taskaccid.add(acc.whatid);

            for(Accounfor(Account accph: accphone ) 
{
                              acountph.add(accph.Phone)

if(!acountph.isEmpty()
acc.StrPhr_Do_Not_Call_Group_Practice__c=True;
 
Muni.sfdcMuni.sfdc
trigger Trigtask on Task(after update) 
{
Set<String> accids = new Set<String>();


    Set<String> taskaccid= new Set<String>();
       Set<String> acountph= new Set<String>();
           string taskacno ;
    
       for(task acc: Trigger.new) 
    {
         List<Task> accListS = [select accountid from task where accountid  IN :accids  AND CallDisposition LIKE '%Lead%'];
            if(accListS != null && !accListS .isEmpty()) 
            {
                taskacno = acc.whatid;
             taskaccid.add(acc.whatid);//For the List your are adding the (Whatid).

           }
           }

      List<Account> accphone = [Select Id,IsPersonAccount,StrPhr_Do_Not_Call_Group_Practice__c, PersonDoNotCall,StrPhr_Lead_Qualified__c  from Account where Phone IN: taskaccid];       // In "taskaccid" we have the whatids and ur compring with Phone.Please Validate this query.[in your scenario accphone  is always null bacause whatid is id and ph number is digits].Please Check the Trigger once .here accphone 
      if(accphone != null && !accphone .isEmpty()) 
            {
             for(Account accph: accphone ) 
{
                              acountph.add(accph.Phone);
           }
    }
   
    if(!acountph.isEmpty()) 
    {
        List<Account> accList = [Select Id,IsPersonAccount,StrPhr_Do_Not_Call_Group_Practice__c, PersonDoNotCall,StrPhr_Lead_Qualified__c  from Account where Phone IN: acountph];        {
            for(Account acc: accList) 
            {
               if ( acc.IsPersonAccount == true && acc.StrPhr_Do_Not_Call_Group_Practice__c == False && acc.ID ==taskacno )  
            
                {
                acc.StrPhr_Do_Not_Call_Group_Practice__c=True;
                 //acc.PersonDoNotCall = True;

                                   }
                if ( acc.IsPersonAccount == true && acc.StrPhr_Do_Not_Call_Group_Practice__c == False && acc.ID !=taskacno ) 
                {
                acc.StrPhr_Do_Not_Call_Group_Practice__c=True;
                 acc.PersonDoNotCall = True;

                                   }

                                   
                         }
            update accList;
        }
    }
}

Please check the above highlighted  comments
soma s 6soma s 6
Hi Very excellent reply, now my code covers 88% if the trigger is for after update
but i want to run this after insert , am not updating any thing on task am just inserting and doing all, what should i do if the same code work for after insert , my test code fails and says  the below
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Trigtask: execution of AfterInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.Trigtask: line 8, column 1: []
 
Muni.sfdcMuni.sfdc
Provide me the Proper Requirements .Not understd the Above Requirent