Use Salesforce Data in Apex Code
We use SOQL to query a Salesforce org and use its data with our code. In this step, we use SOQL to get information about the product that we want to add as the bonus orderItem.
Add SOQL to Get Data from Salesforce
- In your class, under the comment
//TO DO 3.1
(near line 5), paste this code:// Use SOQL to get the ID of the bonus bouquet and store it in an sObject variable called bonusProduct List<Product2> bonusProductList = [SELECT Id, ProductCode FROM Product2 WHERE ProductCode = 'BOT-BB-12']; Product2 bonusProduct = new Product2(); if(bonusProductList.size() > 0) { bonusProduct = bonusProductList[0]; // Use SOQL to get the price book entry ID associated with the bonusProduct and store it in an sObject variable called entry // Every Product has an assosiated PricebookEntry List<PricebookEntry> entryList = [SELECT Id, Product2Id FROM PricebookEntry WHERE Product2Id = :bonusProduct.Id]; PricebookEntry entry = new PricebookEntry(); if(entryList.size() > 0) { entry = entryList[0]; }
What are we doing here?
First, we use SOQL to get the ID and ProductCode for the bouquet product that has the product code BOT-BB-12
, and store the ID in a list called bonusProductList
. Then we check the number of items in bonusProductList
. If there is at least one value in the list, we define a new Product2 sObject, called bonusProduct
, and assign the first sObject in the bonusProductList
list to it.
Next, we use another SOQL query to get the ID of the PricebookEntry for the bonusProduct
. We store the PricebookEntry ID in a PricebookEntry list called entryList
.
Finally, we check the number of items in entryList
. If there is at least one value in the list, then we define a new PricebookEntry sObject, called entry
, and assign the first sObject in the entryList
list to it.
Use DML to Send Data to Salesforce
If you completed the Object-Oriented Programming for Admins module, you might remember that DML is the data manipulation language that we use to send data to a Salesforce org. As an admin, you know DML as the Insert (create a record), Update (edit a record), and Delete (delete a record) statements that you use with data manipulation tools, such as Data Loader.
We’ve created one or more free bouquets and added them to a list. But they exist only in Apex code. We haven’t saved anything to the Salesforce org. We use DML to do that.
- In your class, under the comment
//TO DO 3.2
, paste this code:insert newBouquets;
- Under the comment
//TO DO 2.4
, remove the comment characters (//
) from the line that reads://PricebookEntryId = entry.id,
- Finally, we need to end the
if
statement (the code that runs if we determine we have a bonus bouquet).
In your class, under the comment//TO DO 3.3
, paste this code:} //end if
- Save your class.
Now your class 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 // Use SOQL to get the ID of the bonus bouquet and store it in an sObject variable called bonusProduct List<Product2> bonusProductList = [SELECT Id, ProductCode FROM Product2 WHERE ProductCode = 'BOT-BB-12']; Product2 bonusProduct = new Product2(); if(bonusProductList.size() > 0) { bonusProduct = bonusProductList[0]; // Use SOQL to get the price book entry ID associated with the bonusProduct and store it in an sObject variable called entry // Every Product has an assosiated PricebookEntry List<PricebookEntry> entryList = [SELECT Id, Product2Id FROM PricebookEntry WHERE Product2Id = :bonusProduct.Id]; PricebookEntry entry = new PricebookEntry(); if(entryList.size() > 0) { entry = entryList[0]; } //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 insert newBouquets; //TO DO 3.3: Close the if section } //end if } //end method } //end class