Solving Subscribe to a Platform Event in an Apex Trigger Challange


The trailhead challange says:

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.
  • 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: event.CreatedById
    • (Note: You are assigning the task OwnerId to the same user who published the event. This step keep things simple so you don't have to perform any prerequisites.)

For this, you only need to create the OrderEventTrigger:

trigger OrderEventTrigger on Order_Event__e (after insert) {

    //List to hold all tasks to be created.
    List<Task> tasks = new List<Task>();
    
    //Getting the user id.
    String userId = UserInfo.getUserId(); 

    for (Order_Event__e event : Trigger.New ) {
        // Creating a task for every event where Has_Shipped__c == true 
        // and adding it to the tasks list.
        if(event.Has_Shipped__c == true){
            tasks.add(new Task(
                Priority = 'Medium',
                Subject = 'Follow up on shipped order ' + event.Order_Number__c, 
                OwnerId = userId
                ));
        }
    }

    //Bulk insert of all the tasks created.
    insert tasks;

}


Comentarios

Publicar un comentario

Entradas más populares de este blog

Solving Schedule Jobs Using the Apex Scheduler

Solving Use Batch Apex Challenge