[SalesForce] Issue with “Subscribe to Platform Events” Challenge in the Define and Publish Platform Events Trailhead

I am not spotting what is causing this ERROR:

Challenge Not yet complete… here's what's wrong:
Publishing an Order_Event__e did not create the associated task successfully.
Please check your trigger and event.

There aren't any identified Problems with my OrderEventTrigger.apxt and the following PlatformEventTest.apxc runs successfully:

@isTest
public class PlatformEventTest {
    @isTest static void test1() {
        // Create test event instance
        Order_Event__e newsEvent = new Order_Event__e(
             Has_Shipped__c = true);

       Test.startTest();

        // Call method to publish events
        Database.SaveResult sr = EventBus.publish(newsEvent);

        Test.stopTest();

        // Perform validation here
        // Check that the task that the trigger created is present.
        List<Task> tasks = [SELECT Id FROM Task];
        // Validate that this task was found.
        // There is only one test task in test context.
        System.assertEquals(1, tasks.size());
    }
}

Failures 0

Total 1

Overall Code Coverage 100%

OrderEventTrigger 100%

My OrderEventTrigger.apxt file contains the following:

// Trigger for listening to Order events.

trigger OrderEventTrigger on Order_Event__e (after insert) {

            // List to hold all tasks to be created.

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

            // Iterate through each notification.

    for (Order_Event__e event : Trigger.New) {
        if (event.Has_Shipped__c == true) {

            // Create task for team to follow up on the shipped order.

      Task tk = new Task();
            tk.Priority = 'Medium';
            tk.Status = 'New';
            tk.Subject = 'Follow up on shipped order to ' + 
                event.Order_Number__c;
            tk.OwnerId = UserInfo.getUserId();
            tasks.add(tk);
        }

            // Insert all tasks corresponding to events received.
    insert tasks;
    }
}

Are you spotting the error? I'm missing it.

Best Answer

I'll let you in on a not so well kept secret. You can often troubleshot the Trailhead challenge validation in a developer org. But shh, don't tell anyone!

It's really as simple as having the developer console open and actively capturing debug logs when the validation occurs.

I reran the validation for this challenge and got the following logs.

enter image description here

So Trailhead ran two lots of anonymous Apex to validate the challenge. Interesting...

If we drill into the first anonymous Apex class we can see exactly what they are doing. Publishing the event for Order # 105.

enter image description here

Order_Event__e orderEvent = new Order_Event__e(Order_Number__c='105', Has_Shipped__c=true);
Database.SaveResult sr = EventBus.publish(orderEvent);

And then that second call to validate that it worked as expected.

enter image description here

List<Task> tasks = [select id from task where subject = 'Follow up on shipped order 105'];
System.assert(tasks.size() > 0);
delete tasks;

Now we know this, there are two things we could do:

  1. Create a Task before running the validation with the subject 'Follow up on shipped order 105'
  2. Further debug your Platform Event to figure out why it isn't meeting those requirements.

While the 1st option may be tempting and easy it doesn't really help you in the long run. What's the point in cheating yourself of the opportunity to learn more in the debugging process.

I'd recommend modifying your Apex test case to replicate the assertions Trailhead is making. You could also look at capturing debug logs for the Platform Events to see what happens when the trigger fires.

For a not so subtle hint. Check your Task subject closely to see if there is anything out of place.