+ Start a Discussion
Peter McGavinPeter McGavin 

Publishing an Order_Event__e did not create the associated task successfully. Please check your trigger and event

I cannot complete the Subscribe to Platform Events challenge. When I click the 'Check Challenge' button I get the following error:

Publishing an Order_Event__e did not create the associated task successfully. Please check your trigger and event.

Here is my code:

trigger OrderEventTrigger on Order_Event__e (after insert) {
    // List to hold all tasks to be created.
    List<Task> tasks = new List<Task>();
    
    // Get queue Id for case owner
    String usr = UserInfo.getUserId(); 
        
       
    // Iterate through each notification.
    for (Order_Event__e event : Trigger.New) {
        if (event.Has_Shipped__c == true) {
            // Create Task to dispatch new team.
            Task t = new Task();
            t.Priority = 'Medium';
            t.Status = 'New';
            t.Subject = 'Follow up on shipped order' + event.Order_Number__c;
            t.OwnerId = Usr;
            tasks.add(t);
        }
   }
    
    // Insert all tasks corresponding to events received.
    insert tasks;
}

Please assist. Thanks. 
Best Answer chosen by Peter McGavin
NagendraNagendra (Salesforce Developers) 
Hi Peter,

Sorry for this issue you are encountering.

May I suggest you please refer to the below code which might help you.
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;
}
Hope this helps.

Kindly mark this as solved if it's resolved.

Thanks,
Nagendra
 

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Peter,

Sorry for this issue you are encountering.

May I suggest you please refer to the below code which might help you.
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;
}
Hope this helps.

Kindly mark this as solved if it's resolved.

Thanks,
Nagendra
 
This was selected as the best answer
Peter McGavinPeter McGavin
Thanks Nage
SHISHIR BANSALSHISHIR BANSAL

Hi All, 

The above answer will make you pass the challenge but if you want to check what error your code made is . Everything in there in the section and the only line that is wrong is 

 

tcase.Subject  = 'Follow up on shipped order'+ event.Order_Number__c;

there should be space after order

tcase.Subject  = 'Follow up on shipped order '+ event.Order_Number__c;


And you will pass :)  #HappyCoding

M UsamaM Usama
@Nagendra

Still getting the same error :

Publishing an Order_Event__e did not create the associated task successfully. Please check your trigger and event.
Manikantha nayakManikantha nayak
hello nagedra even iam getting the same error 
Abhisek Das 6Abhisek Das 6
Shishir's suggestion worked for me. Please try it out.