[SalesForce] Spot the Difference: Can’t complete challenge with the code. Platform Events Basics

This is the challenge:

Subscribe to a Platform Event in an Apex Trigger

Your Salesforce app uses a trigger to listen to events. Once your app receives the notification from the order system through the trigger, it creates a
task to follow up on the order shipment.

  • Create an Apex trigger named OrderEventTrigger for Order_Event__e.
    This trigger will be similar to the CloudNewsTrigger trigger, but operates on the Order_Event__e event and creates tasks instead of cases.
  • To get the Task OwnerId, use the following query:
    • User myUser = [SELECT Id FROM User WHERE Name='FirstName LastName' LIMIT 1];
    • Replace FirstName and LastName with the corresponding name of your logged-in user.
  • If the order has shipped (event.Has_Shipped__c == true), create a task with the following values:
    • Priority: 'Medium'
    • Subject: 'Follow up on shipped order ' + event.Order_Number__c
    • OwnerId: myUser.Id

This is the code that I initially wrote (challenge rejected it: Publishing an Order_Event__e did not create the associated task successfully. Please check your trigger and event.):

trigger OrderEventTrigger on Order_Event__e (after insert)
{
    List<Task> tasks = new List<Task>();

    User myUser = [SELECT Id FROM User WHERE Name = 'Elizabeth Rodriguez' LIMIT 1];

    for (Order_Event__e event : Trigger.new)
    {
        if (event.Has_Shipped__c == true)
        {
            Task t = new Task ();
            t.Priority = 'Medium';
            t.Subject = 'Follow up on shipped order' + event.Order_Number__c;
            t.OwnerId = myUser.Id;
            tasks.add(t);
        }
    }

    insert tasks;
}

Then I looked up the challenge on stack exchange and rewrote my code to be like the code that I found (challenge rejected it: Publishing an Order_Event__e did not create the associated task successfully. Please check your trigger and event.):

trigger OrderEventTrigger on Order_Event__e (after insert)
{
    List<Task> tasks = new List<Task>();

    //User myUser = [SELECT Id FROM User WHERE Name = 'Elizabeth Rodriguez' LIMIT 1];
    String usr = UserInfo.getUserId();

    for (Order_Event__e event : Trigger.new)
    {
        if (event.Has_Shipped__c == true)
        {
            Task t = new Task ();
            t.Priority = 'Medium';
            t.Status = 'New';
            t.Subject = 'Follow up on shipped order' + event.Order_Number__c;
            t.OwnerId = usr;//myUser.Id;
            tasks.add(t);
        }
    }

    insert tasks;
}

However, if I just copy and paste the code from the stack exchange the challenge accepts it. I've been trying to find the difference between my code and the code that I found for about 30 minutes and I can't find it. Here's the link and the code that I found on stack exchange:
https://developer.salesforce.com/forums/?id=9060G000000BhoQQAS

trigger OrderEventTrigger on Order_Event__e (after insert) {
    // List to hold all cases 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 Case to dispatch new team.
            task ts = new task();
            ts.Priority = 'Medium';
            ts.status = 'New';
            ts.Subject = 'Follow up on shipped order ' + event.Order_Number__c;
            ts.OwnerId = '00528000005NTRJ';  // Active User Id 
            tasks.add(ts);
        }
   }

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

Any ideas with what's wrong with my first two code blocks?

Best Answer

String usr = UserInfo.getUserId();

isn't going to work well in a Platform Event trigger context, because the event trigger runs in the context of the Automated Process user - not the user whose action initiated the posting of the Platform Event. See Considerations for Publishing and Subscribing to Platform Events with Apex and APIs:

In platform event triggers, if you create a Salesforce record that contains an ownerId field, set the ownerId field explicitly to the appropriate user. Platform event triggers run under the Automated Process entity. If you don’t set the ownerId field on records that contain this field, the system sets the default value of Automated Process.

So that's why, I would say, your Version 2 does not work. (Or at least one reason why; see also kurunve's comment noting that you have a spacing issue).

Hard-coding the User Id is never a good idea in production, but it will likely work in this very limited context.

Great suggestion from CharlesT in comments - you can also refer to the CreatedById field on the Platform Event to determine which use posted the Platform Event. That may not be what you want in this specific Trailhead context, though.