2

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?

  • 1
    Just a small nitpick, but the official Salesforce developer forum is not the same as this site (or stackoverflow/stackexchange in general) – Derek F Aug 1 '18 at 14:32
  • 3
    Subject has space character after order block in last code block. Newer rely on checking mechanism but rather test functionality by yourself before clicking submit button. – kurunve Aug 1 '18 at 14:36
4
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.

| improve this answer | |
  • Would it work to check the CreatedBy field of the Platform Event? – Charles T Aug 1 '18 at 14:43
  • Good thought, @CharlesT - yeah, it does. The CreatedById of the event object is the Id of the running user that posted the platform event. – David Reed Aug 1 '18 at 15:12
3

When I find myself up against a problem, and I'm working with a vague error message or I'm otherwise convinced that my code looks correct, I like to keep Sherlock Holmes in mind

When you eliminate the impossible, whatever remains, however improbable, must be the truth

The difference between all 3 iterations of the code that you've posted comes down to how you determine which user is assigned as the owner of the task, and a missing space in your subject (thanks to Kurunve for pointing that out). Since everything else is the same, this is where your problem must lie.

Addressing the issue is then a process of trying one thing at a time, and collecting data in logs to inform you of the differences between each attempted run.

Using system.debug() and looking at debug logs is a powerful combination when it comes to working out the kinks in your code.
If you doubt that a record is being created, debug the results of a query that should return that new record.
If you think the user could be different, debug the user record and compare the logs between two different runs.
The point here is to gather the information you want to know in a place that you can see it.

My process for working out this issue would have been something like this

My initial thought was that you must have been assigning the Task to an owner that Trailhead wasn't expecting. That was the most obvious difference between your three code blocks.

I'd system.debug() the tasks list, or the user (or userId) being used as the owner. Then, I'd run the successful code and note the userId. After that, I'd run the unsuccessful code, and compare the userId.

If that didn't fix the problem, then it's time to stop assuming that the other data being populated in the Task is correct. Go through each field one by one and check it against what Trailhead is prescribing.

If I still couldn't pick out the missing space in the task subject, then I'm probably too close to the code (or out of brainpower for the day) and would take a step back and work on something else.

If I came back to the problem later, and still couldn't figure it out, I'd either ask someone else (with fresh eyes) to look at it, or use a diff tool like Meld or P4Merge.

| improve this answer | |
  • 1
    Thank you so much for the advice! I greatly appreciate that you took the time to tell me about what your process would look like. I will definitely keep that in mind as I continue to learn and grow! – Elizabeth Rodriguez Aug 1 '18 at 15:18

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.