INTRODUCTION TO EXTERNAL SERVICES – PART 2

The major inspiration for this blog was my webinar that I did for the Salesforce Apex Hours and for the Official Salesforce Developer APAC Webinar Series. Here is the link to the recording of both these webinars in case if you are interested –

title slide Easy-no-code-integrations-with-External-Services-and-Visual

  1. Salesforce Apex Hours – https://www.youtube.com/watch?v=dpspn5sVbIU
  2. Offcial Salesforce Developer APAC Webinar – https://developer.salesforce.com/events/webinars/easy-no-code-integration

That being said, my quest to explore and understand more about External Services was another driving factor behind these webinars. Well I think it’s time to say that I have started loving this feature and I think it has already stole my heart. Without further ado, let me reveal what I would be exploring in this blog post. In this series, I would be explaining about another use case using External Services – “Building a Currency Converter using Flows and External Services

Alright so hang tight and tag along to see how you can build this with absolutely NO CODE!

Shameless plug: I would highly recommend you to read the first blog post in this series in order to get a grasp of External Services. Here is the link –
https://shrutisridharan.wordpress.com/2018/02/12/introduction-to- external-services- part-1/

Currency Exchange API

Let’s explore the Currency Exchange API. It is a free API for current foreign exchange rates published by different providers such as Fixer, currencylayer etc. In our case, we will be using Fixer.

The Currency Exchange API has several query string parameters. The following are the parameters:

  • provider: this is to specify the Currency Exchange Rates provider that we are using. And its fixer for us.
  • fr: this is to specify the from currency or the Source currency from which we are converting the Opportunity Amount.
  • to: this is to specify the to currency or the Target currency to which we are converting the Opportunity Amount.
  • response (optional): this is to specify the type of the response that we would like to receive. Currency Exchange API returns the response in two formats. One is JSON and the other format is Text. If we do not specify this parameter, then by default, it returns a text.

Writing a Swagger Schema for the Currency Exchange API

Now let me explain you the Swagger Schema for the Currency Exchange API.

{
  "swagger" : "2.0",
  "info" : {
    "description" : "Currency Exchange API",
    "version" : "1.0.0",
    "title" : "Currency Exchange API"
  },
  "host" : "currency-exchange.apphb.com",
  "schemes" : [ "https" ],
  "paths" : {
    "/api/rates" : {
      "get" : {
        "summary" : "Convert from one currency to the other",
        "produces" : [ "text/plain" ],
        "parameters" : [ {
          "in" : "query",
          "name" : "provider",
          "description" : "Currency Exchange Rate providers like fixer, currency layer etc",
          "required" : true,
          "type" : "string"
        }, {
          "in" : "query",
          "name" : "fr",
          "description" : "Source currency",
          "required" : true,
          "type" : "string"
        }, {
          "in" : "query",
          "name" : "to",
          "description" : "Target currency",
          "required" : true,
          "type" : "string"
        } ],
        "responses" : {
          "200" : {
            "description" : "Result of the conversion",
            "schema" : {
              "type" : "string"
            }
          }
        }
      }
    }
  }
}
  1. info node – This is self-explanatory.
  2. host node – The domain or the host of the API that needs to be integrated.
  3. scheme node – The protocol supported by the API. This is an array of values and could be httphttps or both.
  4. paths node – Different paths or routes to the API.
    1. /api/rates – The API endpoint exposed by Currency Exchange API.
    2. GET node – Type of the verb.
      1. produces node – Return type of the API endpoint. This could be values like
        text/plain’, ‘application/json’ etc.
      2. parameters node – Different parameters that the GET verb supports.
        1. provider parameter –
          1. in node – Defines how the parameter is sent to the API. In our
            example it is a query string parameter.
          2. name node – Name of the query string parameter.
          3. type node – Data type of the parameter. This can take values
            like string, number etc.
        2. … (I guess you get the idea for the remaining)
      3. responses node –
        1. 200 node – Defines what will be returned when the API call is
          successful. And in our case, the return type is a string.

Creating the Named Credentials

Named Credential is what you use to define the type of authentication for your API Endpoint. Salesforce currently supports three types of Named Credential. One is Anonymous which means No Authentication, another using Username and Password and oAuth. In our case, the Currency Exchange API does not have any authentication and hence it is Anonymous.

Screenshot_21

Registering External Services

The next step after creating Named Credential is to register for a new External Service. Here you will have to specify the Swagger Schema for the API you are making the call to. You can also specify the Swagger Schema via a URL Path.

The Flow Apex Action

Alright! Now the Visual Workflow. The flow has a screen to get the user inputs. The From currency and To currency.

Screenshot_22

Once it has fetched the currency inputs, API call is made to get the current currency rate for the selected currency. This is done by dragging the Apex Action that was created as a result of registering for a new External Service. You can find the recently created Apex Action on the left side in the Palette. In this, you will have to specify the input parameters as well as the output parameter.

Screenshot_34

I am collecting the output response in a text variable called ‘Response’.

Screenshot_25

If you remember, the Currency Exchange API returns the response in a text format. Hence it is our responsibility to convert it into a number format so that we can perform calculations with it. For that I am using a formula as shown below.

Screenshot_24

Then the next thing is to display the current rate and also ask for confirmation if they are ready to convert or not. If they select no, then they will be taken to the beginning where they can alter their selections.

Screenshot_35

Screenshot_27

Screenshot_28

And if they choose Yes, then the respected Opportunity record will be queried using Fast Lookup and the Id of the Opportunity record which spawned the Flow is received in a variable called ‘recordId’. Exactly in this manner.

Screenshot_36

The converted amount is calculated in a formula as shown below:

Screenshot_31

Once that is done, we use an Assignment to assign the converted amount to the custom field Converted_Amount__c of Opportunity record.

Screenshot_30

Finally, we update the Opportunity record using Fast Update.

Screenshot_32

It also has a screen which is used to display the converted amount at the end.

Screenshot_33

Glimpse into the Flow

So here is the flow as a whole.

Screenshot_37

Here is a small video showing the demo:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s