[SalesForce] Trailhead: Subscribe to Platform Events

Running into a problem for this trailhead

I have the following OrderEventTrigger.apxt apex trigger

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 for task owner
    User usr = [SELECT Id FROM User WHERE Name='Admin User' LIMIT 1];


    // Iterate through each notification.
    for (Order_Event__e event : Trigger.New) {
        if (event.Has_Shipped__c == true) {
            // Create task 
            Task tk = new Task(Priority='Medium', status='New',subject='Follow up on shipped order ' + event.Order_Number__c, ownerId=usr.Id);
            tasks.add(tk);

        }
   }

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

running into the following 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.

Best Answer

The OwnerId is supposed to be set using the current user Id.

You can use the System method: UserInfo.getUserId() to get the current user Id.

Related Topic