Add Products to an Opportunity via a Modal

Goal

For this tutorial, you’ll create a modal that allows users to add product line items to a single opportunity. To do this, you’ll use Salesforce’s Opportunity and OpportunityLineItems objects within a custom opportunity detail page.

Download XML

Example

This is an example page. Its data resets each time the page is loaded.

Concepts covered

This tutorial uses Skuid features to create an opportunity detail page and a product line items modal. For more information about these features, see the following:

Prerequisite: Create a custom opportunity detail page

A custom detail page for the Opportunity object is essential to complete this tutorial. Build a custom detail page and ensure you have the following:

  • A model of the Opportunity object with the following:
    • Fields
      • Description
      • Name
      • Opportunity ID
      • Opportunity Type
      • Price Book ID
        • This must be included to ensure line items can be added
    • Conditions
      • A condition on Opportunity Id set to the Id Page/URL parameter, which limits the page to a single Opportunity record
  • A Header component with the following properties:
    • Model: Opportunity
    • Title: {{Name}}
    • Subtitle: Opportunity
  • A Form component with the following properties:
    • Model: Opportunity
    • Default mode: Read with inline-edit
    • Show save and cancel buttons: Checked
    • Section properties > Show section heading: Unchecked
  • The following fields added to the Form component:
    • Description
    • Opportunity Type
    • Price Book ID
  • If the components look cramped on the page, try editing all of the margins to the same value. We recommend 16px, which is Regular spacing in the Ink design system.

Once you have your opportunity detail page, follow the instructions below to build the Add product modal on that page.

Create product line item models

To add products to a single opportunity, you’ll need two models–one to view the opportunity’s products and another to create new line items records attached to the opportunity. This is made possible by the Opportunity Line Items object in Salesforce, which serves as a junction object. This object stores records for the many-to-many relationship between opportunities (on the Opportunity object) and products (on the Product2 object).

Create the Opportunity Line Item List model

This model returns the products for a single opportunity record. This is accomplished by adding fields from Salesforce’s Opportunity Line Items object and defining a condition that refers to the Id field on the existing Opportunity model (which you created when setting up the detail page).

  1. Click the models Models tab.

  2. Click add Add Model.

  3. Set your model’s properties:

    • Model ID: OpportunityLineItemsList
    • Data source type: salesforce
    • Data source: Current Salesforce Org
    • Salesforce object name: OpportunityLineItem
  4. Click on edit-form Fields under your new model.

  5. Check the following fields:

    • Opportunity ID
      • Opportunity.Name
    • Opportunity Product Name
    • Product ID
      • Product2.Name
    • Quantity
    • Sales Price
  6. Click on rules Conditions under your new model.

  7. Click add Add.

  8. Define the condition:

    • Field: Opportunity ID
    • Operator: =
    • Value:
      • Content: Field from another model
      • Source model: Opportunity
      • Source field: Id

Create the New Line Item model

This model stores the new products that will be added to the opportunity in the Add product modal. This model also contains a condition that limits the opportunity line items to a single opportunity record and an action that queries the opportunity line item list model whenever this model is saved. To save time, we’ll duplicate the model you’ve already created and turn off the query on page load property.

  1. Click the models Models tab.

  2. Click the OpportunityLineItemsList model you created in the first step.

  3. Click copy Duplicate.

  4. Set the model properties:

    • Model ID: NewLineItem
    • Query on page load: Unchecked
    • Max # of records (limit): 1

Add components and a modal for product line items

Add Table component

This table will contain a list of the products associated with the opportunity and will update when users add products to the opportunity.

  1. Drag and drop a Table component under the Form component.

  2. Set your Table component’s properties:

    • Model: OpportunityLineItemsList
    • Default mode: Read with inline-editing
    • Show save and cancel buttons: Checked
  3. Click the Styles tab > Spacing.

  4. Set all four margins to the same spacing value. (We recommend 16px for all four margins.)

  5. Click chevron-small-down Add fields > Model fields.

  6. Check the following fields:

    • Opportunity Product Name
    • Quantity
    • Sales Price
  7. Click Apply. The Table updates with the selected fields.

  8. Click the Opportunity Product Name field in the Table.

  9. Set the field properties:

    • Required: Checked
  10. Repeat the last two steps for the Quantity and Sales Price fields.

Add button and modal actions

