Monday, December 16, 2013

Advanced Data Manipulation With Flows

Salesforce Visual Flow provides a relatively easy means to design task-specific user interfaces for end users to interact with data.  But it is important to note that Flow data is not permanent and exists only during one user's execution of the Flow.  You easily can connect Flow data to your Salesforce database, Visualforce pages and Apex code as well.

See my previous posts for more information about how Flows can simplify difficult processes for Salesforce users and tips for styling Flows

Record Lookup

Don't forget to save data if you need it!
If you intend for users to save the data, it can simply be written to Salesforce via the Flow data creation elements.  Similarly, there are elements for retrieving, updating, and deleting Salesforce
data. 

One of the important elements for connected Flow data to your Salesforce database is Lookup.  You can use the record lookup element to prevent duplicates when users enter data using Flow forms.  Note that the lookup will only return one record at a time.  If, for example, your form requests four fields for a Lead: first and last name as well as an email address and company, you can use all four to match the newly input data with existing data in Salesforce.  An exact match will be returned for Pat Smith with the same company name and email address.  It will not return matches with similar company names.

In addition, if there are already two Pat Smith leads with the same company and email address, currently only one of them will be returned.  At Dreamforce 2013, there was some talk about future releases returning a collection of records.

Once you have sorted out duplicates, you can determine whether the Flow data should be used with the Create element to create a new record in Salesforce or with the Update element to modify an existing record.  Make sure your Flow users have the appropriate object permissions to perform these tasks.

Error Messages

When you connect Flow data and Salesforce data, note that Salesforce might generate errors.  For example, because Flows respect user permissions and security settings as well as required fields, a user could try to save or retrieve data that is not appropriate for them.  In these cases, Salesforce will return an error.

Use a Decision Branch to process these "Fault" behaviors after linking to Salesforce.  For example, you can present error message Screens or send a user back to a previous Screen to start over.  

Accessing Flow Variables

If you need to use it in more complex processes, Visualforce and Apex can help.  As I mentioned, Flow data is not automatically retained and is unique to the specific user's current execution of the Flow.  Variables within the Flow can be made accessible to Apex and Visualforce by changing their settings from "Private", for use only in the specific Flow, to "Input", "Output" or "Input and Output".  These settings will also determine whether a Flow variable can be accessed by other Flows when one Flow contains another existing Flow as a subflow.

Note that the only way Visualforce by itself can access Flow variables is when it sends initial values to an Input variable.  For example, if your Visualforce page calls a Flow named JustSteps, it can pass a value to start at a specific step.  The code for passing a variable to specify step 3 might look like the following on the Visualforce page:

   <apex:page>
    <flow:interview name="JustSteps">
        <apex:param name="stepNumber" value="3"/>
    </flow:interview>
   </apex:page>

The Flow would then need to incorporate a Decision element at the start to check the stepNumber Input variable.

Note that Visualforce cannot change the value of a variable after the Flow has been initiated, nor can it retrieve values from Output variables without the use of an Apex controller as well.

Apex & the Flow

You can gain even more control over Flow behavior by using an Apex controller with your Flow.  The most basic controller provides your Visualforce pages access to output variables.  For example, a simple controller might look like the following:

   public class FlowClass {
    public Flow.Interview.JustSteps myflow { get; set; }
   }

Note that the  Flow name is the same in the class and in the Visualforce page that refers to the class. 
In this case the Visualforce page that uses the controller and retrieves values for a Flow Output variable called "stepsRemaining" might look something like this:

   <apex:page controller="FlowClass">

    <flow:interview name="JustSteps" interview="{!myflow}" />
   
    <apex:outputText value="Remaining steps: {!myflow.stepsRemaining}  "/> 

   </apex:page>

Another way to get more out of your Flow is through the use of an Apex plug-in.  Note that plug-in code needs to have a particular bit of code in use in order to implement the Apex Process.Plugin Interface.  An Apex plug-in can then be incorporated like any other element directly into your Flow and can assist with more complex data processing such as creating and saving attachments or converting Leads.

Once you start delving deeply into Apex and Visualforce for use with your Flow, you will probably want to reconsider managing the process.  If your process is very code intensive, you might then decide to bypass Flow and create what you need with just Visualforce and Apex.  For those who prefer not to code any more than they have to, Flow is a great alternative.

No comments:

Post a Comment