Skip to main content

Create the Automation to Run the Free Bouquet Promotion

In this step we create a new class and a new method, in which we check the status of all orders. If an order is activated, then we automatically add a bonus bouquet to the order.

As you look at the code, remember that any text that starts with // is a code comment. In code, comments describe the purpose of a block of code (one or more lines of code). Comments help developers understand what code to write, what existing code does, and what failing code is supposed to do.

Create a Class and a Method

  1. Click Setup and select Developer Console.
  2. Click File | New | Apex Class.
  3. Name the class OrderItemUtility and click OK.
  4. Replace the existing code with this code:
    //Create the class
    public class OrderItemUtility {
        //Create the method that will add free bonus bouquet when order is activated
        public static void addBonusBouquet(List<Order> ordersFromTrigger) {
            //TO DO 3.1: Determine if we have a bonus product and get its ID to add to the order
                //TO DO 2.1: Create a list to store any new bouquets we'll insert later
                //TO DO 2.2: Loop over orders in ordersFromTrigger, for each order (called currentOrder) do something
                    //TO DO 2.3: Verify the order status is 'Activated'
                        //TO DO 2.4: Create a new bouquet and set values
                        //TO DO 2.5: Add the freeBouquet sObject to your list
                    //TO DO 2.6: Close the "if" and "for loop" sections
                //TO DO 3.2: Use DML to add the new list of newBouquets
            //TO DO 3.3: Close the if section
        } //end method
    } //end class
  5. Click File | Save to save the class.

Add Automation to Create a New Bouquet

Next we add automation to the class. Once you paste the additional lines of code you may notice a code comment, //, added to the blank line. Don't forget to delete the extra comment before you paste your code.

  1. Create a list to store new bonus bouquets.
    In your class, under the comment //TO DO 2.1, paste this code:
    List<OrderItem> newBouquets = new List<OrderItem>();
  2. Use a for loop to iterate through orders sent from a trigger. (We create that trigger later.)
    In your class, under the comment //TO DO 2.2, paste this code:
    for(Order currentOrder : ordersFromTrigger) {

    This is the first part of the for loop. It takes all the orders from the trigger and adds them to the ordersFromTrigger list. The for loop evaluates each order and names the order currentOrder.
Note

You might notice that we only have one curly brace ( { ) here. That's because we do more with this order before we stop. We close the for loop later in this step.

  1. Next we look at each order (using the currentOrder variable that we created) and ask: Is the order activated? If it is, then we do something. If it isn't, then we move on to the next order and ask the same question.
    In your class, under the comment //TO DO 2.3, paste this code:
    if(currentOrder.Status == 'Activated') {

    As you saw with the for loop code, there's only one curly brace. We'll add the other one after we finish everything we want to do to each activated order.
  2. When we find an order that is indeed activated, we create an sObject for a new OrderItem.
    In your class, under the comment //TO DO 2.4, paste this code:
    OrderItem freeBouquet = new OrderItem(
        OrderId = currentOrder.id, //this is the order we're linking the bouquet to
        //PricebookEntryId = entry.id,
        numberOfFlowers__c = 3,
        description = 'FREE Bouquet',
        Quantity = 1,
        colorTheme__c = 'Spectacular Sunset',
        percentOfOpening__c = 0,
        UnitPrice = 0.00
    );
    Here we create the OrderItem sObject, called freeBouquet, and then assign values to its fields.
  3. Next let's add the sObject to the list that we created in //TO DO 2.1.
    In your class, under the comment //TO DO 2.5, paste this code:
    newBouquets.add(freeBouquet);

    We're using the add method that's built into Apex, making it easy to add an item to a list.
    newBouquets.add(freeBouquet);
    In this statement, newBouquets is the name of the list, add is the method, and freeBouquet (in parentheses) is the sObject that is added to the newBouquets list.
  4. Finally, we need to end the if statement (the code that runs only for activated orders) and the for loop (the code that runs for each order as the loop cycles through all activated orders).
    In your class, under the comment //TO DO 2.6, paste this code:
        } //end if
    } //end for loop
    Now your class file should look like this:
    //Create the class
    public class OrderItemUtility {
        //Create the method that will add free bonus bouquet when order is activated
        public static void addBonusBouquet(List<Order> ordersFromTrigger) {
            //TO DO 3.1: Determine if we have a bonus product and get its ID to add to the order
                //TO DO 2.1: Create a list to store any new bouquets we'll insert later
                List<OrderItem> newBouquets = new List<OrderItem>();
                //TO DO 2.2: Loop over orders in ordersFromTrigger, for each order (called currentOrder) do something
                for(Order currentOrder : ordersFromTrigger) {
                    //TO DO 2.3: Verify the order status is 'Activated'
                    if(currentOrder.Status == 'Activated') {
                        //TO DO 2.4: Create a new bouquet and set values
                        OrderItem freeBouquet = new OrderItem(
                            OrderId = currentOrder.id, //this is the order we're linking the bouquet to
                            //PricebookEntryId = entry.id,
                            numberOfFlowers__c = 3,
                            description = 'FREE Bouquet',
                            Quantity = 1,
                            colorTheme__c = 'Spectacular Sunset',
                            percentOfOpening__c = 0,
                            UnitPrice = 0.00
                        );
                        //TO DO 2.5: Add the freeBouquet sObject to your list
                        newBouquets.add(freeBouquet);
                    //TO DO 2.6: Close the "if" and "for loop" sections
                    } //end if
                } //end for loop
                //TO DO 3.2: Use DML to add the new bouquet to the Order
            //TO DO 3.3: Close the if section
        } //end method
    } //end class
  5. Click File | Save to save the OrderItemUtility class.

In the next unit, you learn how to get data from and send data to Salesforce and you create a trigger. Then the fun really begins—you test your code!

在 Salesforce 帮助中分享 Trailhead 反馈

我们很想听听您使用 Trailhead 的经验——您现在可以随时从 Salesforce 帮助网站访问新的反馈表单。

了解更多 继续分享反馈