Sunday 7 September 2014

Invoking Apex Callouts through Visual Flows

Few weeks back I was attending a Webinar on Advanced Flow From SFDCBill(Product Manager Flows Salesforce.com) .I was quite surprised to discover flows are very powerful feature provided by the platform for the administrator to configure the business logic .

My friend Rakesh has been blogging lot of use cases of flows for Salesforce 1 and using chatter objects .You can refer his blog for more details on the work he has done .I have learned lot from those and I am thankful to him for sharing his experience with visual flows through his blog


http://rakeshistom.wordpress.com/category/workflow-rule/visual-workflow-flow/


One of the things that came was invoking callouts or webservices through flows and SFDCBill was able to communicate the design that one needs apex and process plugin for invoking callouts directly from the flow .I did a quick online search to see if this has been demonstrated  and was not able to get some good resource so i thought of sharing a sample tutorial on how to invoke callouts from flows directly.

The more information on the process plugin can be obtained from below URLs ,

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_process_plugin.htm
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_process_plugin_example_lead_convert.htm

The Process plugin is an apex Interface and that needs to be implemented in order to pass request into the apex class from the flow input or screen and the apex interface returns the response back to the flow.

Lets discuss how we can make a web service or apex callouts from flow directly .

1)Our first step will be to write the apex plugin class that makes a webservice callout to external website and process and makes the data ready for passing back to the flow.

For webservice part I have taken a sample REST service provided for free for learning purpose from the website http://api.wunderground.com/weather/api/

The web-service takes input of state and city of US and outputs current weather condition in the form of JSON objects .Note that you need to sign up and generate an API key for your use .

The input of state and city can be taken from flow screen and it can be passed directly as request parameter to the apex class .

Here is the complete apex class for the flow plugin



2)For JSON parsing ,I have used another class shown below ,which handles parsing of the data .This can be easily generated using https://json2apex.herokuapp.com/ by providing sample response JSON.

Lets dig into the creation of flows ,

Visual workflow basics can be studied from following resources 

http://help.salesforce.com/help/pdfs/en/salesforce_vpm_implementation_guide.pdf
http://www.salesforce.com/us/developer/docs/workbook_vf/workbook_vf.pdf

3)We will need below three components 

i))Input Screen to take data of city and state from User need as input for our webservice
ii)WeatherAppApex Plugin we have just developed using apex
iii)Output Screen to display the result



The input screen will have two fields to capture input 



The Apex Plugin will have a mapping for both input and output from result.The below screen shots show the input and output mappings .Note for output mapping for further usage of output  parameters flow variables are created




The output Screen is last piece to display the results .

Now lets see the outcome of this work in below video



I hope you enjoyed this tutorial and looking forward to your comments below .

Please note error handling in code and Test classes are left as it was just a POC code .Be careful to take care of all these when implementing for client code .

Happy Coding !!!

Introducing Lightning Base Components

Lightning Base Components are great addition to the platform and in fact revolutionary .One of the concerns around lightning component ...