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
sudhIr NarayansudhIr Narayan 

Code Coverage

Hi 

 I wrote a trigger on appointment object which will update lead status in lead which is working fine. I wrote a test class for trigger as mentioned below it is still showing me same 0% please suggest me how to fix

Trigger
======
trigger Update_Lead_Status on Appointment_Detail__c (After Insert, After Update) 
{
 set<id> ADId = new set<id>();
 
 for(Appointment_Detail__c AD : trigger.new) 
 {
     ADId.add(AD.ID);
     Appointment_Detail__c ApptDt = [ SELECT  Completed__c,Lead_Id__c  FROM Appointment_Detail__c where id in :ADId ]; 
     
    If ( ApptDt.Lead_Id__c <> null && ApptDt.Completed__c == 'Yes' ) 
    {
      Lead ld = [select staTus from Lead where id = :ApptDt.Lead_Id__c]; 
        ld.status = 'SQL';
        update ld;      
          }         
      }
}

Test Class
==========

@isTest(SeeAllData = true) 
private class test_appointment_leadstatus
{
  public static testmethod void testlead()
    {   
    test.startTest();

    Appointment_Detail__c ApptDt = [ SELECT  Completed__c,Lead_Id__c  FROM Appointment_Detail__c where id = 'a1gm000000001GJ'];   
    
  If ( ApptDt.Lead_Id__c <> null && ApptDt.Completed__c == 'Yes' ) 
    {
      Lead ld = [select status from Lead where id = :ApptDt.Lead_Id__c]; 
        ld.status = 'SQL';
        update ld;  
    } 
   
    test.stopTest();
   
   }  
           

}


Thanks

Sudhir

Best Answer chosen by sudhIr Narayan
bob_buzzardbob_buzzard
Your trigger will execute after the insert/update of an Appointment_Detail__c record, but you are simply querying one from the database and updating the associated lead.  You'd do better to create your own Appointment_Detail__c record in the test class and insert it there. From the look of it you need a lead also, but it should be fairly straightforward:

Lead ld=new Lead(Lastname='Test', Company='Test');
insert ld;
Appointment_Detail__c appDt=new Appointment_Detail__c(Name='Test', 
                                                      Lead_Id__c=ld.id,
                                                      Completed__c='Yes');
insert appDt;
You then won't need the @SeeAllData=true, as your unit test data will be created by the test itself.

I'm also duty bound to point out that you have SOQL queries nested inside for loops in your trigger, so this probably wouldn't handle mass insert/updates (from the data loader, for example) without breaching governor limits.  You can find out more about this at : 

https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code

All Answers

bob_buzzardbob_buzzard
Your trigger will execute after the insert/update of an Appointment_Detail__c record, but you are simply querying one from the database and updating the associated lead.  You'd do better to create your own Appointment_Detail__c record in the test class and insert it there. From the look of it you need a lead also, but it should be fairly straightforward:

Lead ld=new Lead(Lastname='Test', Company='Test');
insert ld;
Appointment_Detail__c appDt=new Appointment_Detail__c(Name='Test', 
                                                      Lead_Id__c=ld.id,
                                                      Completed__c='Yes');
insert appDt;
You then won't need the @SeeAllData=true, as your unit test data will be created by the test itself.

I'm also duty bound to point out that you have SOQL queries nested inside for loops in your trigger, so this probably wouldn't handle mass insert/updates (from the data loader, for example) without breaching governor limits.  You can find out more about this at : 

https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code

This was selected as the best answer
Anoop yadavAnoop yadav
Hi Sudhir,

You have not the trigger properly for Bulk data.
This will give you Too many Soql, if you insert more than 100 records at once.

Try the below link for Trigger Best Practices.
https://developer.salesforce.com/page/Apex_Code_Best_Practices

If you want to Test coverege for this Trigger,
you should insert a lead and Appointment_Detail__ records.