function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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;
}

 
Shibu Nair 7Shibu Nair 7
Thanks @nagendra. The space after the subject text was the issue for me. 
Scott Song 4Scott Song 4
@Graeme Glebe

It should be "Follow up" instead of "Follow-up".
 
Suryanarayan BeheraSuryanarayan Behera
Better you can try with this code :
trigger OrderEventTrigger on Order_Event__e (after insert) {
// Trigger for listening to Order_Event events.
    // 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.Subject = 'Follow up on shipped order ' + event.Order_Number__c;
            ts.OwnerId = event.CreatedById;
            tasks.add(ts);
        }
   }
    // Insert all cases corresponding to events received.
    insert tasks;
}
Raj Ramachandran 14Raj Ramachandran 14
Following code works and passed 

trigger OrderEventTrigger on Order_Event__e (after insert) {
  List<Task> Tasks = new List<Task>();
    // Get queue Id for case owner
    Group queue = [SELECT Id FROM Group WHERE Name='Regional Dispatch' AND Type='Queue'];
    // Iterate through each notification.
    for (Order_Event__e event : Trigger.New) {
        if  (event.Has_Shipped__c == true) {
            // Create Case to dispatch new team.
            Task tsk = new Task();
            tsk.Priority = 'Medium';
            tsk.Status = 'New';
            tsk.Subject =  'Follow up on shipped order ' + event.Order_Number__c;
            tsk.OwnerId = event.CreatedById;
            Tasks.add(tsk);
        }
   }
    // Insert all cases corresponding to events received.
    insert Tasks;
}
Raj Ramachandran 14Raj Ramachandran 14
there are few mandatory fields in tasks that has to be taken care :)
Shashank Sharma 47Shashank Sharma 47
Mine was failing because Follow up on shipped order DID NOT contain a space at the end inside the string value 'Follow up on shipped order '.
Marcin StolarzMarcin Stolarz
In my case the issue was with the User ID - I used the example provided in the unit, so I referred to the queue rather than User Id. When changed to the User object and reffered to its ID rather using event.CreatedById; it worked 
Nick BleibtreyNick Bleibtrey
My failure was caused by having Task's "Medium" Priority inactive.  Once I updated activated the "Medium" picklist value it worked.
Abdullah NafisAbdullah Nafis
I have complted this task without an error and completed the chanllange: Let me know if you have any query
// Trigger for listening to Order_Event__e events.
trigger OrderEventTrigger on Order_Event__e (after insert) {
    // List to hold all cases 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.Status = 'New';
            ts.Subject = 'Follow up on shipped order 105';
            ts.OwnerId = event.CreatedById;
            tasks.add(ts);
        }
   }
    // Insert all tasks corresponding to events received.
    insert tasks;
}
Mahesh PalanMahesh Palan
I think issue is
a)we have done lot of customization to task object while doing prev trailblazer course so there could be some field required etc 

Here is how i solve the problem

a)Use another playground and create order event again but this time with shipped field default as checked (I think their test developer might have bug )

B)copy the above trigger code 

c)it orked perfectly well 
Allan Torres 7Allan Torres 7
I have tried all of the above and have had no luck, can you help me, please?
Shubhankar Das 5Shubhankar Das 5
Hi Everyone,

I think Mahesh was right, because when I set the debug log I found that due to task trigger it was failing to create Task, so make sure to inactivate any task trigger or validation rules or look up filter whatever may be that'swhy its important to set debug log.

Thanks!
Shubhankar