Erfassen Sie Ihre Fortschritte
Trailhead-Startseite
Trailhead-Startseite

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.

An icon representing the Salesforce Orders object with an arrow labeled SOQL pointing to a table of data matching your query.

Add SOQL to Get Data from Salesforce 

  1. In your class, under the heading //TO DO 3.1 (near line 7), 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
    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.

Note

Note


The numbering of items in a list always starts with 0, so bonusProductList[0] refers to the first item in the list.

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.

A bouquet of flowers labeled OrderItem sObject with an arrow labeled DML pointing to an OrderItem icon within a cloud labeled Record saved in Salesforce OrderItem object.

  1. In your class, under the heading //TO DO 3.2 (near line 38), paste this code:
    insert newBouquets;
  2. Under the heading //TO DO 2.4 (near line 35), remove the comment characters ( // ) from the line that reads: //PricebookEntryId = entry.id,
  3. 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 2.1: Create a list to store any new bouquets we'll insert later
        List<OrderItem> newBouquets = new List<OrderItem>();
        //TO DO 3.1: Get the values for the bonus product we want to add to the order
        // Use SOQL to get the ID of the bonus bouquet and store it in a 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
        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.2: Loop over our orders from the trigger for each order in the list (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;
    } //end method
} //end class