+ 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.
Graeme GlebeGraeme Glebe
Hi everyone, I have followed the advice given by Shishir, but I am still getting the same error message as everyone else in this thread. Here is my code for the record:

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 task owner
     User q = [Select Id From User Where IsActive = true LIMIT 1];
    // Iterate through each notification.
    for (Order_Event__e event : Trigger.New) {
        if (event.Has_Shipped__c == true) {
            // Create task  to dispatch new team.
            Task ts = new task();
            ts.Priority = 'Medium';
            ts.Subject = 'Follow-up on shipped order ' +
                event.Order_Number__c;
            ts.OwnerId = event.CreatedById;
            tasks.add(ts);
        }
   }

I have also tested the code with the following test, which doesn't give me any errors.
@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();
           Database.SaveResult sr = EventBus.publish(newsEvent);
        Test.stopTest();
         System.assertEquals(true, sr.isSuccess());
  List<Case> cases = [SELECT Id FROM Case];
        // Validate that no cases were found.
        System.assertEquals(0, cases.size());
    }
}
    // Insert all cases corresponding to events received.
    insert tasks;
}

Could it be something with the way my events are configured in my org, or is it my code? Any help is much appreciated.

Sincerely,

Graeme
Alena DrachovaAlena Drachova
Hi, Graeme Glebe.
May be this should help. After adding tasks to list you should insert them.
// Trigger for listening to Order events.
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 new Task.
            Task task = new Task();
            task.Status = 'New';
            task.Priority = 'Medium';
            task.Subject = 'Follow up on shipped order ' + 
                event.Order_Number__c;
            task.OwnerId = event.CreatedById;
            tasks.add(task);
        }
   }
    // Insert all tasks corresponding to events received.
    insert tasks;
}