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
- Click and select Developer Console.
- Click File | New | Apex Class.
- Name the class
OrderItemUtility
and click OK.
- 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
- 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.
- 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>();
- 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 theordersFromTrigger
list. The for loop evaluates each order and names the ordercurrentOrder
.
- 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 thefor
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.
- 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:Here we create the OrderItem sObject, calledOrderItem 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 );
freeBouquet
, and then assign values to its fields.
- 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 theadd
method that's built into Apex, making it easy to add an item to a list.
In this statement,newBouquets
is the name of the list, add is the method, andfreeBouquet
(in parentheses) is the sObject that is added to thenewBouquets
list.
- Finally, we need to end the
if
statement (the code that runs only for activated orders) and thefor
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:Now your class file should look like this:} //end if } //end for loop
//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
- 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!