Calling External Services
After reading a recent blog called “INTRODUCTION TO EXTERNAL SERVICES” by Shruti Sridharan I was inspired to start looking at a good use case to start calling an external API by using a combination of:
- Process Builder
- and External Services
That opportunity finally came around when I visited a customer who was entering a lot of Vehicle information manually into Salesforce. A great opportunity for integration I thought!
To start, I started looking at companies that offer DVLA lookups via API. I found their API details, got a test account and then went through the steps in Salesforce to set up the external service.
To get started
Once you have successfully set up the Named Credential and the External Service you are able to call them from Flows or from Process Builder.
I started testing it with a Screen Flow, as it allowed me to see any error messages that were coming up whilst I made it.
I tried out 2 approaches for adding the external service flow to the system.
1. Process Builder (Autolaunched Flow)
Firstly I wanted to experiment with using Process Builder to call the flow. This would mean that an end user wouldn’t ever see it, the Flow would just add more data to the record in the background.
Once you have the flow working as intended, just create a Process Builder and then under Actions you can add Apex Flow. From there, you can select any of the services in your external service.
I struggled with this approach. At first I was getting errors when I added the flow as an Immediate Action on the process builder (more detail on that one blow), then I was getting another error about flow paused/waiting each time, and finally when I thought I’d cracked it everything ran smoothly but the record never got updated!
My takeaways on using Flow with Process Builder were:
- Always used Scheduled Actions rather than immediate1
2. Flow on Lightning Page (Screen Flow)
Then, I also wanted to try adding the Flow itself to the record page and/or a button so that a user would actually see it.
Once you have a flow calling your Apex Service and you have successfully tested it it is now time to add it to your record.
Just click on the cog icon and select Edit Page. Then in the Lightning Page editor choose Flow Component and drag it to the area you want to show it.
N.B. One really important setting is passing the recordID of the page you are working in to the Flow.
The schema is not recognised by Salesforce
When I first saved the URL for the API I wasn’t seeing any of the available services that I could use. Long story short it turns out that Salesforce needs the API schema to be in a particular format as mentioned in the documentation: “External Services supports the Interagent hyper-schema using JSON and Swagger Open API 2.0.” If the API you are using is not in this format your options are either:
- Ask the API provider to send you a schema in one of these formats
- Create your own Swagger schema based on your understanding of the actions available within the API
An Apex error occurred: System.UnexpectedException: Salesforce System Error: 149982672-76353 (677431025) (677431025)
This error took me a while to debug. It’s not a very useful message is it?
After trying lots of different setting changes I finally pasted the URL of the API I was calling into my browser and then compared the full strings. I had a typo in the Named Credential. After correcting the URL it worked a treat.
Error Occurred: An Apex error occurred: System.TypeException: Invalid conversion from runtime type Integer to String
This also took a while to track down. I couldn’t understand what was being converted and why it would suddenly change from one type to the other.
This turned out to be an issue with the API schema given to me by the company providing the service. In the Swagger schema, each field type is described. However, when Salesforce receives the values it also decides what type of information it is. If they disagree you get this error.
To solve this, I had to manually change the schema to line up the field types against each field.
An Apex error occurred: System.CalloutException: Received error response – You have uncommitted work pending. Please commit or rollback before calling out.
When I first added the Flow to the Process Builder, I put it in the Immediate Actions box. When creating a record, I would get this error about uncommitted work each time.
After reading a few posts on the Success community and on Stackexchange I now understand that External Services always need to be called in @Future and therefore you can’t specify them as immediate along with other actions.
I moved the Flow to a Scheduled Action and the error messages stopped.
External Services allow you to call outside APIs with standard configuration rather than needing to code. If you have a well-defined compliant schema having Salesforce generate all of the code for you is great!
I hope that this blog, along with other great write ups from Shruti and others encourage you to try this approach out.
What data is being manually keyed into your Salesforce environment that could be brought in from another system? Phone numbers / Addresses / Car Information / Stock Information / Sales Information?
Original blog by Shruti – https://shrutisridharan.wordpress.com/2018/02/12/introduction-to-external-services-part-1/