4

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.

0
6

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.

1
  • 1
    Nice blog post! And nice subtle hint at the end there. :)
    – Adrian Larson
    Aug 24 '17 at 4:20
0
trigger OrderEventTrigger on Order_Event__e (after insert) {    

    // List to hold all tasks to be created
    List<Task> tasks = new List<Task>();

    // Get User Id who is active to assign the Tasks
    User q = [Select Id From User Where IsActive = true LIMIT 1];

        // Iterate through each notification
        for(Order_Event__e event: Trigger.New){

       //get all the order shipped events
        if(event.Has_Shipped__c == true)  {

        // Create a task when order is shipped
            Task tk = new Task();
            tk.Priority = 'Medium';
            tk.Status = 'New';
            tk.Subject = 'Follow up on shipped order ' +event.Order_Number__c;
            tk.OwnerId = q.id;
            tasks.add(tk);
        }    
    }
  // Insert all tasks corresponding to events received.
    insert tasks;
}
1
  • From review: Code only answers are not that helpful, to further improve your answer please add an explanation on how this solves the problem of the asker. Thank you! Aug 31 '17 at 7:11
0

trigger OrderEventTrigger on Order_Event__e (after insert) { // List to hold all cases to be created. List tsk= new List();

// Get queue Id for case owner
 user u = [SELECT Id FROM user LIMIT 1];

// Iterate through each notification.
for (Order_Event__e event : Trigger.New) {
    if (event.Has_Shipped__c  == true) {

         Task T = new Task();
T.Type = 'Email';
        T.Status = 'New';

T.Subject = 'Follow up on shipped order ' + event.Order_Number__c;
T.OwnerId = u.id;
tsk.add(T);
    }

}

// Insert all cases corresponding to events received.
insert tsk;

}

0

Run the publishing code in anonymous block and check whether task is created. If created successfully, re-evaluate the challenge. It worked for me.

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

It is a simple fix .Make sure the priority picklist have the value Medium . when I added the value in to the picklist challenge got passed.

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.