+ Start a Discussion
Bharat.SharmaBharat.Sharma 

My Code is Working but i don't get Code Coverage.

Hello all, 
Question: i want to create a custom field in lead object the lable would be Activity count (Number type & API Name: Activity_count__c).
(firstly i am dealing with the task only)For a Specific lead  Whenever task created with subject ='call' the count would be increase 0 or null to 1. if the subject is other then 'Call' the Activity count field Will remain same and it will not increase after the Activity count= 1 it will increase only once. Example= When Activity count= 1 and task created the  Activity count would be remain same as before.


Ans: I have Already implement this scenario. my trigger is working ( not working when the value of Activity count=null) . StilI have issue:
Why it is not working for null value that i have Discussed before?
The code coverage is still 35% after the creating test class I want atleast 75% 

​Trigger-
 
​trigger TaskUpdateLead on task (after delete, after insert,  after update) {
    
    Set<ID> LeadIds = new Set<ID>();
    String leadPrefix = Lead.SObjectType.getDescribe().getKeyPrefix();
    if(trigger.new!=null){
        for (Task t : Trigger.new) {
            if (t.WhoId!= null && string.valueof(t.WhoId).startsWith(leadPrefix) ) {
                if(!LeadIds.contains(t.WhoId)){
                    LeadIds.add(t.WhoId);
                }
            }
        }
    }
    
    if(trigger.old!=null){
        for (Task t2 : Trigger.old) {
            if (t2.WhoId!= null && string.valueof(t2.WhoId).startsWith(leadPrefix) )
            {
                if(!LeadIds.contains(t2.WhoId)){
                    LeadIds.add(t2.WhoId);
                }
            }
        }
    }
    if (LeadIds.size() > 0){
        List<Lead> leadsWithTasks = [select id,Activity_Count__c,(select id from Tasks where  Subject = 'Call'     LIMIT 1 ) from Lead where Id IN : Leadids   ];
        List<Lead> leadsUpdatable = new List<Lead>();
        for(Lead L : leadsWithTasks){
            L.Activity_Count__c = L.Tasks.size();
            leadsUpdatable.add(L);
        }
        if(leadsUpdatable.size()>0){
            
            update leadsUpdatable;
            
        }
        
    }
}



Test Class-
 
@istest
/** Test when subject is call**/
public Class TaskupdateLead
{
    Private static testMethod void TaskinsertLead()
    {
        Task t1 = new Task();
        t1.OwnerId = UserInfo.getUserId();
        t1.Subject = 'Call';
        t1.Status = 'Not Started';
        t1.Priority = 'Normal';
        insert t1;
    }
    private static testMethod void taskwidemail()
    {
        Task t2 = new Task();
        t2.OwnerId = UserInfo.getUserId();
        t2.Subject = 'Email';
        t2.Status = 'completed';
        t2.Priority = 'Normal';
        insert t2;
    } 
}
Hope some one will help me

Thanks And Regards
Bharat Sharma
 
Best Answer chosen by Bharat.Sharma
saikrishna.Gsaikrishna.G

Try this one

@isTest
public class TaskupdateLeadTriggerTest {
    @isTest static void testupdatelead(){
    lead l=new lead();
    l.LastName = 'Steave';
    l.Status = '';
    l.Company = 'Apple';
    insert l;
    
    lead l1=new lead();
    l1.LastName = 'Steave';
    l1.Status = '';
    l1.Company = 'Apple';
    insert l1;
        
    task t = new task();
    t.WhoId = l.Id;
    t.Subject = 'Call';
    t.Status = 'Not Started';
    t.Priority = 'Normal';
    insert t;
        
    task t1 = new task();
    t1.WhoId = l1.Id;
    t1.Subject = 'test';
    t1.Status = 'Not Started';
    t1.Priority = 'Normal';
    insert t1;
        
    t1.Subject = 'Call'; 
    update t1;
     
    }
}

All Answers

veda shriveda shri
Hi Bharat,

Please insert lead in test class and for that lead try to insert tasks.

this will give good test coverage.

please let me know if you need more information on this.

Thanks,
​Veda
saikrishna.Gsaikrishna.G

Try this one

@isTest
public class TaskupdateLeadTriggerTest {
    @isTest static void testupdatelead(){
    lead l=new lead();
    l.LastName = 'Steave';
    l.Status = '';
    l.Company = 'Apple';
    insert l;
    
    lead l1=new lead();
    l1.LastName = 'Steave';
    l1.Status = '';
    l1.Company = 'Apple';
    insert l1;
        
    task t = new task();
    t.WhoId = l.Id;
    t.Subject = 'Call';
    t.Status = 'Not Started';
    t.Priority = 'Normal';
    insert t;
        
    task t1 = new task();
    t1.WhoId = l1.Id;
    t1.Subject = 'test';
    t1.Status = 'Not Started';
    t1.Priority = 'Normal';
    insert t1;
        
    t1.Subject = 'Call'; 
    update t1;
     
    }
}

This was selected as the best answer
saikrishna.Gsaikrishna.G
try this one i have made 100%
@isTest
public class TaskupdateLeadTriggerTest {
    @isTest static void testupdatelead(){
    lead l=new lead();
    l.LastName = 'Steave';
    l.Status = '';
    l.Company = 'Apple';
    insert l;
        
    task t = new task();
    t.WhoId = l.Id;
    t.Subject = 'Call';
    t.Status = 'Not Started';
    t.Priority = 'Normal';
    insert t;
        
    t.WhoId = NULL;
      update t;
      
     t.WhoId = l.Id;
     t.Subject = 'Call';
      update t; 
        
  
    }
}
Bharat.SharmaBharat.Sharma
Thanks Everyone for such a fast Response !!
And thank you @Veda for your Suggestion its Working.
Nice job @Saikrishna i really appreciate your efforts.
Bharat.SharmaBharat.Sharma
Hello 
veda shri and saikrishna.G
i have one more issue with same task...
 please help me. 
 same issue now i just want to show task subject in lead page and i would like to show some information of Event and task in lead page ..
 is that possible to Salesforce trigger ??
Please Explan and Reply ASAP.