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
schmichaelschmichael 

Test Class for Task Trigger

Hello All,

 

After finding some examples on this forum, I wrote this Apex Trigger for Tasks:

 

trigger taskActivityDate on Task (after insert) {
    
List<Lead> updatedLeads= new List<Lead>();
 
    for(Task newTask:Trigger.new){
            if(string.valueOf(NewTask.WhoId).startsWith('00Q'))
            {
            Lead changedLead = [select id, First_Activity_Date__c from lead where id=:newTask.WhoId];
               if(changedLead.First_Activity_Date__c == NULL){
                   changedLead.First_Activity_Date__c = DATE.newinstance(newTask.CreatedDate.year(),newTask.CreatedDate.month(),newTask.CreatedDate.day());
                   updatedLeads.add(changedLead);
           }}
   
              update updatedLeads;             
   }
}

 

I did not realize that I needed to create tests to prove that this code is functional. I have no idea how to write unit tests, nevertheless a test specific to this Trigger. Would anyone be able to help me write this test, or just provide some good explanation on what the tests need to do in order to cover this code? I've already read some of the basics in other salesforce help forums, but they haven't really helped me very much.

 

Thanks a bunch!

sfdcfoxsfdcfox

Basically, you need to just make sure your code is covered. At a basic level, the test for your code would look like this:

 

@isTest
class Test_TaskActivityDate_Trigger {
  static testMethod void testTrigger() {
    Lead l = new Lead(Company='Test',LastName='Test');
    insert l;
    Task t = new Task(WhoId=l.Id,Subject='Test',ActivityDate=Date.Today());
    insert t;
    l = [SELECT Id,First_Activity_Date__c FROM Lead WHERE Id = :l.Id];
    System.assertEquals(t.ActivityDate,l.First_Activity_Date__c);
  }
}

Note that your trigger isn't bulk-safe. I'd propose the following changes:

 

trigger taskActivityDate on Task (after insert) {
  map<id,lead> leads = new map<id,lead>(), updates = new map<id,lead>();
  for(task record:trigger.new)
    if(record.whoid!=null&&record.whoid.getsobjecttype()==lead.sobjecttype)
      leads.put(record.whoid,null);
  leads.putall([select id,first_activity_date__c from lead where id in :leads.keyset()]);
  for(task record:trigger.new)
    if(leads.containskey(record.whoid)&&leads.get(record.whoid).first_activity_date__c=null))
      updates.put(record.whoid,new lead(id=record.whoid,first_activity_date__c=record.createddate));
  update updates.values();
}

Hope this helps.

 

 

schmichaelschmichael

Thanks very much for your super speedy reply! 

 

The test you sent works perfectly for the trigger, but I am still working through making the changes you suggested to make my trigger bulk-safe. Now I just have to teach myself how to use the Force.com IDE and deploy to production.

 

Thanks very much.

sfdcfoxsfdcfox
Basically, in your original code, you are querying each lead one at a time, while the proposed change aggregates them all together and then performs just one query. It's a subtle difference, but has major performance implications.

The Force.com IDE is pretty simple, but if your code is covered, you can create a package (Setup > Create > Packages) in the organization where the code resides, add the trigger and class files, upload it, then install it using the install link to your production organization. You can stave off learning the IDE for a day when you have time.
schmichaelschmichael

Thanks again for all your help. I am trying to install the package on my production salesforce, and I see the installation URL but I can't get it to work. When I click through to that, I can't install it from the sandbox for obvious reasons, but when I copy paste the url into my task bar, I can't log in on my production account. Am I missing something?

schmichaelschmichael

Actually figured that one out myself. Thanks again for all your help. Looks like I have it working!