To set up a modal that allows users to add products, you first need to add a Button Set component. You’ll configure the button to create a row in the model and show the modal. Additionally, an opportunity must have a value in the Pricebook2Id field to add line items; you’ll set up an enable condition so that the button is only enabled when the Pricebook2Id field has a value.

  1. Drag and drop a Button Set component above the new Table component.

  2. Set the Button Set component’s properties:

    • Model: NewLineItem
    • Position: Left
  3. Click the newly-added Run actions button in the component.

  4. Set the button properties:

    • Button label: Add product
  5. Click the Display logic tab.

  6. Click Enable conditions.

  7. Set the enable condition’s properties:

    • Enable if…: ALL conditions are met
    • Message to show when disabled: Must specify a pricebook to enable
  8. Click add next to Enable conditions.

  9. Set the new condition’s properties:

    • Source type: Model field value
    • Source model: Opportunity
    • If no rows in model…: Ignore this condition
    • Field: Pricebook2Id
    • Operator: !=
    • Content: None - blank value
  10. Click the Styles tab > Spacing.

  11. Set all four margins to the same spacing value. (We recommend 16px for all four margins.)

  12. Click the Actions tab.

  13. Click add Add action.

  14. Set the following action properties:

    • Action type: Create new rows
    • Model: NewLineItem
    • Place row at…: Start of model data
  15. Click add Add action again.

  16. Set the following action properties:

    • Action type: Show modal
  17. Click open next to Configure modal. A popup appears on the canvas.

  18. Set the modal properties:

    • Title: Add product
    • Restrict closing the modal: Checked
  19. Click Save.

Configure the modal

The modal is a pop-up that overlays the main page and puts the focus on a specific set of components and data; in this case, we’ll focus on the data needed to add a product to an opportunity. You’ll add a Form component, which allows end users to enter relevant field information. You’ll also add two custom buttons that include actions to save, close, and query your page’s models.

Add a Form component

  1. Drag and drop a Form component into the modal on the canvas.

  2. Set the Form component’s properties:

    • Model: NewLineItem
    • Default mode: Edit
    • Show save and cancel buttons: Unchecked
  3. Click the Styles tab > Spacing.

  4. Set all four margins to the same spacing value. (We recommend 16px for all four margins.)

  5. On the canvas, click the section in your Form component.

  6. Set the section properties:

    • Show section heading: Unchecked
  7. Click chevron-small-down Add fields > Model fields.

  8. Check the following fields:

    • Product ID
    • Quantity
    • Sales Price
  9. Click Apply. The Form updates with the selected fields.

Add a button to save

This custom save button includes actions that save the product information in the NewLineItems model, queries the OpportunityLineItemsList model, closes the modal, and empties all of the rows from the NewLineItems model. The last action ensures that the modal doesn’t retain any previous entries when re-opened.

  1. In the modal, click add Add button to footer.

  2. Click the newly added Run actions.

  3. Set the button properties:

    • Button label: Save
  4. In the button properties pane, click Actions.

  5. Click add Add action.

  6. Set the action properties:

    • Action type: Save model changes
    • Models to save: NewLineItem
  7. Click add Add action again.

  8. Set the action properties:

    • Action type: Query models
    • Models to query: OpportunityLineItemsList
    • Query behavior: Get more – Merge in new rows with old
  9. Click add Add action again.

  10. Set the action properties:

    • Action type: Close modals
    • Behavior: Close all
  11. Click add Add action again.

  12. Set the action properties:

    • Action type: Remove all rows from model
    • Models to empty: NewLineItem

Add a button to cancel

This button cancels all edits to the NewLineItems model through the Cancel model changes action. The Cancel model changes action cancels all changes in the model, including the newly created row that hasn’t been saved yet. For more about actions, see the Model Data Changes Actions topic.

  1. In the modal, click add Add button to footer.

  2. Click the newly added Run actions.

  3. Set the button properties:

    • Button label: Cancel
  4. In the button properties pane, click Actions.

  5. Click add Add action.

  6. Set the action properties:

    • Action type: Cancel model changes
    • Model: NewLineItem
  7. Click add Add action again.

  8. Set the action properties:

    • Action type: Close modals
    • Behavior: Close all
  9. Click Save.

Preview the page for a single record

Choose the Opportunity record you want to preview

  1. Click Preview.
  2. Select an opportunity from the list. The selected opportunity’s Id field will be used to populate the condition we created that limits your page to a single opportunity.
  3. Click Go.
  4. Click Add product to test out the new modal.

Deploy the page

After you’ve previewed your page and ensured it’s working properly, it’s time to deploy it to your end users. See our deployment docs for more information or check out our other page tutorials.