2

error msg: Assertion Failed: Expected: 2000 test comments after change, Actual: 28888 test comments before

APEX Trigger - it check the existing lead and if comments_c field been modified, will fire the trigger to create a task. and the trigger works in Sandbox.

trigger CreateUpdateTask01 on Lead (before update) {

List<Task> lNewTasks = new List<Task>();
for (Integer i = 0; i < Trigger.new.size(); i++) {

         //Assign the context before and after the change into a Map
         Map<Id,Lead> newLeadMap = Trigger.newMap;
         Map<Id,Lead> oldLeadMap = Trigger.oldMap;

         //Loop through the map
         for(Id leadId:newLeadMap.keySet()){
         Lead myNewLead = newLeadMap.get(leadId);
         Lead myOldLead = oldLeadMap.get(leadId);

         if ((myNewLead.Comments__c<> myOldLead.Comments__c)&&(myNewLead.Comments__c !=null)){
         lNewTasks.add(new Task(
                ActivityDate = Date.today(),
                WhoID=Trigger.new[i].Id,
                Status = 'Not Started',
                type='Other',
                Priority='Normal',
                Subject='trigger Comment changed',
                description = Trigger.new[i].Comments__c
                ));
         }

        }
        }

insert lNewTasks;

}

Unit Test - Apex Class code:

@isTest
public class UnitTests_CreateUpdateTask01 {

   static testMethod void CreateUpdateTask01() {

       Lead record = new Lead(
            FirstName='Test firstName',
            LastName = 'Test LastName', 
            Comments__c = '28888 test comments update'
            );

       insert record;
       System.debug('Created and inserted lead');

       test.startTest();

       record.Comments__c = '28888 test comments update';

       Update record;
       System.debug('update existing lead comments');

       List<Task> tasks = new List<Task>();

       tasks.add(new Task(
       ActivityDate = Date.today(),
       WhoId = record.Id,
       Status = 'Not Started',
       type='Other',
       Priority='Normal',
       Subject='trigger Comment changed',
       Description = record.Comments__c
       ));

       insert tasks;
       System.debug('Created and inserted task based on existing lead comment update');

       Task comment = [SELECT Id,WhoId,Subject,Description,ActivityDate FROM Task WHERE Whoid=:record.Id Limit 1];

       system.assertEquals('28888 test comments update', comment.Description);
       System.assertEquals(record.Comments__c, comment.Description);

       test.stopTest();

   }

}

Could you help to check what is wrong with the update query i suppose. Thanks

3

Your issue here is related to the fact you are incorrectly doing things inside multiple loops. If all you want to do is check to see if your Comments__c field changed, something like that should work much better:

trigger CreateUpdateTask01 on Lead (after update) {

    List<Task> lNewTasks = new List<Task>();
    for(Lead updatedLead:Trigger.new){
        if(updatedLead.Comments__c != Trigger.oldMap.get(updatedLead.Id).Comments__c){
            lNewTasks.add(new Task(
                ActivityDate = Date.today(),
                WhoID = updatedLead.Id,
                Status = 'Not Started',
                type = 'Other',
                Priority = 'Normal',
                Subject = 'trigger Comment changed',
                description = updatedLead.Comments__c
            ));
        }
    }

    insert lNewTasks;
}

It is important to notice a few things here. I changed the trigger to after update. You do not want to create these new tasks unless the update was successful, otherwise validation might fail and then the changes get rolled back yet you already generated these new Tasks.

You will notice I used a different for loop syntax. This was due to the fact that we only want to loop over that single List of updated leads using Trigger.new. This was one of your bigger mistakes above. You loop over Trigger.new, and then inside that loop you go over every single lead again using the Trigger.newMap.keySet.

The way this code works is it:

  • Loops over all updated leads
  • Checks the updated lead against the Trigger.oldMap using the Map.get() method. This will use the currently selected Lead's Id and pull the old value out of the Map.
  • You can then check to see if the Comments__c fields are equal. If they are not equal, you know the field has changed and you can generate your task.
  • Add your tasks as part of a List of tasks, and then insert it all together
  • Hi Jesse, thanks for the trigger code explain, and it help me a lot. Could you also help to check the unit test code. It seems the Lead comments_c update is not take place. error msg: Assertion Failed: Expected: 2000 test comments after change, Actual: 28888 test comments before many thanks in advance. – webon100 Aug 19 '13 at 17:21
  • It most likely isn't taking place because your trigger code is not properly written right now. You are going to want to read about how the unit tests work as well - wiki.developerforce.com/page/… - what you have written does not seem like it would test what you expect it would test at all. – Jesse Altman Aug 19 '13 at 17:28
  • If this solved your issue @webon100, please mark it as the answer. It is important for the community statistics to mark correct answers as the answers (we shoot for a 90% answered mark). – Jesse Altman Aug 23 '13 at 1:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.