+ Start a Discussion
Javier Peña LucenaJavier Peña Lucena 

Platform Events Basics 3/3 Subscribe to Platform Events - Challenge solution

Easy challenge, copying the code of CloudNewsTrigger and including small changes, but I have stucked with the UserId.

Using this code for OrderEventTrigger you will pass the course:
 
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
    //Group queue = [SELECT Id FROM Group WHERE Name='Regional Dispatch' LIMIT 1];
     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 ts = new Task();
            ts.Priority = 'Medium';
            ts.Status = 'New';
            ts.Subject = 'Follow up on shipped order ' + event.Order_Number__c;
            ts.OwnerId = usr;//queue.Id;
            tasks.add(ts);
        }
   }
    
    // Insert all tasks corresponding to events received.
    insert tasks;

}

Good luck!
Siva N R ChamarthySiva N R Chamarthy
Thank you. it works!
Raghu Singh RawatRaghu Singh Rawat
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;
}

it Is Look like thi. 
Krishna RadhakrishnanKrishna Radhakrishnan
Basically you need to do an execute anonymous window, and run the following sample of inserting an Order Event.
Order_Event__e newsEvent = new Order_Event__e(
             Has_Shipped__c = true);
Database.SaveResult sr = EventBus.publish(newsEvent);

After that do the 'Check challenge'.
Jhonny Alexander Ramirez Chiroque 4Jhonny Alexander Ramirez Chiroque 4
A mi me funciono con este codigo
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
    //Group queue = [SELECT Id FROM Group WHERE Name='Regional Dispatch' LIMIT 1];
     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 ts = new Task();
            ts.Priority = 'Medium';
            ts.Status = 'New';
            ts.Subject = 'Follow up on shipped order ' + event.Order_Number__c;
            ts.OwnerId = usr;//queue.Id;
            tasks.add(ts);
        }
   }
    
    // Insert all tasks corresponding to events received.
    insert tasks;

}
 
Surya Pratap Singh 13Surya Pratap Singh 13
Clean and correct code is here, check it out :
 
trigger OrderEventTrigger on Order_Event__e (after insert) {
    List<Task> taskList = new List<Task>();
    for(Order_Event__e event : Trigger.new){
        if(event.Has_Shipped__c == true){
             Task taskObj = new Task();
            taskObj.Priority='Medium';
            taskObj.Status='New';
            taskObj.Subject='Follow up on shipped order '+event.Order_Number__c ; 
            taskObj.OwnerId =Userinfo.getUserId() ;
            taskList.add(taskObj);
        }
    }
     insert taskList ;
}