Going with the “Flow”


The other day there was a question posted on the Salesforce Stack Exchange that asked how to convert a trigger on an Opportunity to a button. The requirement was one that I have heard before, take all of the Opportunity Line Items and convert them to Assets. The first thing that popped in my head was — I bet you can use a headless (autolaunch) flow for this! It had been a couple of years since I’ve really used Visual Workflow, but I had been reading and hearing a lot about some really cool new features, so I fired up my Developer org to prove it out. Let’s walk through the process together.

First we’ll create a new Flow with the variables we will need:

  1. Navigate to Flows in the setup menu and create a New Flow
  2. Next create the variable that will hold the Id of the Account the Opportunity belongs too
    • From the Resources pane, double click Variable
    • Enter in the Unique Name: var_AcctId 
    • Select Input for the Input/Output Type
  3. Create another variable to hold the Id of the Opportunity
    • From the Resources pane, double click Variable
    • Enter in the Unique Namevar_OppId 
    • Select Input for the Input/Output Type
  4. Now let’s create an sObject Collection Variable to hold all of the related Line Items
    • From the Resources pane, double click sObject Collection Variable
    • Enter in the Unique Name: list_Line_Items
    • Select Private for the Input/Output Type
    • Select the Object Type: Opportunity Line Item and click OK
  5. Next we’ll create another sObject Collection Variable to hold the Asset records to be inserting
    • From the Resources pane, double click sObject Collection Variable
    • Enter in the Unique Namelist_Assets
    • Select Private for the Input/Output Type
    • Select the Object Type: Asset and click OK
  6.  We’ll also need a matching sObject Variable for each Collection, let’s start with the Line Item
    • From the Resources pane, double click sObject Variable
    • Enter in the Unique Name: var_Line_Item
    • Select Private for the Input/Output Type
  7. Now we’ll create the Asset variable
    • From the Resources pane, double click sObject Variable
    • Enter in the Unique Name: var_Asset
    • Select Private for the Input/Output Type

FlowElements

Now that we have all of our variables, we can start putting our Flow together.

  1. The first thing we’ll need to do is query the database for all related Line Items
    • From the Palette pane, drag the Fast Lookup element over to your Canvas
    • Enter in the Name: Lookup Line Items
    • Select OpportunityLineItem for the Look up under Filters and Assignments
      • Select OpportunityId for the Field with the equals Operator
      • Select list_Line_Items for the Variable to assign the results to
      • Select the Name, Product2Id and Quantity fields and click OK
  2. Next we’ll loop through our Line Item list to generate the Asset records
    • From the Palette pane, drag the Loop element over to your Canvas
    • Enter in the NameConvert Line Items to Assets
    • Leave the default Unique Name
    • Select list_Line_Items for the Loop through option
    • Select var_Line_Item for the Loop Variable and click OK
  3. Now we’ll need to populate the Asset variable with data from each Line Item
    • From the Palette pane, drag the Assignment element over to your Canvas
    • Enter in the Name: Create Asset
    • Populate the following Asset fields with values from the var_Line_Item:
      • var_Asset.AccountId = var_AcctId
      • var_Asset.Product2Id = var_Line_Item.Product2Id
      • var_Asset.Name = var_Line_Item.Name
      • var_Asset.Quantity = var_Line_Item.Quantity
    • Click OK
  4. Next we’ll need to add that newly generated Asset to our Asset List
    • From the Palette pane, drag over another Assignment element
    • Enter in the Name: Add to Asset List
    • Select the list_Asset for the Variable
    • Select add for the Operator
    • Select the var_Asset for the Value and click OK
  5. The next thing we’ll need to do is insert the list of Assets
    • From the Palette pane, drag the Fast Create element over to your Canvas
    • Enter in the Name: Insert Assets List
    • Select the list_Asset for the Variable and click OK
  6. Now we need to tie it all together by connecting the elements
    • Drag a line from the Fast Lookup to the Loop element
    • Drag a line from the Loop element to the Create Asset element (leave the default for each value in the collection)
    • Drag a line from the Create Asset element over to the Add to Asset List element
    • Drag a line from the Add to Asset List back to the Loop element
    • Drag a line from the Loop element over to the Fast Create element

With the Flow finished, we will need to designate the start element and Save our changes and Activate

  1. Set the Fast Lookup as your Start element
  2. Click Save
    • Enter in the Name: Convert Line Items to Assets
    • Leave the default Unique Name
    • Leave the default Autolaunched Flow for the Type
    • Leave the default Interview Label and click OK
  3. Click Close
  4. Click Activate to the left of our Flow name

Great, now we have our flow, but how to do we go about populating the two input fields and attach it to a button?

  1. Navigate to Pages in the Setup menu and create a New page
  2. Enter in the Label: Create Assets
  3. Copy over the following page and Save:

The last step is to setup a custom button on the Opportunity that calls our Visualforce page.

  1. Navigate to the Buttons, Links and Actions option under the Opportunities object in Setup and create a New Button or Link
  2. Enter in the Label: Create Assets
  3. Select Detail Page Button for the Display Type
  4. Select Display in existing window without sidebar or header for the Behavior
  5. Select Visualforce Page for the Content Source
  6. Select Create Assets for the Content and Save
  7. Add the new Custom Button to your Opportunity Page Layouts

Let’s test it out!

  1. Navigate to an Opportunity with related Opportunity Line Items
  2. Click the Create Assets button
  3. You should now be on the related Account page with the newly generated Assets in the related list

 

Leave a comment

Your email address will not be published. Required fields are marked *