please help to figure out where is the issue

here is the APEX trigger

    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().addDays(1),
                    Status = 'Not Started',
                    Subject='trigger Comment changed',
                    description = Trigger.new[i].Comments__c


    insert lNewTasks;


here is the APEX unit test class

public class UnitTests_CreateUpdateTask01 {

   static testMethod void CreateUpdateTask01() {


      List<Lead> newLeadMap = new List<Lead>();
      List<Lead> oldLeadMap = new List<Lead>();

       Lead l = new Lead(
            FirstName='Test firstName',
            LastName = 'Test LastName',
            Comments__c = '28888 test comments new'

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

        Lead 10l=[select id,Comments__c from Lead where id=:l.id limit 1];

        10l.Comments__c = '28888 test comments new change to update';

        update 10l;

       //Assign the context before and after the change into a Map
       // Declare method variables
       Map<Id,Lead> mapVerifyOldLead = new Map<Id, Lead>();

       // Create initial Map of o ld Lead
       for(lead leadOld : oldLeadMap) { mapVerifyOldLead.put(leadOld.Id, leadOld); }

       //Loop through the 1newLeadMap and check old values
       for(Lead leadNew : newLeadMap) {

       if ((newLeadMap.Comments__c<> oldLeadMap.Comments__c)&&(newLeadMap.Comments__c !=null)){
       Tasks.add(new Task(
          ActivityDate = Date.today().addDays(1),
          WhoId = l.Id,
          Status = 'Not Started',
          Subject='trigger Comment changed',
          Description = l.Comments__c

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

       Task Ttest = [SELECT Whoid, Description FROM Task WHERE Whoid=:l.Id Limit 1];

       system.assertEquals('28888 test comments update', Ttest.Description);





  • Please point out the error, you have deleted the previous post and re-posted the same question:(.
    – Rao
    Aug 1, 2013 at 0:09
  • The test class wouldn't compile, for starters...
    – sfdcfox
    Aug 1, 2013 at 14:20

2 Answers 2


Your trigger can be written simply as:

trigger CreateUpdateTask01 on Lead (before update) {
    Task[] tasks = new Task[0];
    for(Lead record:Trigger.new) {
        Lead oldRecord = Trigger.newMap.get(record.Id);
        if(record.Comments__c!=oldRecord.Comments__c&&!String.isBlank(record.Comments__c)) {
                new Task(
                    ActivityDate = Date.Today().addDays(1),
                    WhoId = record.Id,
                    Status = 'Not Started',
                    Type = 'Other',
                    Priority = 'Normal',
                    Subject = 'New Lead Comment',
                    Description = record.Comments__c));
    insert tasks;

Your test code, on the other hand, has several problems. First, you're trying to create a task, which is completely unnecessary in the test code; the point was to have the system create a task from the trigger. Also, you used a numeric-starting identifier, which simply won't work. You should start your variable names with a letter.

Here's a probable version that would work using the example code above:

class TestCreateUpdateTask01 {
    static void testMethod test() {
        Lead record = new Lead(LastName='Smith',Company='Smith & Co.',Status='New');
        insert record;
        record.Comment__c = 'Test Comment';
        update record;
        Task comment = [SELECT Id,WhoId,Subject,Description,ActivityDate FROM Task];
        System.assertEquals(record.Id, comment.WhoId);
        System.assertEquals(Date.Today().addDays(1), comment.ActivityDate);
        System.assertEquals('New Lead Comment', comment.Subject);
        System.assertequals(record.Comment__c, comment.Description);
  • Hi sfdcfox thanks so much for the change on the codes. i did a test under my sandbox. the apex trigger seems not able to create the new task as i test and modify the comments__c field. the unit test has the error as below System.QueryException: List has no rows for assignment to SObject plesae help to check on this. thanks again
    – webon100
    Aug 14, 2013 at 18:55
  • hi, again i also try your unit test code together with my orginial trigger code, it display the error System.AssertException: Assertion Failed start from line of code System.assertEquals(Date.Today().addDays(1), comment.ActivityDate); thanks
    – webon100
    Aug 14, 2013 at 19:24

You are looping through the list of Trigger.new twice. Once using the Integer iterator i and once (inside the other loop) using the keyset from Trigger.newmap. I don't know what your error is, but I think if you just remove the outer loop you'll be better off than you were.

If this isn't enough to fix your problem, please post more about what you're trying to do, and what errors/exceptions you're receiving.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .