• Roberto Gimenez 1
  • NEWBIE
  • 10 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies
Hi,

I'm trying to accomplish OpportunityChangeTrigger challenger from trailhead but I found one error and I don't know how to address it.
The error is:
The trigger doesn't create the expected task for opportunities whose stage has been updated to 'Closed Won'.

This is my code for the trigger:
 
trigger OpportunityChangeTrigger on OpportunityChangeEvent (after insert) {
    List<Opportunity> opps = new List<Opportunity>();
    List<Task> taskList = new List<Task>();
    for(OpportunityChangeEvent event : Trigger.New){
        EventBus.ChangeEventHeader header = event.ChangeEventHeader;
        if(header.changeType == 'UPDATE' && event.isWon){
			Task task = new Task();
            task.subject = 'Follow up on won opportunities: ' +header.recordIds;
            tasklist.add(task);
        }
    }
    if(taskList.size()>0){
        insert taskList;
    }

}

And this is the  test class
@isTest
public class TestOpportunityChangeTrigger {
    @isTest static void testCreateAndUpdateEmployee(){
        Test.enableChangeDataCapture();
        insert new Opportunity(Name='Sell 100 Widgets', StageName='Prospecting', CloseDate=Date.today().addMonths(3));
        Test.getEventBus().deliver();
        Opportunity[] oppList = [SELECT Id,StageName FROM Opportunity];
        for (Opportunity opp : oppList){
          opp.StageName = 'Closed Won';
          update opp;
          Test.getEventBus().deliver();
      }
        Task[] taskList = [SELECT Id FROM Task];
        System.assertEquals(1, taskList.size(),'The change event trigger did not create the expected task.');
    }
}



 
Hi,

I'm trying to accomplish OpportunityChangeTrigger challenger from trailhead but I found one error and I don't know how to address it.
The error is:
The trigger doesn't create the expected task for opportunities whose stage has been updated to 'Closed Won'.

This is my code for the trigger:
 
trigger OpportunityChangeTrigger on OpportunityChangeEvent (after insert) {
    List<Opportunity> opps = new List<Opportunity>();
    List<Task> taskList = new List<Task>();
    for(OpportunityChangeEvent event : Trigger.New){
        EventBus.ChangeEventHeader header = event.ChangeEventHeader;
        if(header.changeType == 'UPDATE' && event.isWon){
			Task task = new Task();
            task.subject = 'Follow up on won opportunities: ' +header.recordIds;
            tasklist.add(task);
        }
    }
    if(taskList.size()>0){
        insert taskList;
    }

}

And this is the  test class
@isTest
public class TestOpportunityChangeTrigger {
    @isTest static void testCreateAndUpdateEmployee(){
        Test.enableChangeDataCapture();
        insert new Opportunity(Name='Sell 100 Widgets', StageName='Prospecting', CloseDate=Date.today().addMonths(3));
        Test.getEventBus().deliver();
        Opportunity[] oppList = [SELECT Id,StageName FROM Opportunity];
        for (Opportunity opp : oppList){
          opp.StageName = 'Closed Won';
          update opp;
          Test.getEventBus().deliver();
      }
        Task[] taskList = [SELECT Id FROM Task];
        System.assertEquals(1, taskList.size(),'The change event trigger did not create the expected task.');
    }
}